Rectangle
| Kind of class: | class |
|---|---|
| Inherits from: | none |
| Author: | Arthur Clemens |
| Classpath: | playground.classes.util.types.Rectangle |
| File last modified: | Sunday, 15 January 2006, 00:55:19 |
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
Class methods
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.
center
center:Point
(read,write)
The center Point of the rectangle relative to its parent.
This property is not included in the Flash 8 Rectangle class.
This property is not included in the Flash 8 Rectangle class.
Example:
var rectangle:Rectangle = new Rectangle(1,2,4,8); // the rectangle has width:4 and height:8 var center.Point = rectangle.center; trace(center); // (x=3, y=6)
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.
right
right:Number
(read,write)
The sum of the x and width properties.
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.
Class methods
boundsOfMovieClip
static function boundsOfMovieClip (
inMC:MovieClip,
inTargetSpace:MovieClip) : Rectangle
Deprecated As of Flash 8: use
var trans:Transform = new Transform(my_mc).pixelBounds to get the Rectangle bounds of a movieclip.This method is not included in the Flash 8 Rectangle class.
Parameters:
inMc :
the MovieClip to be measured
inTargetSpace:
the target path of the Timeline whose coordinate system you want to use as a reference point; if null,
_level0 is assumedReturns:
A new Rectangle that has the size of the MovieClip and the position of the clip on the Stage.
Implementation note:
Calls MovieClip.getBounds.
Usage note:
A newly created movieclip without any contents has these bounds: (x=6710886.35, y=6710886.35, w=0, h=0)
centerPointOfMovieClip
static function centerPointOfMovieClip (
inMC:MovieClip,
inTargetSpace:MovieClip) : Point
Returns the center point of the MovieClip's bounds.
This method is not included in the Flash 8 Rectangle class.
This method is not included in the Flash 8 Rectangle class.
Parameters:
inMc :
the MovieClip to be measured
inTargetSpace:
the target path of the Timeline whose coordinate system you want to use as a reference point; if null,
_level0 is assumed; pass the movieclip to get the center of its contentsReturns:
A new Point that has the position of the center of the MovieClip bounds. Returns Point(0,0) if the clip bounding rectangle has no width or heigth.
Implementation note:
Calls boundsOfMovieClip.
Usage note:
A newly created movieclip without any contents has these bounds: (x=6710886.35, y=6710886.35, w=0, h=0); the center of this movieclip will be returned as Point (0,0)
To get the relative center point (the center of a movieclip's contents), write
To get the relative center point (the center of a movieclip's contents), write
centerPointOfMovieClip(my_mc, my_mc)Example:
var mc:MovieClip = _level0.createEmptyMovieClip("box", 1); var size:Number = 100; with (mc) { lineStyle( 0, 0x000000, 100 ); moveTo( 0, 0 ); lineTo( 0, size ); lineTo( size, size ); lineTo( size, 0 ); lineTo( 0, 0 ); } mc._x = 300; mc._y = 200; trace(Rectangle.centerPointOfMovieClip(mc); // ((x=350, y=250) trace(Rectangle.centerPointOfMovieClip(mc,mc); // (x=50, y=50)
rectOfMovieClip
static function rectOfMovieClip (
inMC:MovieClip) : Rectangle
Returns the rectangle of the MovieClip's unscaled contents as a new Rectangle. When a movieclip is scaled, this method returns the unscaled clip sizes.
This method is not included in the Flash 8 Rectangle class.
This method is not included in the Flash 8 Rectangle class.
Parameters:
inMc:
the MovieClip to be measured
See also:
Returns:
A new Rectangle that has the size of the MovieClip and the position of its contents. Returns an empty Rectangle when an empty movieclip without any contents is passed.
Implementation note:
Calls boundsOfMovieClip with (inMC, inMC)
Instance methods
clone
function clone (
) : Rectangle
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.
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:
increases 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)
intersection
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
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.