Skip to content

Commit f195a09

Browse files
committed
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.
1 parent 5063fe3 commit f195a09

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ There is a new Game Object Component called `TextureCrop`. It replaces the Textu
9090
* `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.
9191
* `TransformMatrix.copyFrom` is a new method that will copy the given matrix into the values of the current one.
9292
* `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.
93+
* `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.
9394

9495
### Updates
9596

src/geom/rectangle/Intersection.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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;

src/geom/rectangle/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Rectangle.GetPoint = require('./GetPoint');
2828
Rectangle.GetPoints = require('./GetPoints');
2929
Rectangle.GetSize = require('./GetSize');
3030
Rectangle.Inflate = require('./Inflate');
31+
Rectangle.Intersection = require('./Intersection');
3132
Rectangle.MarchingAnts = require('./MarchingAnts');
3233
Rectangle.MergePoints = require('./MergePoints');
3334
Rectangle.MergeRect = require('./MergeRect');

0 commit comments

Comments
 (0)