[Prev: Point] [Home] [Next: Size]

Rect

A Rect object represents a rectangle.

The rectangle class provides three types of constructors as shown below.

    var rect = new Rect( 10, 10, 10, 10 ); // x=10, y=10, width=10, height=10
    var copy = new Rect( rect ); // x=10, y=10, width=10, height=10
    var nullRect = new Rect(); // 0, 0, 0, 0
Rect Properties
x

Describes the left position of the rectangle.

y

Describes the right position of the rectangle.

width

Describes the width of the rectangle.

height

Describes the height of the rectangle.

top

Describes the position of the top of the rectangle. This is defined as top = y.

bottom

Describes the position of the bottom of the rectangle. This is defined as bottom = y + height + 1.

left

Describes the position of the rectangle left side. This is defined as left = x

right

Describes the position of the rectangle right side. This is defined as right = x + width + 1.

center

Describes the center of the rectangle.

Rect Functions
isNull

isNull()

    var empty = new Rect();
    empty.isNull(); // true;

    var square = new Rect( 10, 10, 10, 10 );
    square.isNull(); // false;

Returns true if the rectangle has a width and height of 0.

isEmpty()

isEmpty()

    var rect = new Rect( 10, 10, -10, -10 );
    rect.isEmpty(); // true
    
    var rect = new Rect( 10, 10, 10, 10 );
    rect.isEmpty(); // false

Returns true if the rectangle is empty, meaning that it has width and/or height that is negative.

vontains

contains( otherRect )

    new Rect( 0, 0, 100, 100 ).contains( new Rect( 10, 10, 10, 10 ) ); // true
    new rect( 10, 10, 10, 10 ).contains( new Rect( 0, 0, 100, 100 ) ); // false

Returns true if the rectangle contains another rectangle.

intersection

intersection( otherRect )

Returns the intersection between this rectangle and another rectangle. The intersection of two rectangles is the part of the rectangles that overlap. If they do not overlap, an empty rectangle is returned.

union

union( otherRect )

Returns the union of two rectangles. The union is a rectangle large enough to encapsulate both rectangles.

intersects

intersects( otherRect )

Returns true if this rectangle intersects the input rectangle.

normalize

normalize()

Normalizes this rectangle. This means changing the prefix of the width and/or height if they are negative. After being normalized, a rectangle will no longer be empty.

moveLeft

moveLeft( pos )

Moves the rectangle so that its left property will be equal to pos.

moveRight

moveRight( pos )

Moves the rectangle so that its right property will be equal to pos.

moveTop

moveTop( pos )

Moves the rectangle so that its top property will be equal to pos.

moveBottom

moveBottom( pos )

Moves the rectangle so that its bottom property will be equal to pos.

moveBy

moveBy( dx, dy )

Translates the rectangle by dx and dy. dx and dy will be added to x and y, and width and height will be left unchanged.

[Prev: Point] [Home] [Next: Size]