flash.geom

Class Rectangle

Object


public class Rectangle
extends Object

Player version: Flash Player 8

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.

Rectangle objects are used to support the BitmapData class filters. They are also used in the MovieClip.scrollRect property to support the ability to crop and scroll a MovieClip instance with specific width, height and scrolling offsets.

See also
MovieClip.scrollRect



Property Summary
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.

Properties inherited from class Object
__proto__, __resolve, constructor, prototype


Constructor Summary
Rectangle(x:Number, y:Number, width:Number, height:Number)
Creates a new Rectangle object whose top-left corner is specified by the x and y parameters.


Method Summary
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(x:Number, y:Number) : Boolean
Determines whether the specified point is contained within the rectangular region defined by this Rectangle object.
containsPoint(pt:Point) : Boolean
Determines whether the specified point is contained within the rectangular region defined by this Rectangle object.
containsRectangle(rect:Rectangle) : Boolean
Determines whether the Rectangle object specified by the rect parameter is contained within this Rectangle object.
equals(toCompare:Object) : Boolean
Determines whether the object specified in the toCompare parameter is equal to this Rectangle object.
inflate(dx:Number, dy:Number) : Void
Increases the size of the Rectangle object by the specified amounts.
inflatePoint(pt:Point) : Void
Increases the size of the Rectangle object.
intersection(toIntersect:Rectangle) : Rectangle
If the Rectangle object specified in the toIntersect parameter intersects with this Rectangle object, the intersection() method returns the area of intersection as a Rectangle object.
intersects(toIntersect:Rectangle) : Boolean
Determines whether the object specified in the toIntersect parameter intersects with this Rectangle object.
isEmpty() : Boolean
Determines whether or not this Rectangle object is empty.
offset(dx:Number, dy:Number) : Void
Adjusts the location of the Rectangle object, as determined by its top-left corner, by the specified amounts.
offsetPoint(pt:Point) : Void
Adjusts the location of the Rectangle object using a Point object as a parameter.
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.
union(toUnion:Rectangle) : Rectangle
Adds two rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two rectangles.

Methods inherited from class Object
addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch


Property Detail

bottom Property

public bottom : Number

Player version: Flash Player 8

The sum of the y and height properties.

Example
The following example creates a Rectangle object and changes the value of its bottom property from 15 to 30. Notice that the value of rect.height is also changed, from 10 to 25.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(5, 5, 10, 10);
trace(rect.height); // 10
trace(rect.bottom); // 15

rect.bottom = 30;
trace(rect.height); // 25
trace(rect.bottom); // 30

See also
Rectangle.y, Rectangle.height

bottomRight Property

public bottomRight : Point

Player version: Flash Player 8

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

Example
The following example sets the Rectangle object's bottomRight property using the values of the Point object. Notice that rect.width and rect.height are changed.
import flash.geom.Rectangle;
import flash.geom.Point;

var rect:Rectangle = new Rectangle(1, 2, 4, 8);
trace(rect.bottom);    // 10
trace(rect.right);    // 5
trace(rect.height);    // 8
trace(rect.width);    // 4

var myBottomRight:Point = new Point(16, 32);
rect.bottomRight = myBottomRight;
trace(rect.bottom);    // 32
trace(rect.right);    // 16
trace(rect.height);    // 30
trace(rect.width);    // 15

See also
Point

height Property

public height : Number

Player version: Flash Player 8

The height of the rectangle in pixels. Changing the height value of a Rectangle object has no effect on the x, y, and width properties.

Example
The following example creates a Rectangle object and changes its height property from 10 to 20. Notice that rect.bottom is also changed.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(5, 5, 10, 10);
trace(rect.height); // 10
trace(rect.bottom); // 15

rect.height = 20;
trace(rect.height); // 20
trace(rect.bottom); // 25

See also
Rectangle.x, Rectangle.y, Rectangle.width

left Property

public left : Number

Player version: Flash Player 8

The x coordinate of the top-left corner of the rectangle. Changing the x value of a Rectangle object has no effect on the y, width, and height properties.

The left property is equal to the x property.

Example
The following example changes the left property from 0 to 10. Notice that rect.x also changes.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle();
trace(rect.left);     // 0
trace(rect.x);     // 0

rect.left = 10;
trace(rect.left);     // 10
trace(rect.x);     // 10

See also
Rectangle.x, Rectangle.y, Rectangle.width, Rectangle.height

right Property

public right : Number

Player version: Flash Player 8

The sum of the x and width properties.

Example
The following example creates a Rectangle object and changes its right property from 15 to 30. Notice that rect.width also changes.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(5, 5, 10, 10);
trace(rect.width); // 10
trace(rect.right); // 15

rect.right = 30;
trace(rect.width); // 25
trace(rect.right); // 30

See also
Rectangle.x, Rectangle.width

size Property

public size : Point

Player version: Flash Player 8

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

Example
The following example creates a Rectangle object, retrieves its size (size), changes its size (size), and sets the new values on the Rectangle object. It is important to remember that the Point object used by the size property uses x and y values to represent the width and height properties of the Rectangle object.
import flash.geom.Rectangle;
import flash.geom.Point;

var rect:Rectangle = new Rectangle(1, 2, 4, 8);
var size:Point = rect.size;
trace(size.x); // 4;
trace(size.y); // 8;

size.x = 16;
size.y = 32;
rect.size = size;
trace(rect.x); // 1
trace(rect.y); // 2
trace(rect.width); // 16
trace(rect.height); // 32

See also
Point

top Property

public top : Number

Player version: Flash Player 8

The y coordinate of the top-left corner of the rectangle. Changing the value of the top property of a Rectangle object has no effect on the x, width, and height properties.

The value of the top property is equal to the value of the y property.

Example
This example changes the value of the top property from 0 to 10. Notice that rect.y also changes.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle();
trace(rect.top);    // 0
trace(rect.y);    // 0

rect.top = 10;
trace(rect.top);    // 10
trace(rect.y);    // 10

See also
Rectangle.x, Rectangle.y, Rectangle.width, Rectangle.height

topLeft Property

public topLeft : Point

Player version: Flash Player 8

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

Example
The following example sets the Rectangle object's topLeft property using the values in a Point object. Notice that rect.x and rect.y are changed.
import flash.geom.Rectangle;
import flash.geom.Point;

var rect:Rectangle = new Rectangle();
trace(rect.left);    // 0
trace(rect.top);    // 0
trace(rect.x);    // 0
trace(rect.y);    // 0

var myTopLeft:Point = new Point(5, 15);
rect.topLeft = myTopLeft;
trace(rect.left);    // 5
trace(rect.top);    // 15
trace(rect.x);    // 5
trace(rect.y);    // 15

See also
Point, x, y

width Property

public width : Number

Player version: Flash Player 8

The width of the rectangle in pixels. Changing the value of the width property of a Rectangle object has no effect on the x, y, and height properties.

Example
The following example creates a Rectangle object and change its width property from 10 to 20. Notice that rect.right also changes.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(5, 5, 10, 10);
trace(rect.width); // 10
trace(rect.right); // 15

rect.width = 20;
trace(rect.width); // 20
trace(rect.right); // 25

See also
Rectangle.x, Rectangle.y, Rectangle.height

x Property

public x : Number

Player version: Flash Player 8

The x coordinate of the top-left corner of the rectangle. Changing the value of the x property of a Rectangle object has no effect on the y, width, and height properties.

The x property is equal to the left property.

Example
The following example creates an empty Rectangle and sets its x property to 10. Notice that rect.left is also changed.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle();
trace(rect.x);     // 0
trace(rect.left); // 0

rect.x = 10;
trace(rect.x);     // 10
trace(rect.left); // 10

See also
Rectangle.left

y Property

public y : Number

Player version: Flash Player 8

The y coordinate of the top-left corner of the rectangle. Changing the value of the y property of a Rectangle object has no effect on the x, width, and height properties.

The y property is equal to the top property.

Example
The following example creates an empty Rectangle and sets its y property to 10. Notice that rect.top is also changed.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle();
trace(rect.y);     // 0
trace(rect.top); // 0

rect.y = 10;
trace(rect.y);     // 10
trace(rect.top); // 10

See also
Rectangle.x, Rectangle.width, Rectangle.height, Rectangle.top


Constructor Detail

Rectangle Constructor

public Rectangle(x:Number, y:Number, width:Number, height:Number)

Player version: Flash Player 8

Creates a new Rectangle object whose top-left corner is specified by the x and y parameters. If you call this constructor function without parameters, a rectangle with x, y, width, and height properties set to 0 is created.

Parameters
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.
width:Number — The width of the rectangle in pixels.
height:Number — The height of the rectangle in pixels.

Example
The following example creates a Rectangle object with the specified parameters.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(5, 10, 50, 100);
trace(rect.toString()); // (x=5, y=10, w=50, h=100)

See also
Rectangle.x, Rectangle.y, Rectangle.width, Rectangle.height


Method Detail

clone Method

public clone() : Rectangle

Player version: Flash Player 8

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

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

Example
The following example creates three Rectangle objects and compares them. rect_1 is created using the Rectangle constructor. rect_2 is created by setting it equal to rect_1. And, clonedRect is created by cloning rect_1. Notice that while rect_2 evaluates as being equal to rect_1, clonedRect, even though it contains the same values as rect_1, does not.
import flash.geom.Rectangle;

var rect_1:Rectangle = new Rectangle(1, 2, 4, 8);
var rect_2:Rectangle = rect_1;
var clonedRect:Rectangle = rect_1.clone();

trace(rect_1 == rect_2);        // true
trace(rect_1 == clonedFilter);    // false

for(var i in rect_1) {
    trace(">> " + i + ": " + rect_1[i]);
    >> toString: [type Function]
    >> equals: [type Function]
    >> union: [type Function]
    >> intersects: [type Function]
    >> intersection: [type Function]
    >> containsRectangle: [type Function]
    >> containsPoint: [type Function]
    >> contains: [type Function]
    >> offsetPoint: [type Function]
    >> offset: [type Function]
    >> inflatePoint: [type Function]
    >> inflate: [type Function]
    >> size: (x=4, y=8)
    >> bottomRight: (x=5, y=10)
    >> topLeft: (x=1, y=2)
    >> bottom: 10
    >> top: 2
    >> right: 5
    >> left: 1
    >> isEmpty: [type Function]
    >> setEmpty: [type Function]
    >> clone: [type Function]
    >> height: 8
    >> width: 4
    >> y: 2
    >> x: 1
}

for(var i in clonedRect) {
    trace(">> " + i + ": " + clonedRect[i]);
    >> toString: [type Function]
    >> equals: [type Function]
    >> union: [type Function]
    >> intersects: [type Function]
    >> intersection: [type Function]
    >> containsRectangle: [type Function]
    >> containsPoint: [type Function]
    >> contains: [type Function]
    >> offsetPoint: [type Function]
    >> offset: [type Function]
    >> inflatePoint: [type Function]
    >> inflate: [type Function]
    >> size: (x=4, y=8)
    >> bottomRight: (x=5, y=10)
    >> topLeft: (x=1, y=2)
    >> bottom: 10
    >> top: 2
    >> right: 5
    >> left: 1
    >> isEmpty: [type Function]
    >> setEmpty: [type Function]
    >> clone: [type Function]
    >> height: 8
    >> width: 4
    >> y: 2
    >> x: 1
}
To further demonstrate the relationships between rect_1, rect_2, and clonedRect the example below modifies the x property of rect_1. Modifying x demonstrates that the clone() method creates a new instance based on values of the rect_1 instead of pointing to them in reference.
import flash.geom.Rectangle;

var rect_1:Rectangle = new Rectangle(1, 2, 4, 8);
var rect_2:Rectangle = rect_1;
var clonedRect:Rectangle = rect_1.clone();

trace(rect_1.x);            // 1
trace(rect_2.x);            // 1
trace(clonedRect.x);        // 1

rect_1.x = 10;

trace(rect_1.x);            // 10
trace(rect_2.x);            // 10
trace(clonedRect.x);        // 1

See also
Rectangle.x, Rectangle.y, Rectangle.width, Rectangle.height

contains Method

public contains(x:Number, y:Number) : Boolean

Player version: Flash Player 8

Determines whether the specified point is contained within the rectangular region defined by this Rectangle object.

Parameters
x:Number — The x-value (horizontal position) of the point.
y:Number — The y-value (vertical position) of the point.

Returns
Boolean — If the specified point is contained in the Rectangle object, returns true; otherwise false.

Example
The following example creates a Rectangle object and tests whether each of three coordinate pairs falls within its boundaries.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(10, 10, 50, 50);
trace(rect.contains(59, 59));    // true
trace(rect.contains(10, 10));    // true
trace(rect.contains(60, 60));    // false

See also
Point

containsPoint Method

public containsPoint(pt:Point) : Boolean

Player version: Flash Player 8

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
pt:Point — The point, as represented by its x,y values.

Returns
Boolean — If the specified point is contained within this Rectangle object, returns true; otherwise false.

Example
The following example creates a Rectangle object and three Point objects, and tests whether each of the points falls within the boundaries of the rectangle.
import flash.geom.Rectangle;
import flash.geom.Point;

var rect:Rectangle = new Rectangle(10, 10, 50, 50);
trace(rect.containsPoint(new Point(10, 10)));    // true
trace(rect.containsPoint(new Point(59, 59)));    // true
trace(rect.containsPoint(new Point(60, 60)));    // false

See also
Rectangle.contains, Point

containsRectangle Method

public containsRectangle(rect:Rectangle) : Boolean

Player version: Flash Player 8

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
rect:Rectangle — The Rectangle object being checked.

Returns
Boolean — If the Rectangle object that you specify is contained by this Rectangle object, returns true; otherwise false.

Example
The following example creates four new Rectangle objects and determines whether rectangle A contains rectangle B, C, or D.
import flash.geom.Rectangle;

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 Method

public equals(toCompare:Object) : Boolean

Player version: Flash Player 8

Determines whether the object specified in the toCompare parameter 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
toCompare:Object — The rectangle to compare to this Rectangle object.

Returns
Boolean — If the object has exactly the same values for the x, y, width, and height properties as this Rectangle object, returns true; otherwise false.

Example
In the following example, rect_1 and rect_2 are equal, but rect_3 is not equal to the other two objects because its x, y, width, and height properties are not equal to those of rect_1 and rect_2.
import flash.geom.Rectangle;

var rect_1:Rectangle = new Rectangle(0, 0, 50, 100);
var rect_2:Rectangle = new Rectangle(0, 0, 50, 100);
var rect_3:Rectangle = new Rectangle(10, 10, 60, 110);

trace(rect_1.equals(rect_2)); // true;
trace(rect_1.equals(rect_3)); // false;

Even though the method signature expects only an abstract object, only other Rectangle instances are treated as equal.
import flash.geom.Rectangle;

var rect_1:Rectangle = new Rectangle(0, 0, 50, 100);
var nonRect:Object = new Object();
nonRect.x = 0;
nonRect.y = 0;
nonRect.width = 50;
nonRect.height = 100;
trace(rect_1.equals(nonRect));

See also
Rectangle.x, Rectangle.y, Rectangle.width, Rectangle.height

inflate Method

public inflate(dx:Number, dy:Number) : Void

Player version: Flash Player 8

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.

Parameters
dx:Number — 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;
dy:Number — 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;

Example
The following example creates a Rectangle object and increases the value of its width property by 16 * 2 (32) and of its height property by 32 * 2 (64)
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(1, 2, 4, 8);
trace(rect.toString()); // (x=1, y=2, w=4, h=8)

rect.inflate(16, 32);
trace(rect.toString()); // (x=-15, y=-30, w=36, h=72)    

See also
Rectangle.x, Rectangle.y

inflatePoint Method

public inflatePoint(pt:Point) : Void

Player version: Flash Player 8

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.

The following two code examples give the same result:

rect1 = new flash.geom.Rectangle(0,0,2,5);
rect1.inflate(2,2)
rect1 = new flash.geom.Rectangle(0,0,2,5);
pt1 = new flash.geom.Point(2,2);
rect1.inflatePoint(pt1)

Parameters
pt:Point — Increases the rectangle by the x and y coordinate values of the point.

Example
The following example creates a Rectangle object and inflates it by the x (horizontal) and y (vertical) amounts found in a point.
import flash.geom.Rectangle;
import flash.geom.Point;

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)

See also
Point

intersection Method

public intersection(toIntersect:Rectangle) : Rectangle

Player version: Flash Player 8

If the Rectangle object specified in the toIntersect 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
toIntersect:Rectangle — The Rectangle object to compare against to see if it intersects with this Rectangle object.

Returns
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.

Example
The following example determines the area where rect_1 intersects rect_2.
import flash.geom.Rectangle;

var rect_1:Rectangle = new Rectangle(0, 0, 50, 50);
var rect_2:Rectangle = new Rectangle(25, 25, 100, 100);
var intersectingArea:Rectangle = rect_1.intersection(rect_2);
trace(intersectingArea.toString()); // (x=25, y=25, w=25, h=25)


intersects Method

public intersects(toIntersect:Rectangle) : Boolean

Player version: Flash Player 8

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
toIntersect:Rectangle — The Rectangle object to compare against this Rectangle object.

Returns
Boolean — If the specified object intersects with this Rectangle object, returns true; otherwise false.

Example
The following example determines whether rectA intersects with rectB or rectC.
import flash.geom.Rectangle;
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

See also
Rectangle.x, Rectangle.y, Rectangle.width, Rectangle.height

isEmpty Method

public isEmpty() : Boolean

Player version: Flash Player 8

Determines whether or not this Rectangle object is empty.

Returns
Boolean — If the Rectangle object's width or height is less than or equal to 0, returns true; otherwise false.

Example
The following example creates an empty Rectangle object and verifies that it is empty.
import flash.geom.*;
var rect:Rectangle = new Rectangle(1, 2, 0, 0);
trace(rect.toString()); // (x=1, y=2, w=0, h=0)
trace(rect.isEmpty()); // true

The following example creates a non-empty Rectangle and makes it become empty.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(1, 2, 4, 8);
trace(rect.isEmpty()); // false
rect.width = 0;
trace(rect.isEmpty()); // true
rect.width = 4;
trace(rect.isEmpty()); // false
rect.height = 0;
trace(rect.isEmpty()); // true


offset Method

public offset(dx:Number, dy:Number) : Void

Player version: Flash Player 8

Adjusts the location of the Rectangle object, as determined by its top-left corner, by the specified amounts.

Parameters
dx:Number — Moves the x value of the Rectangle object by this amount.
dy:Number — Moves the y value of the Rectangle object by this amount.

Example
The following example creates a Rectangle object and offsets its x and y values by 5 and 10 respectively
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(1, 2, 4, 8);
trace(rect.toString()); // (x=1, y=2, w=4, h=8)

rect.offset(16, 32);
trace(rect.toString()); // (x=17, y=34, w=4, h=8)


offsetPoint Method

public offsetPoint(pt:Point) : Void

Player version: Flash Player 8

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
pt:Point — A Point object to use to offset this Rectangle object.

Example
The following example offsets a Rectangle by using the values found in a point.
import flash.geom.Rectangle;
import flash.geom.Point;
    
var rect:Rectangle = new Rectangle(1, 2, 4, 8);
trace(rect.toString()); // (x=1, y=2, w=4, h=8)

var myPoint:Point = new Point(16, 32);
rect.offsetPoint(myPoint);
trace(rect.toString()); // (x=17, y=34, w=4, h=8)

See also
Point

setEmpty Method

public setEmpty() : Void

Player version: Flash Player 8

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.

Example
The following example creates a non-empty Rectangle object and makes it empty.
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(5, 10, 50, 100);
trace(rect.isEmpty()); // false
rect.setEmpty();
trace(rect.isEmpty()); // true

See also
Rectangle.x, Rectangle.y, Rectangle.width, Rectangle.height

toString Method

public toString() : String

Player version: Flash Player 8

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

Returns
String — A string that lists the value of each of the following properties of the Rectangle object: x, y, width, and height.

Example
The following example concatenates a string representation of rect_1 with some helpful debugging text.
import flash.geom.Rectangle;

var rect_1:Rectangle = new Rectangle(0, 0, 50, 100);
trace("Rectangle 1 : " + rect_1.toString()); // Rectangle 1 : (x=0, y=0, w=50, h=100)

See also
Rectangle.x, Rectangle.y, Rectangle.width, Rectangle.height

union Method

public union(toUnion:Rectangle) : Rectangle

Player version: Flash Player 8

Adds two rectangles together to create a new Rectangle object, by filling in the horizontal and vertical space between the two rectangles.

Parameters
toUnion:Rectangle — A Rectangle object to add to this Rectangle object.

Returns
Rectangle — A new Rectangle object that is the union of the two rectangles.

Example
The following example creates a Rectangle object out of the union of two others.

For example, consider a rectangle with properties x=20, y=50, width=60, and height=30 (20, 50, 60, 30), and a second rectangle with properties (150, 130, 50, 30). The union of these two rectangles is a larger rectangle that encompasses the two rectangles with the properties (20, 50, 180, 110).

import flash.geom.Rectangle;

var rect_1:Rectangle = new Rectangle(20, 50, 60, 30);
var rect_2:Rectangle = new Rectangle(150, 130, 50, 30);
var combined:Rectangle = rect_1.union(rect_2);
trace(combined.toString()); // (x=20, y=50, w=180, h=110)