|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Rectangle = require('./Rectangle'); |
| 8 | +var Intersects = require('../intersects/RectangleToRectangle'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Takes two Rectangles and first checks to see if they intersect. |
| 12 | + * If they intersect it will return the area of intersection in the `out` Rectangle. |
| 13 | + * If they do not intersect, the `out` Rectangle will have a width and height of zero. |
| 14 | + * |
| 15 | + * @function Phaser.Geom.Rectangle.Intersection |
| 16 | + * @since 3.11.0 |
| 17 | + * |
| 18 | + * @generic {Phaser.Geom.Rectangle} O - [rect,$return] |
| 19 | + * |
| 20 | + * @param {Phaser.Geom.Rectangle} rectA - The first Rectangle to get the intersection from. |
| 21 | + * @param {Phaser.Geom.Rectangle} rectB - The second Rectangle to get the intersection from. |
| 22 | + * @param {Phaser.Geom.Rectangle} [out] - A Rectangle to store the intersection results in. |
| 23 | + * |
| 24 | + * @return {Phaser.Geom.Rectangle} The intersection result. If the width and height are zero, no intersection occurred. |
| 25 | + */ |
| 26 | +var Intersection = function (rectA, rectB, out) |
| 27 | +{ |
| 28 | + if (out === undefined) { out = new Rectangle(); } |
| 29 | + |
| 30 | + if (Intersects(rectA, rectB)) |
| 31 | + { |
| 32 | + out.x = Math.max(rectA.x, rectB.x); |
| 33 | + out.y = Math.max(rectA.y, rectB.y); |
| 34 | + out.width = Math.min(rectA.right, rectB.right) - out.x; |
| 35 | + out.height = Math.min(rectA.bottom, rectB.bottom) - out.y; |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + out.setEmpty(); |
| 40 | + } |
| 41 | + |
| 42 | + return out; |
| 43 | +}; |
| 44 | + |
| 45 | +module.exports = Intersection; |
0 commit comments