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

Overview

A PXPoint object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.

Tasks

Properties

length

The length of the line segment from (0, 0) to this point.

@property (nonatomic, readonly) float length

Declared In

PXPoint.h

x

The horizontal coordinate.

@property (nonatomic, assign) float x

Declared In

PXPoint.h

y

The vertical coordinate.

@property (nonatomic, assign) float y

Declared In

PXPoint.h

Class Methods

angleBetweenPointA:pointB:

Returns the angle between pt1 and pt2.

+ (float)angleBetweenPointA:(PXPoint *)pt1 pointB:(PXPoint *)pt2

Parameters

pt1

The first point.

pt2

The second point.

Return Value

The angle between the first and second degrees.

Example:

PXPoint *pt1 = [[PXPoint alloc] initWithX:0.0f y:0.0f];
PXPoint *pt2 = [[PXPoint alloc] initWithX:5.0f y:-5.0f];
float angle = [PXPoint angleBetweenPointA:pt1 pointB:pt2];
// angle will be 45.0f

Declared In

PXPoint.m

distanceBetweenPointA:pointB:

Returns the distance between pt1 and pt2.

+ (float)distanceBetweenPointA:(PXPoint *)pt1 pointB:(PXPoint *)pt2

Parameters

pt1

The first point.

pt2

The second point.

Return Value

The distance between the first and second points.

Example:

PXPoint *pt1 = [[PXPoint alloc] initWithX:3.0f y:4.0f];
PXPoint *pt2 = [[PXPoint alloc] initWithX:-3.0f y:-4.0f];
float dist = [PXPoint distanceBetweenPointA:pt1 pointB:pt2];
// dist will be 10.0f

Declared In

PXPoint.m

pointByInterpolatingBetweenPointA:pointB:withCoefficientOfInterpolation:

Determines a point between two specified points. The parameter f determines where the new interpolated point is located relative to the two end points specified by parameters pt1 and pt2.

+ (PXPoint *)pointByInterpolatingBetweenPointA:(PXPoint *)pt1 pointB:(PXPoint *)pt2 withCoefficientOfInterpolation:(float)f

Parameters

pt1

The first point.

pt2

The second point.

f

The level of interpolation between the two points. Indicates where the new point will be, along the line between pt1 and pt2. If f == 1, pt1 is returned; if == 0, pt2 is returned.

Return Value

The interpolated point.

Example:

PXPoint *pt1 = [[PXPoint alloc] initWithX:3.0f y:4.0f];
PXPoint *pt2 = [[PXPoint alloc] initWithX:-3.0f y:-4.0f];
PXPoint *pt3 = [PXPoint pointByInterpolatingBetweenPointA:pt1 pointB:pt2 withCoefficientOfInterpolation:0.3f];
// pt3 will be (-1.2f, -1.6f)

Discussion

The closer the value of the parameter f is to 1.0, the closer the interpolated point is to the first point (parameter pt1).

Declared In

PXPoint.m

pointUsingPolarCoordWithLen:angle:

Converts a pair of polar coordinates to a cartesian point coordinate.

+ (PXPoint *)pointUsingPolarCoordWithLen:(float)len angle:(float)angle

Parameters

len

The length coordinate of the polar pair.

angle

The angle, in radians, of the polar pair.

Return Value

The cartesian point.

Example:

float angle = 45.0f * (M_PI/180.0f);
float length = 1.0f;
PXPoint *point = [PXPoint pointUsingPolarCoordWithLen:length angle:angle];
// point will be (0.707107f, 0.707107f)

Declared In

PXPoint.m

pointWithX:y:

Creates a point at (x, y).

+ (PXPoint *)pointWithX:(float)x y:(float)y

Parameters

x

The horizontal coordinate.

y

The vertical coordinate.

Return Value

The created point.

Example:

PXPoint *point = [PXPoint pointWithX:4 y:5];
// point will be (4, 5)

Declared In

PXPoint.m

Instance Methods

addPoint:

Adds the coordinates of the given point to the coordinates of this point to create a new point.

- (PXPoint *)addPoint:(PXPoint *)point

Parameters

point

The point to be added.

Return Value

The created point.

Example:

PXPoint *pt1 = [[PXPoint alloc] initWithX:5 y:7];
PXPoint *pt2 = [[PXPoint alloc] initWithX:-3 y:10];
PXPoint *pt3 = [pt1 addPoint:pt2];
// pt3 will be (2, 17)
PXPoint *pt4 = [pt2 addPoint:pt3];
// pt4 will be (-1, 27)

Declared In

PXPoint.m

initWithX:y:

Creates a new point at (x, y).

- (id)initWithX:(float)_x y:(float)_y

Parameters

x

The horizontal coordinate.

y

The vertical coordinate.

Example:

PXPoint *point = [[PXPoint alloc] initWithX:5 y:7];
// point will be (5,7)

Declared In

PXPoint.m

isEqualToPoint:

Determines whether two points are equal. Two points are equal if they have the same x and y values.

- (BOOL)isEqualToPoint:(PXPoint *)point

Parameters

point

The point to be compared.

Return Value

YES if the object is equal to this point object; NO if it is not equal.

Example:

PXPoint *pt1 = [[PXPoint alloc] initWithX:5.0f y:7.0f];
PXPoint *pt2 = [[PXPoint alloc] initWithX:-3.0f y:10.0f];
PXPoint *pt3 = [[PXPoint alloc] initWithX:-3.0f y:10.0f];
BOOL isEqual = [pt1 isEqualToPoint:pt2];
// will result in NO
isEqual = [pt2 isEqualToPoint:pt3];
// will result in YES

Declared In

PXPoint.m

normalize

Sets the vector represented by this point to unit length.

- (void)normalize

Discussion

Example:

PXPoint *point = [[PXPoint alloc] initWithX:3.0f y:4.0f];
[point normalize];
// point will now be (0.6f, 0.8f)

Declared In

PXPoint.m

normalizeWithLength:

Scales the line segment between (0, 0) and the current point to a set length.

- (void)normalizeWithLength:(float)newLength

Parameters

newLength

The scaling value.

Example:

PXPoint *point = [[PXPoint alloc] initWithX:3.0f y:4.0f];
[point normalizeWithLength:10.0f];
// point will now be (6, 8)

Declared In

PXPoint.m

offsetWithX:y:

Offsets the PXPoint object by the specified amount. The value of dx is added to the original value of x to create the new x value. The value of dy is added to the original value of y to create the new y value.

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

Parameters

dx

The amount by which to offset the horizontal coordinate, x

dy

The amount by which to offset the vertical coordinate, y.

Example:

PXPoint *point = [[PXPoint alloc] initWithX:3.0f y:4.0f];
[point offsetWithX:11.0f y:-5.0f];
// point will now be (14.0f, -1.0f)

Declared In

PXPoint.m

setX:y:

Sets the point to (x, y).

- (void)setX:(float)_x y:(float)_y

Parameters

x

The horizontal coordinate.

y

The vertical coordinate.

Example:

PXPoint *point = [PXPoint new];
// point will be (0, 0)
[point setX:5 y:7];
// point will now be (5, 7)

Declared In

PXPoint.m

subtractPoint:

Subtracts the coordinates of the given point from the coordinates of this point to create a new point.

- (PXPoint *)subtractPoint:(PXPoint *)point

Parameters

point

The point to be subtracted.

Return Value

The created point.

Example:

PXPoint *pt1 = [[PXPoint alloc] initWithX:5 y:7];
PXPoint *pt2 = [[PXPoint alloc] initWithX:-3 y:10];
PXPoint *pt3 = [pt1 subtractPoint:pt2];
// pt3 will be (8, -3)
PXPoint *pt4 = [pt2 subtractPoint:pt3];
// pt3 will now be (-11, 13)

Declared In

PXPoint.m