phaser/Phaser/utils/RectangleUtils.ts

211 lines
11 KiB
TypeScript
Raw Normal View History

/// <reference path="../_definitions.ts" />
2013-05-25 03:21:24 +00:00
/**
2013-08-11 23:52:35 +00:00
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
* @module Phaser
2013-05-25 03:21:24 +00:00
*/
module Phaser {
2013-08-11 23:52:35 +00:00
/**
* A collection of methods useful for manipulating and comparing Rectangle objects.
*
* @class RectangleUtils
*/
2013-05-25 03:21:24 +00:00
export class RectangleUtils {
/**
* Get the location of the Rectangles top-left corner as a Point object.
* @method getTopLeftAsPoint
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
*/
public static getTopLeftAsPoint(a: Phaser.Rectangle, out: Phaser.Point = new Phaser.Point): Phaser.Point {
2013-05-25 03:21:24 +00:00
return out.setTo(a.x, a.y);
}
/**
* Get the location of the Rectangles bottom-right corner as a Point object.
* @method getTopLeftAsPoint
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} out Optional Point to store the value in, if not supplied a new Point object will be created.
* @return {Phaser.Point} The new Point object.
2013-05-25 03:21:24 +00:00
**/
2013-08-11 23:52:35 +00:00
public static getBottomRightAsPoint(a: Phaser.Rectangle, out: Phaser.Point = new Phaser.Point): Phaser.Point {
2013-05-25 03:21:24 +00:00
return out.setTo(a.right, a.bottom);
}
/**
* Increases the size of the Rectangle object by the specified amounts. The center point of the Rectangle object stays the same, and its size increases to the left and right by the dx value, and to the top and the bottom by the dy value.
* @method inflate
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The Rectangle object.
2013-05-25 03:21:24 +00:00
* @param {Number} dx The amount to be added to the left side of the Rectangle.
* @param {Number} dy The amount to be added to the bottom side of the Rectangle.
2013-08-11 23:52:35 +00:00
* @return {Phaser.Rectangle} This Rectangle object.
*/
public static inflate(a: Phaser.Rectangle, dx: number, dy: number): Phaser.Rectangle {
2013-05-25 03:21:24 +00:00
a.x -= dx;
a.width += 2 * dx;
a.y -= dy;
a.height += 2 * dy;
return a;
}
/**
* Increases the size of the Rectangle object. This method is similar to the Rectangle.inflate() method except it takes a Point object as a parameter.
* @method inflatePoint
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} point The x property of this Point object is used to increase the horizontal dimension of the Rectangle object. The y property is used to increase the vertical dimension of the Rectangle object.
* @return {Phaser.Rectangle} The Rectangle object.
*/
public static inflatePoint(a: Phaser.Rectangle, point: Phaser.Point): Phaser.Rectangle {
return Phaser.RectangleUtils.inflate(a, point.x, point.y);
2013-05-25 03:21:24 +00:00
}
/**
* The size of the Rectangle object, expressed as a Point object with the values of the width and height properties.
* @method size
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} output Optional Point object. If given the values will be set into the object, otherwise a brand new Point object will be created and returned.
* @return {Phaser.Point} The size of the Rectangle object
*/
public static size(a: Phaser.Rectangle, output: Phaser.Point = new Phaser.Point): Phaser.Point {
2013-05-25 03:21:24 +00:00
return output.setTo(a.width, a.height);
}
/**
* Returns a new Rectangle object with the same values for the x, y, width, and height properties as the original Rectangle object.
* @method clone
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Rectangle} output Optional Rectangle object. If given the values will be set into the object, otherwise a brand new Rectangle object will be created and returned.
* @return {Phaser.Rectangle}
*/
public static clone(a: Phaser.Rectangle, output: Phaser.Rectangle = new Phaser.Rectangle): Phaser.Rectangle {
2013-05-25 03:21:24 +00:00
return output.setTo(a.x, a.y, a.width, a.height);
}
/**
* Determines whether the specified coordinates are contained within the region defined by this Rectangle object.
* @method contains
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The Rectangle object.
2013-05-25 03:21:24 +00:00
* @param {Number} x The x coordinate of the point to test.
* @param {Number} y The y coordinate of the point to test.
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
2013-08-11 23:52:35 +00:00
*/
public static contains(a: Phaser.Rectangle, x: number, y: number): boolean {
2013-05-25 03:21:24 +00:00
return (x >= a.x && x <= a.right && y >= a.y && y <= a.bottom);
}
/**
* Determines whether the specified point is contained within the rectangular region defined by this Rectangle object. This method is similar to the Rectangle.contains() method, except that it takes a Point object as a parameter.
* @method containsPoint
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The Rectangle object.
* @param {Phaser.Point} point The point object being checked. Can be Point or any object with .x and .y values.
2013-05-25 03:21:24 +00:00
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
2013-08-11 23:52:35 +00:00
*/
public static containsPoint(a: Phaser.Rectangle, point: Phaser.Point): boolean {
return Phaser.RectangleUtils.contains(a, point.x, point.y);
2013-05-25 03:21:24 +00:00
}
/**
* Determines whether the first Rectangle object is fully contained within the second Rectangle object.
* A Rectangle object is said to contain another if the second Rectangle object falls entirely within the boundaries of the first.
* @method containsRect
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
2013-05-25 03:21:24 +00:00
* @return {Boolean} A value of true if the Rectangle object contains the specified point; otherwise false.
2013-08-11 23:52:35 +00:00
*/
public static containsRect(a: Phaser.Rectangle, b: Phaser.Rectangle): boolean {
2013-05-25 03:21:24 +00:00
// If the given rect has a larger volume than this one then it can never contain it
if (a.volume > b.volume)
{
return false;
}
return (a.x >= b.x && a.y >= b.y && a.right <= b.right && a.bottom <= b.bottom);
}
/**
* Determines whether the two Rectangles are equal.
* This method compares the x, y, width and height properties of each Rectangle.
* @method equals
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
2013-05-25 03:21:24 +00:00
* @return {Boolean} A value of true if the two Rectangles have exactly the same values for the x, y, width and height properties; otherwise false.
2013-08-11 23:52:35 +00:00
*/
public static equals(a: Phaser.Rectangle, b: Phaser.Rectangle): boolean {
2013-05-25 03:21:24 +00:00
return (a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height);
}
/**
* If the Rectangle object specified in the toIntersect parameter intersects with this Rectangle object, returns the area of intersection as a Rectangle object. If the Rectangles do not intersect, this method returns an empty Rectangle object with its properties set to 0.
2013-05-25 03:21:24 +00:00
* @method intersection
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @param {Phaser.Rectangle} output Optional Rectangle object. If given the intersection values will be set into this object, otherwise a brand new Rectangle object will be created and returned.
* @return {Phaser.Rectangle} A Rectangle object that equals the area of intersection. If the Rectangles do not intersect, this method returns an empty Rectangle object; that is, a Rectangle with its x, y, width, and height properties set to 0.
*/
public static intersection(a: Phaser.Rectangle, b: Phaser.Rectangle, out: Phaser.Rectangle = new Phaser.Rectangle): Phaser.Rectangle {
2013-05-25 03:21:24 +00:00
if (Phaser.RectangleUtils.intersects(a, b))
2013-05-25 03:21:24 +00:00
{
out.x = Math.max(a.x, b.x);
out.y = Math.max(a.y, b.y);
out.width = Math.min(a.right, b.right) - out.x;
out.height = Math.min(a.bottom, b.bottom) - out.y;
}
return out;
}
/**
* Determines whether the two Rectangles intersect with each other.
* This method checks the x, y, width, and height properties of the Rectangles.
* @method intersects
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
2013-05-25 03:21:24 +00:00
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {Boolean} A value of true if the specified object intersects with this Rectangle object; otherwise false.
2013-08-11 23:52:35 +00:00
*/
public static intersects(a: Phaser.Rectangle, b: Phaser.Rectangle, tolerance: number = 0): boolean {
2013-05-25 03:21:24 +00:00
return !(a.left > b.right + tolerance || a.right < b.left - tolerance || a.top > b.bottom + tolerance || a.bottom < b.top - tolerance);
}
/**
* Determines whether the object specified intersects (overlaps) with the given values.
* @method intersectsRaw
* @param {Number} left
* @param {Number} right
* @param {Number} top
* @param {Number} bottomt
* @param {Number} tolerance A tolerance value to allow for an intersection test with padding, default to 0
* @return {Boolean} A value of true if the specified object intersects with the Rectangle; otherwise false.
2013-08-11 23:52:35 +00:00
*/
public static intersectsRaw(a: Phaser.Rectangle, left: number, right: number, top: number, bottom: number, tolerance: number = 0): boolean {
2013-05-25 03:21:24 +00:00
return !(left > a.right + tolerance || right < a.left - tolerance || top > a.bottom + tolerance || bottom < a.top - tolerance);
}
/**
* Adds two Rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two Rectangles.
2013-05-25 03:21:24 +00:00
* @method union
2013-08-11 23:52:35 +00:00
* @param {Phaser.Rectangle} a The first Rectangle object.
* @param {Phaser.Rectangle} b The second Rectangle object.
* @param {Phaser.Rectangle} output Optional Rectangle object. If given the new values will be set into this object, otherwise a brand new Rectangle object will be created and returned.
* @return {Phaser.Rectangle} A Rectangle object that is the union of the two Rectangles.
*/
public static union(a: Phaser.Rectangle, b: Phaser.Rectangle, out: Phaser.Rectangle = new Phaser.Rectangle): Phaser.Rectangle {
2013-05-25 03:21:24 +00:00
return out.setTo(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.max(a.right, b.right), Math.max(a.bottom, b.bottom));
}
}
}