diff --git a/CHANGELOG.md b/CHANGELOG.md index 337684548..d39c5cc65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,6 +90,7 @@ There is a new Game Object Component called `TextureCrop`. It replaces the Textu * `TileSprite.tileScaleX` and `tileScaleY` are two new properties that allow you to control the scale of the texture within the Tile Sprite. This impacts the way the repeating texture is scaled, and is independent to scaling the Tile Sprite itself. It works in both Canvas and WebGL mode. * `TransformMatrix.copyFrom` is a new method that will copy the given matrix into the values of the current one. * `TransformMatrix.multiplyWithOffset` is a new method that will multiply the given matrix with the current one, factoring in an additional offset to the results. This is used internally by the renderer code in various places. +* `Rectangle.Intersection` will take two Rectangle objects and return the area of intersection between them. If there is no intersection, an empty Rectangle is returned. ### Updates diff --git a/src/geom/rectangle/Intersection.js b/src/geom/rectangle/Intersection.js new file mode 100644 index 000000000..f39bdd48b --- /dev/null +++ b/src/geom/rectangle/Intersection.js @@ -0,0 +1,45 @@ +/** + * @author Richard Davey + * @copyright 2018 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +var Rectangle = require('./Rectangle'); +var Intersects = require('../intersects/RectangleToRectangle'); + +/** + * Takes two Rectangles and first checks to see if they intersect. + * If they intersect it will return the area of intersection in the `out` Rectangle. + * If they do not intersect, the `out` Rectangle will have a width and height of zero. + * + * @function Phaser.Geom.Rectangle.Intersection + * @since 3.11.0 + * + * @generic {Phaser.Geom.Rectangle} O - [rect,$return] + * + * @param {Phaser.Geom.Rectangle} rectA - The first Rectangle to get the intersection from. + * @param {Phaser.Geom.Rectangle} rectB - The second Rectangle to get the intersection from. + * @param {Phaser.Geom.Rectangle} [out] - A Rectangle to store the intersection results in. + * + * @return {Phaser.Geom.Rectangle} The intersection result. If the width and height are zero, no intersection occurred. + */ +var Intersection = function (rectA, rectB, out) +{ + if (out === undefined) { out = new Rectangle(); } + + if (Intersects(rectA, rectB)) + { + out.x = Math.max(rectA.x, rectB.x); + out.y = Math.max(rectA.y, rectB.y); + out.width = Math.min(rectA.right, rectB.right) - out.x; + out.height = Math.min(rectA.bottom, rectB.bottom) - out.y; + } + else + { + out.setEmpty(); + } + + return out; +}; + +module.exports = Intersection; diff --git a/src/geom/rectangle/index.js b/src/geom/rectangle/index.js index 4836e2755..1589a0c1f 100644 --- a/src/geom/rectangle/index.js +++ b/src/geom/rectangle/index.js @@ -28,6 +28,7 @@ Rectangle.GetPoint = require('./GetPoint'); Rectangle.GetPoints = require('./GetPoints'); Rectangle.GetSize = require('./GetSize'); Rectangle.Inflate = require('./Inflate'); +Rectangle.Intersection = require('./Intersection'); Rectangle.MarchingAnts = require('./MarchingAnts'); Rectangle.MergePoints = require('./MergePoints'); Rectangle.MergeRect = require('./MergeRect');