Rectangle

Kind of class:class deprecated
Inherits from:none
Author:Arthur Clemens
Classpath:org.asapframework.util.types.Rectangle
File last modified:Monday, 29 January 2007, 15:33:53
Deprecated Use this class for Flash 7 projects; projects that use Flash 8 or higher use the Adobe Rectangle class.
This Rectangle class copies the interface of the Rectangle class distributed with Flash 8, to make Rectangle usable for Flash 7 projects.

The Rectangle class is used to create and modify Rectangle objects. A Rectangle 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. Be careful when you design these areas--if a rectangle is described as having its upper-left corner at 0,0 and has a height of 10 and a width of 20, the lower-right corner is at 9,19, because the count of width and height began at 0,0.

The x, y, width, and height properties of the Rectangle 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--if you change right, you are changing width; if you change bottom, you are changing height, and so on. And you must have the left or x property established before you set width or right property.

Summary


Constructor
  • Rectangle (inX:Number, inY:Number, inWidth:Number, inHeight:Number)
    • Creates a new Rectangle object whose top-left corner is specified by the inX and inY parameters.
Instance properties
  • mX : Number
  • mY : Number
  • mWidth : Number
  • mHeight : Number
  • bottom : Number
    • The sum of the y and height properties.
  • bottomRight : Point
    • The location of the Rectangle object's bottom-right corner, determined by the values of the x and y properties.
  • height : Number
    • The height of the rectangle in pixels.
  • left : Number
    • The x coordinate of the top-left corner of the rectangle.
  • right : Number
    • The sum of the x and width properties.
  • size : Point
    • The size of the Rectangle object, expressed as a Point object with the values of the width and height properties.
  • top : Number
    • The y coordinate of the top-left corner of the rectangle.
  • topLeft : Point
    • The location of the Rectangle object's top-left corner determined by the x and y values of the point.
  • width : Number
    • The width of the rectangle in pixels.
  • x : Number
    • The x coordinate of the top-left corner of the rectangle.
  • y : Number
    • The y coordinate of the top-left corner of the rectangle.
Instance methods
  • clone : Rectangle
    • Returns a new Rectangle object with the same values for the x, y, width, and height properties as the original Rectangle object.
  • contains (inX:Number, inY:Number) : Boolean
    • Determines whether the specified point is contained within the rectangular region defined by this Rectangle object.
  • containsPoint (inPoint:Point) : Boolean
    • Determines whether the specified point is contained within the rectangular region defined by this Rectangle object.
  • containsRectangle (inRect:Rectangle) : Boolean
    • Determines whether the Rectangle object specified by the rect parameter is contained within this Rectangle object.
  • equals (inRectangle:Rectangle) : Boolean
    • Determines whether inRectangle is equal to this Rectangle object.
  • inflate (inX:Number, inY:Number) : Void
    • Increases the size of the Rectangle object by the specified amounts.
  • inflatePoint (inPoint:Point) : Void
    • Increases the size of the Rectangle object.
  • intersection (inRectangle:Rectangle) : Rectangle
    • If the Rectangle object specified in the inRectangle parameter intersects with this Rectangle object, the intersection() method returns the area of intersection as a Rectangle object.
  • getIntersectionRectangle (inRectangle:Rectangle) : Rectangle
  • intersects (inRectangle:Rectangle) : Boolean
    • Determines whether the object specified in the toIntersect parameter intersects with this Rectangle object.
  • offset (inX:Number, inY:Number) : Void
    • Adjusts the location of the Rectangle object, as determined by its top-left corner, by the specified amounts.
  • offsetPoint (inPoint:Point) : Void
    • Adjusts the location of the Rectangle object using a Point object as a parameter.
  • union (inRectangle:Rectangle) : Rectangle
    • Adds two rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two rectangles.
  • isEmpty : Boolean
    • Determines whether or not this Rectangle object is empty.
  • setEmpty : Void
    • Sets all of the Rectangle object's properties to 0.
  • toString : String
    • Builds and returns a string that lists the horizontal and vertical positions and the width and height of the Rectangle object.

Constructor

Rectangle

function Rectangle (
inX:Number, inY:Number, inWidth:Number, inHeight:Number)

Creates a new Rectangle object whose top-left corner is specified by the inX and inY parameters. If you call this constructor function without parameters, a rectangle with x, y, width, and height properties set to 0 is created.
Parameters:
inX :
the x coordinate of the top-left corner of the rectangle
inY :
the y coordinate of the top-left corner of the rectangle
inWidth :
the width of the rectangle in pixels
inHeight:
the height of the rectangle in pixels

Instance properties

bottom

bottom:Number
(read,write)

The sum of the y and height properties.

bottomRight

bottomRight:Point
(read,write)

The location of the Rectangle object's bottom-right corner, determined by the values of the x and y properties.

height

height:Number
(read,write)

The height of the rectangle in pixels.

left

left:Number
(read,write)

The x coordinate of the top-left corner of the rectangle.

mHeight

private mHeight:Number
(read)

mWidth

private mWidth:Number
(read)

mX

private mX:Number
(read)

mY

private mY:Number
(read)

size

size:Point
(read,write)

The size of the Rectangle object, expressed as a Point object with the values of the width and height properties.

top

top:Number
(read,write)

The y coordinate of the top-left corner of the rectangle.

topLeft

topLeft:Point
(read,write)

The location of the Rectangle object's top-left corner determined by the x and y values of the point.

width

width:Number
(read,write)

The width of the rectangle in pixels.

x

x:Number
(read,write)

The x coordinate of the top-left corner of the rectangle.

y

y:Number
(read,write)

The y coordinate of the top-left corner of the rectangle.

Instance methods

clone

function clone (

Returns a new Rectangle object with the same values for the x, y, width, and height properties as the original Rectangle object.
Returns:
  • A new Rectangle object with the same values for the x, y, width, and height properties as the original Rectangle object.

contains

function contains (
inX:Number, inY:Number) : Boolean

Determines whether the specified point is contained within the rectangular region defined by this Rectangle object.
Parameters:
inX:
the x-value (horizontal position) of the point
inY:
the y-value (vertical position) of the point
Example:
  • var rectangle:Rectangle = new Rectangle(10, 10, 50, 50);
    trace(rectangle.contains(59, 59)); // true
    trace(rectangle.contains(10, 10)); // true
    trace(rectangle.contains(60, 60)); // false

containsPoint

function containsPoint (
inPoint:Point) : Boolean

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.
Parameters:
inPoint:
the point, as represented by its x,y values
Returns:
  • If the specified point is contained within this Rectangle object, returns true; otherwise false.
Example:
  • var rectangle:Rectangle = new Rectangle(10, 10, 50, 50);
    trace(rectangle.containsPoint(new Point(10, 10))); // true
    trace(rectangle.containsPoint(new Point(59, 59))); // true
    trace(rectangle.containsPoint(new Point(60, 60))); // false

containsRectangle

function containsRectangle (
inRect:Rectangle) : Boolean

Determines whether the Rectangle object specified by the rect parameter is contained within this Rectangle object. A Rectangle object is said to contain another if the second Rectangle object falls entirely within the boundaries of the first.
Parameters:
inRect:
the Rectangle object being checked
Returns:
  • If the Rectangle object that you specify is contained by this Rectangle object, returns true; otherwise false.
Example:
  • var rectA:Rectangle = new Rectangle(10, 10, 50, 50);
    var rectB:Rectangle = new Rectangle(10, 10, 50, 50);
    var rectC:Rectangle = new Rectangle(10, 10, 51, 51);
    var rectD:Rectangle = new Rectangle(15, 15, 45, 45);
    
    trace(rectA.containsRectangle(rectB)); // true
    trace(rectA.containsRectangle(rectC)); // false
    trace(rectA.containsRectangle(rectD)); // true

equals

function equals (
inRectangle:Rectangle) : Boolean

Determines whether inRectangle is equal to this Rectangle object. This method compares the x, y, width, and height properties of an object against the same properties of this Rectangle object.
Parameters:
inRectangle:
the rectangle to compare to this Rectangle object
Returns:
  • If the object has exactly the same values for the x, y, width, and height properties as this Rectangle object, returns true; otherwise false.

getIntersectionRectangle

private function getIntersectionRectangle (
inRectangle:Rectangle) : Rectangle

inflate

function inflate (
inX:Number, inY:Number) : Void

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 inX value, and to the top and the bottom by the inY value.
Parameters:
inX:
The value to be added to the left and the right of the Rectangle object. The following equation is used to calculate the new width and position of the rectangle: x -= dx; width += 2 * dx;
inY:
The value to be added to the top and the bottom of the Rectangle object. The following equation is used to calculate the new height and position of the rectangle: y -= dy; height += 2 * dy;

inflatePoint

function inflatePoint (
inPoint:Point) : Void

Increases the size of the Rectangle object. This method is similar to the Rectangle.inflate() method, except that it takes a Point object as a parameter.
Parameters:
inPoint:
resizes the rectangle by the x and y coordinate values of the point
Example:
  • var rect:Rectangle = new Rectangle(0, 0, 2, 5);
    trace(rect.toString()); // (x=0, y=0, w=2, h=5
    
    var myPoint:Point = new Point(2, 2);
    rect.inflatePoint(myPoint);
    trace(rect.toString()); // (x=-2, y=-2, w=6, h=9)
    Similarly the Rectangle can be decreased. In the following example the Rectangle is decrease by 20 pixels at each side:
    import org.asapframework.util.RectangleUtils;
    
    var viewPort:Rectangle = RectangleUtils.boundsOfMovieClip(world_mc);
    var margins:Point = new Point(-20, -20);
    viewPort.inflatePoint(margins);

intersection

function intersection (
inRectangle:Rectangle) : Rectangle

If the Rectangle object specified in the inRectangle parameter intersects with this Rectangle object, the intersection() method 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.
Parameters:
inRectangle:
the Rectangle object to compare against to see if it intersects with this Rectangle object
Returns:
  • 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.
Example:
  • var rectangle1:Rectangle = new Rectangle(0, 0, 50, 50);
    var rectangle2:Rectangle = new Rectangle(25, 25, 100, 100);
    var intersectingArea:Rectangle = rectangle1.intersection(rectangle2);
    trace(intersectingArea.toString()); // (x=25, y=25, w=25, h=25)

intersects

function intersects (
inRectangle:Rectangle) : Boolean

Determines whether the object specified in the toIntersect parameter intersects with this Rectangle object. This method checks the x, y, width, and height properties of the specified Rectangle object to see if it intersects with this Rectangle object.
Parameters:
inRectangle:
the Rectangle object to compare against this Rectangle object
Returns:
  • If the specified object intersects with this Rectangle object, returns true; otherwise false.
Example:
  • var rectA:Rectangle = new Rectangle(10, 10, 50, 50);
    var rectB:Rectangle = new Rectangle(59, 59, 50, 50);
    var rectC:Rectangle = new Rectangle(60, 60, 50, 50);
    var rectAIntersectsB:Boolean = rectA.intersects(rectB);
    var rectAIntersectsC:Boolean = rectA.intersects(rectC);
    trace(rectAIntersectsB); // true
    trace(rectAIntersectsC); // false
    
    var firstPixel:Rectangle = new Rectangle(0, 0, 1, 1);
    var adjacentPixel:Rectangle = new Rectangle(1, 1, 1, 1);
    var pixelsIntersect:Boolean = firstPixel.intersects(adjacentPixel);
    trace(pixelsIntersect); // false

isEmpty

function isEmpty (
) : Boolean

Determines whether or not this Rectangle object is empty.
Returns:
  • If the Rectangle object's width or height is less than or equal to 0, returns true; otherwise false.

offset

function offset (
inX:Number, inY:Number) : Void

Adjusts the location of the Rectangle object, as determined by its top-left corner, by the specified amounts.
Parameters:
inX:
moves the x value of the Rectangle object by this amount
inY:
moves the y value of the Rectangle object by this amount

offsetPoint

function offsetPoint (
inPoint:Point) : Void

Adjusts the location of the Rectangle object using a Point object as a parameter. This method is similar to the Rectangle.offset() method, except that it takes a Point object as a parameter.
Parameters:
inPoint:
a Point object to use to offset this Rectangle object

setEmpty

function setEmpty (
) : Void

Sets all of the Rectangle object's properties to 0. A Rectangle 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.

toString

function toString (
) : String

Builds and returns a string that lists the horizontal and vertical positions and the width and height of the Rectangle object.

union

function union (
inRectangle:Rectangle) : Rectangle

Adds two rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two rectangles.
Parameters:
inRectangle:
a Rectangle object to add to this Rectangle object
Returns:
  • A new Rectangle object that is the union of the two rectangles.