PXRectangle Class Reference
| 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
-
xThe
propertytopLeftcorner’s horizontal coordinate. -
yThe
propertytopLeftcorner’s vertical coordinate. -
widthThe size of the rectangle along the x-axis.
property -
heightThe size of the rectangle along the y-acis.
property -
topThe
propertytopLeftcorner’s vertical coordinate. -
bottomThe
propertybottomRightcorner’s vertical coordinate. -
leftThe
propertyleftcorner’s horizontal coordinate. -
rightThe
propertyrightcorner’s horizontal coordinate. -
sizeA point with the values of the
propertywidthandheightproperties. -
bottomRightA point with the values of the rectangle’s bottom-right corner cordinates.
property -
topLeftA point with the values of the rectangle’s top-left corner cordinates.
property -
– initWithX:y:width:height:Creates a new rectangle with
topLeftcorner at (x, y) and size of (width, height). -
– setX:y:width:height:Sets the rectangle’s
topLeftcorner to (x, y) and size of (width, height). -
– containsX:y:Determines whether the specified point is contained within the rectangle’s area.
-
– containsPoint:Determines whether the specified point is contained within the rectangle’s area.
-
– containsRect:Determines whether the entire specified rectangle is contained within this rectangle’s area.
-
– 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.
-
– isEmptyDetermines if the rectangle has an area of 0.
-
– inflateWithX:y:Increases the size of the rectangle by specified amounts from the center.
-
– inflateWithPoint:Increases the size of the rectangle by specified amounts from the center.
-
– 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.
-
– intersectsWithRect:Determines if the rectangle specified intersects with this rectangle.
-
– offsetWithX:y:Adjusts the location of the rectangle.
-
– offsetWithPoint:Adjusts the location of the rectangle.
-
– setEmptySets all of the rectangle’s properties to 0.
-
– unionWithRect:Adds two rectangles together to create a rectangle with their combined properties.
-
+ rectangleWithX:y:width:height:Creates a rectangle with
topLeftcorner at (x, y) and size of (width, height).
Properties
bottom
The bottomRight corner’s vertical coordinate.
@property (nonatomic, assign) float bottomDeclared In
PXRectangle.hbottomRight
A point with the values of the rectangle’s bottom-right corner cordinates.
@property (nonatomic, assign) PXPoint *bottomRightDeclared In
PXRectangle.hheight
The size of the rectangle along the y-acis.
@property (nonatomic, assign) float heightDeclared In
PXRectangle.hleft
The left corner’s horizontal coordinate.
@property (nonatomic, assign) float leftDeclared In
PXRectangle.hright
The right corner’s horizontal coordinate.
@property (nonatomic, assign) float rightDeclared In
PXRectangle.hsize
A point with the values of the width and height
properties.
@property (nonatomic, assign) PXPoint *sizeDeclared In
PXRectangle.htop
The topLeft corner’s vertical coordinate.
@property (nonatomic, assign) float topDeclared In
PXRectangle.htopLeft
A point with the values of the rectangle’s top-left corner cordinates.
@property (nonatomic, assign) PXPoint *topLeftDeclared In
PXRectangle.hwidth
The size of the rectangle along the x-axis.
@property (nonatomic, assign) float widthDeclared In
PXRectangle.hClass Methods
rectangleWithX:y:width:height:
+ (PXRectangle *)rectangleWithX:(float)x y:(float)y width:(float)width height:(float)heightParameters
- x
The horizontal coordinate of the
topLeftcorner.
- y
The vertical coordinate of the
topLeftcorner.
- 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.mInstance Methods
containsPoint:
Determines whether the specified point is contained within the rectangle’s area.
- (BOOL)containsPoint:(PXPoint *)pointParameters
- 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.mcontainsRect:
Determines whether the entire specified rectangle is contained within this rectangle’s area.
- (BOOL)containsRect:(PXRectangle *)rectParameters
- 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.mcontainsX:y:
Determines whether the specified point is contained within the rectangle’s area.
- (BOOL)containsX:(float)_x y:(float)_yParameters
- 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.minflateWithPoint:
Increases the size of the rectangle by specified amounts from the center.
- (void)inflateWithPoint:(PXPoint *)pointParameters
- 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.minflateWithX:y:
Increases the size of the rectangle by specified amounts from the center.
- (void)inflateWithX:(float)dx y:(float)dyParameters
- 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.minitWithX:y:width:height:
- (id)initWithX:(float)_x y:(float)_y width:(float)_width height:(float)_heightParameters
- y
The vertical coordinate of the
topLeftcorner.
- x
The horizontal coordinate of the
topLeftcorner.
- 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.mintersectionWithRect:
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 *)toIntersectParameters
- 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.mintersectsWithRect:
Determines if the rectangle specified intersects with this rectangle.
- (BOOL)intersectsWithRect:(PXRectangle *)toIntersectParameters
- 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.misEmpty
Determines if the rectangle has an area of 0.
- (BOOL)isEmptyReturn 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.misEqualToRect:
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 *)rectangleParameters
- 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.moffsetWithPoint:
Adjusts the location of the rectangle.
- (void)offsetWithPoint:(PXPoint *)pointParameters
- 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.moffsetWithX:y:
Adjusts the location of the rectangle.
- (void)offsetWithX:(float)dx y:(float)dyParameters
- 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.msetEmpty
Sets all of the rectangle’s properties to 0.
- (void)setEmptyDiscussion
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.msetX:y:width:height:
- (void)setX:(float)_x y:(float)_y width:(float)_width height:(float)_heightParameters
- y
The vertical coordinate of the
topLeftcorner.
- x
The horizontal coordinate of the
topLeftcorner.
- 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.munionWithRect:
Adds two rectangles together to create a rectangle with their combined properties.
- (PXRectangle *)unionWithRect:(PXRectangle *)toUnionParameters
- 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