API Docs for: 最后更新日期:2014年8月25日
Google搜索   
Show:

File: geom/LRectangle.js

/** @language english
 * <p>A LRectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height.</p>
 * <p>The x, y, width, and height properties of the LRectangle class are independent of each other; changing the value of one property has no effect on the others. However, the right and bottom properties are integrally related to those four properties. For example, if you change the value of the right property, the value of the width property changes; if you change the bottom property, the value of the height property changes.</p>
 * @class LRectangle
 * @constructor
 * @param {float} x The x coordinate of the top-left corner of the rectangle.
 * @param {float} y The y coordinate of the top-left corner of the rectangle.
 * @param {float} width The width of the rectangle, in pixels.
 * @param {float} height The height of the rectangle, in pixels.
 * @since 1.4.1
 * @public
 */
var LRectangle = (function () {
	function LRectangle (x, y, w, h) {
		var s = this;
		s.x = x;
		s.y = y;
		s.width = w;
		s.height = h;
		s.setRectangle();
	}
	LRectangle.prototype = {
		setRectangle : function () {
			var s = this;
			s.bottom = s.y + s.height;
			s.right = s.x + s.width;
			s.left = s.x;
			s.top = s.y;
		},
		/** @language english
		 * Returns a new LRectangle object with the same values for the x, y, width, and height properties as the original LRectangle object.
		 * @method clone
		 * @return {LRectangle} A new LRectangle object with the same values for the x, y, width, and height properties as the original LRectangle object.
		 * @since 1.4.1
		 * @public
		 */
		clone : function () {
			var s = this;
			return new LRectangle(s.x, s.y, s.width, s.height);
		},
		/** @language english
		 * Determines whether the specified point is contained within the rectangular region defined by this LRectangle object.
		 * @method contains
		 * @param {float} x The x coordinate (horizontal position) of the point.
		 * @param {float} y The y coordinate (vertical position) of the point.
		 * @return {Boolean} A value of true if the LRectangle object contains the specified point; otherwise false.
		 * @since 1.4.1
		 * @public
		 */
		contains : function (x, y) {
			var s = this;
			return x >= s.x && x <= s.right && y >= s.y && y <= s.bottom;
		},
		/** @language english
		 * Determines whether the LRectangle object specified by the rect parameter is contained within this LRectangle object. A LRectangle object is said to contain another if the second LRectangle object falls entirely within the boundaries of the first.
		 * @method containsRect
		 * @param {LRectangle} rect The LRectangle object being checked.
		 * @return {Boolean} A value of true if the LRectangle object that you specify is contained by this LRectangle object; otherwise false.
		 * @since 1.4.1
		 * @public
		 */
		containsRect : function (rect) {
			var s = this;
			return rect.x >= s.x && rect.right <= s.right && rect.y >= s.y && rect.bottom <= s.bottom;
		},
		/** @language english
		 * Determines whether the LRectangle object specified by the rect parameter is contained within this LRectangle object. A LRectangle object is said to contain another if the second LRectangle object falls entirely within the boundaries of the first.
		 * @method equals
		 * @param {LRectangle} toCompare The LRectangle object being checked.
		 * @return {Boolean} A value of true if the LRectangle object that you specify is contained by this LRectangle object; otherwise false.
		 * @since 1.4.1
		 * @public
		 */
		equals : function (v) {
			var s = this;
			return v.x == s.x && v.width == s.width && v.y == s.y && v.height == s.height;
		},
		/** @language english
		 * Increases the size of the LRectangle object by the specified amounts, in pixels. The center point of the LRectangle 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
		 * @param {float} dx The value to be added to the left and the right of the LRectangle object.
		 * @param {float} dy The value to be added to the top and the bottom of the LRectangle object. 
		 * @since 1.4.1
		 * @public
		 */
		inflate : function (dx, dy) {
			var s = this;
			s.width += dx;
			s.height += dy;
			s.setRectangle();
		},
		/** @language english
		 * If the LRectangle object specified in the toIntersect parameter intersects with this LRectangle object, returns the area of intersection as a LRectangle object. If the rectangles do not intersect, this method returns an empty LRectangle object with its properties set to 0.
		 * @method intersection
		 * @param {LRectangle} toIntersect The LRectangle object to compare against to see if it intersects with this LRectangle object.
		 * @return {Boolean} A LRectangle object that equals the area of intersection. If the rectangles do not intersect, this method returns an empty LRectangle object; that is, a rectangle with its x, y, width, and height properties set to 0.
		 * @since 1.4.1
		 * @public
		 */
		intersection : function (t) {
			var s = this;
			var ix = s.x > t.x ? s.x : t.x;
			var iy = s.y > t.y ? s.y : t.y;
			var ax = s.right > t.right ? t.right : s.right;
			var ay = s.bottom > t.bottom ? t.bottom : s.bottom;
			if (ix <= ax && iy <= ay) {
				return new LRectangle(ix, iy, ax, ay);
			}else{
				return new LRectangle(0, 0, 0, 0);
			}
		},
		/** @language english
		 * Determines whether the object specified in the toIntersect parameter intersects with this LRectangle object. This method checks the x, y, width, and height properties of the specified LRectangle object to see if it intersects with this LRectangle object.
		 * @method intersects
		 * @param {LRectangle} toIntersect The LRectangle object to compare against this LRectangle object.
		 * @return {Boolean} A value of true if the specified object intersects with this LRectangle object; otherwise false.
		 * @since 1.4.1
		 * @public
		 */
		intersects : function (t) {
			var s = this;
			var ix = s.x > t.x ? s.x : t.x;
			var iy = s.y > t.y ? s.y : t.y;
			var ax = s.right > t.right ? t.right : s.right;
			var ay = s.bottom > t.bottom ? t.bottom : s.bottom;
			return ix <= ax && iy <= ay;
		},
		/** @language english
		 * Determines whether or not this LRectangle object is empty.
		 * @method isEmpty
		 * @return {Boolean} A value of true if the LRectangle object's width or height is less than or equal to 0; otherwise false.
		 * @since 1.4.1
		 * @public
		 */
		isEmpty : function () {
			var s = this;
			return s.x == 0 && s.y == 0 && s.width == 0 && s.height == 0;
		},
		/** @language english
		 * Adjusts the location of the LRectangle object, as determined by its top-left corner, by the specified amounts.
		 * @method offset
		 * @param {float} dx Moves the x value of the LRectangle object by this amount.
		 * @param {float} dy Moves the y value of the LRectangle object by this amount.
		 * @since 1.4.1
		 * @public
		 */
		offset : function (dx, dy) {
			var s = this;
			s.x += dx;
			s.y += dy;
			s.setRectangle();
		},
		/** @language english
		 * Sets all of the LRectangle object's properties to 0. A LRectangle object is empty if its width or height is less than or equal to 0. This method sets the values of the x, y, width, and height properties to 0.
		 * @method setEmpty
		 * @since 1.4.1
		 * @public
		 */
		setEmpty : function () {
			var s = this;
			s.x = 0;
			s.y = 0;
			s.width = 0;
			s.height = 0;
			s.setRectangle();
		},
		/** @language english
		 * Sets the members of LRectangle to the specified values
		 * @method setTo
		 * @param {float} xa the values to set the rectangle to.
		 * @param {float} ya the values to set the rectangle to.
		 * @param {float} widtha the values to set the rectangle to.
		 * @param {float} heighta the values to set the rectangle to.
		 * @since 1.4.1
		 * @public
		 */
		setTo : function (xa, ya, w, h) {
			var s = this;
			s.x = xa;
			s.y = ya;
			s.width = w;
			s.height = h;
			s.setRectangle();
		},
		toString : function () {
			var s = this;
			return "[object LRectangle(" + s.x + "," + s.y + "," + s.width + "," + s.height + ")]";
		},
		/** @language english
		 * Adds two rectangles together to create a new LRectangle object, by filling in the horizontal and vertical space between the two rectangles.
		 * @method union
		 * @param {LRectangle} toUnion A LRectangle object to add to this Rectangle object.
		 * @return {LRectangle}  A new LRectangle object that is the union of the two rectangles.
		 * @since 1.4.1
		 * @public
		 */
		union : function (t) {
			var s = this;
			return new LRectangle(s.x > t.x ? t.x : s.x, s.y > t.y ? t.y : s.y, s.right > t.right ? s.right : t.right, s.bottom > t.bottom ? s.bottom : t.bottom);
		}
	};
	return LRectangle;
})();