Inherits from NSObject
Conforms to NSCopying
PXPooledObject
Declared in PXRectangle.h
PXRectangle.m

Overview

A PXRectangle object is an area defined by its position, as indicated by its top-left corner (x, y) and by its width and its height.

The x, y, width and height properties of the PXRectangle 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.

The following code creates a rectangle at (0, 0) with a size of (0, 0):

PXRectangle *rect = [PXRectangle new];

The following code creates a rectangle at (10, -7) with a size of (20, 10):

PXRectangle *rect = [[PXRectangle alloc] initWithX:10 y:-7 width:20 height:10];

Tasks

Properties

bottom

The bottomRight corner’s vertical coordinate.

@property (nonatomic, assign) float bottom

Declared In

PXRectangle.h

bottomRight

A point with the values of the rectangle’s bottom-right corner cordinates.

@property (nonatomic, assign) PXPoint *bottomRight

Declared In

PXRectangle.h

height

The size of the rectangle along the y-acis.

@property (nonatomic, assign) float height

Declared In

PXRectangle.h

left

The left corner’s horizontal coordinate.

@property (nonatomic, assign) float left

Declared In

PXRectangle.h

right

The right corner’s horizontal coordinate.

@property (nonatomic, assign) float right

Declared In

PXRectangle.h

size

A point with the values of the width and height properties.

@property (nonatomic, assign) PXPoint *size

Declared In

PXRectangle.h

top

The topLeft corner’s vertical coordinate.

@property (nonatomic, assign) float top

Declared In

PXRectangle.h

topLeft

A point with the values of the rectangle’s top-left corner cordinates.

@property (nonatomic, assign) PXPoint *topLeft

Declared In

PXRectangle.h

width

The size of the rectangle along the x-axis.

@property (nonatomic, assign) float width

Declared In

PXRectangle.h

x

The topLeft corner’s horizontal coordinate.

@property (nonatomic, assign) float x

Declared In

PXRectangle.h

y

The topLeft corner’s vertical coordinate.

@property (nonatomic, assign) float y

Declared In

PXRectangle.h

Class Methods

rectangleWithX:y:width:height:

Creates a rectangle with topLeft corner at (x, y) and size of (width, height).

+ (PXRectangle *)rectangleWithX:(float)x y:(float)y width:(float)width height:(float)height

Parameters

x

The horizontal coordinate of the topLeft corner.

y

The vertical coordinate of the topLeft corner.

width

The width of the rectangle.

height

The height of the rectangle.

Return Value

The created rectangle.

Example:

PXRectangle *rect = [PXRectangle rectangleWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).

Declared In

PXRectangle.m

Instance Methods

containsPoint:

Determines whether the specified point is contained within the rectangle’s area.

- (BOOL)containsPoint:(PXPoint *)point

Parameters

point

The point to test.

Return Value

YES if the rectangle contains the point, otherwise NO.

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).
PXPoint *point = [[PXPoint alloc] initWithX:3 y:8];
BOOL isContained = [rect containsPoint:point];
// isContained is YES

Declared In

PXRectangle.m

containsRect:

Determines whether the entire specified rectangle is contained within this rectangle’s area.

- (BOOL)containsRect:(PXRectangle *)rect

Parameters

rect

The rectangle to test.

Return Value

YES if this rectangle contains the rectangle provided, otherwise NO.

Example:

PXRectangle *rect1 = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// rect1 will have its top-left will be (-5, 7) size will be (10, 4).
PXRectangle *rect2 = [[PXRectangle alloc] initWithX:-3 y:8 width:4 height:2];
// rect2 will have its top-left will be (-3, 8) size will be (4, 2).
BOOL isContained = [rect1 containsRect:rect2];
// isContained is YES
isContained = [rect2 containsRect:rect1];
// isContained is NO

Declared In

PXRectangle.m

containsX:y:

Determines whether the specified point is contained within the rectangle’s area.

- (BOOL)containsX:(float)_x y:(float)_y

Parameters

x

The horizontal coordinate of the point.

y

The vertical coordinate of the point.

Return Value

YES if the rectangle contains the point, otherwise NO.

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).
BOOL isContained = [rect containsX:3 y:8];
// isContained is YES

Declared In

PXRectangle.m

inflateWithPoint:

Increases the size of the rectangle by specified amounts from the center.

- (void)inflateWithPoint:(PXPoint *)point

Parameters

point

The size change.

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).
PXPoint *point = [[PXPoint alloc] initWithX:1 y:0.5f];
[rect inflateWithPoint:point];
// Top-left will be (-6, 6.5) size will be (12, 5).

Declared In

PXRectangle.m

inflateWithX:y:

Increases the size of the rectangle by specified amounts from the center.

- (void)inflateWithX:(float)dx y:(float)dy

Parameters

dx

The size change in the horizontal position.

dy

The size change in the vertical position.

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).
[rect inflateWithX:1 y:0.5f];
// Top-left will be (-6, 6.5) size will be (12, 5).

Declared In

PXRectangle.m

initWithX:y:width:height:

Creates a new rectangle with topLeft corner at (x, y) and size of (width, height).

- (id)initWithX:(float)_x y:(float)_y width:(float)_width height:(float)_height

Parameters

y

The vertical coordinate of the topLeft corner.

x

The horizontal coordinate of the topLeft corner.

width

The width of the rectangle.

height

The height of the rectangle.

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).

Declared In

PXRectangle.m

intersectionWithRect:

If the rectangle specified interesects with this rectangle, then the interesection of the two rectangles is returned as a rectangle. Otherwise an empty rectangle is returned.

- (PXRectangle *)intersectionWithRect:(PXRectangle *)toIntersect

Parameters

toIntersect

The rectangle to compare.

Return Value

A rectangle defining the intersection of the rectangle specified, and this rectangle. It’s empty if no interesection was found.

Example:

PXRectangle *rect1 = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// rect1 will have its top-left will be (-5, 7) size will be (10, 4).
PXRectangle *rect2 = [[PXRectangle alloc] initWithX:-7 y:3 width:5 height:8];
// rect2 will have its top-left will be (-7, 3) size will be (5, 8).
PXRectangle *intersection = [rect1 intersectionWithRect:rect2];
// intersection will have its top-left will be (-5, 7) size will be (3, 4).

Declared In

PXRectangle.m

intersectsWithRect:

Determines if the rectangle specified intersects with this rectangle.

- (BOOL)intersectsWithRect:(PXRectangle *)toIntersect

Parameters

toIntersect

The rectangle to compare.

Return Value

YES if this rectangle intersects with the rectangle specified; otherwise NO.

Example:

PXRectangle *rect1 = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// rect1 will have its top-left will be (-5, 7) size will be (10, 4).
PXRectangle *rect2 = [[PXRectangle alloc] initWithX:-7 y:3 width:5 height:8];
// rect2 will have its top-left will be (-7, 3) size will be (5, 8).
BOOL intersects = [rect1 intersectsWithRect:rect2];
// intersects is YES.

Declared In

PXRectangle.m

isEmpty

Determines if the rectangle has an area of 0.

- (BOOL)isEmpty

Return Value

YES if the rectangle has an area of 0; otherwise NO.

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
BOOL emptyRect = [rect isEmpty];
// emptyRect is NO.
rect.width = 0;
emptyRect = [rect isEmpty];
// emptyRect is YES.

Declared In

PXRectangle.m

isEqualToRect:

Determines whether the rectangle specified is equal to this rectangle. This is only true if the x, y, width and height properties are the same.

- (BOOL)isEqualToRect:(PXRectangle *)rectangle

Parameters

rect

The rectangle to compare.

Return Value

YES if the rectangle specified is equal to this rectangle; otherwise NO.

Example:

PXRectangle *rect1 = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// rect1 will have its top-left will be (-5, 7) size will be (10, 4).
PXRectangle *rect2 = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// rect2 will have its top-left will be (-5, 7) size will be (10, 4).
BOOL isEqual = [rect1 isEqualToRect:rect2];
// isEqual is YES

Declared In

PXRectangle.m

offsetWithPoint:

Adjusts the location of the rectangle.

- (void)offsetWithPoint:(PXPoint *)point

Parameters

point

The change in position.

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).
PXPoint *point = [[PXPoint alloc] initWithX:4 y:-6];
[rect offsetWithPoint:point];
// Top-left will be (-1, 1) size will be (10, 4).

Declared In

PXRectangle.m

offsetWithX:y:

Adjusts the location of the rectangle.

- (void)offsetWithX:(float)dx y:(float)dy

Parameters

dx

The horizontal change in position.

dy

The vertical change in position.

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).
[rect offsetWithX:4 y:-6];
// Top-left will be (-1, 1) size will be (10, 4).

Declared In

PXRectangle.m

setEmpty

Sets all of the rectangle’s properties to 0.

- (void)setEmpty

Discussion

Example:

PXRectangle *rect = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).
[rect setEmpty];
// Top-left will be (0, 0) size will be (0, 0).

Declared In

PXRectangle.m

setX:y:width:height:

Sets the rectangle’s topLeft corner to (x, y) and size of (width, height).

- (void)setX:(float)_x y:(float)_y width:(float)_width height:(float)_height

Parameters

y

The vertical coordinate of the topLeft corner.

x

The horizontal coordinate of the topLeft corner.

width

The width of the rectangle.

height

The height of the rectangle.

Example:

PXRectangle *rect = [PXRectangle new];
// Top-left will be (0, 0) size will be (0, 0).
[rect setX:-5 y:7 width:10 height:4];
// Top-left will be (-5, 7) size will be (10, 4).

Declared In

PXRectangle.m

unionWithRect:

Adds two rectangles together to create a rectangle with their combined properties.

- (PXRectangle *)unionWithRect:(PXRectangle *)toUnion

Parameters

rect

Rectangle to union with.

Return Value

The combined rectangle.

Example:

PXRectangle *rect1 = [[PXRectangle alloc] initWithX:-5 y:7 width:10 height:4];
// rect1 will have its top-left will be (-5, 7) size will be (10, 4).
PXRectangle *rect2 = [[PXRectangle alloc] initWithX:-7 y:3 width:5 height:8];
// rect2 will have its top-left will be (-7, 3) size will be (5, 8).
PXRectangle *unionRect = [rect1 unionWithRect:rect2];
// unionRect will have its top-left will be (-7, 3) size will be (12, 8).

Declared In

PXRectangle.m