mirror of
https://github.com/photonstorm/phaser
synced 2024-11-22 20:53:39 +00:00
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.
This commit is contained in:
parent
5063fe30e5
commit
f195a09530
3 changed files with 47 additions and 0 deletions
|
@ -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
|
||||
|
||||
|
|
45
src/geom/rectangle/Intersection.js
Normal file
45
src/geom/rectangle/Intersection.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @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;
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue