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

Overview

A vector (or point) in a three-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis and z represents the depth axis.

Tasks

Properties

length

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

@property (nonatomic, readonly) float length

Declared In

PXVector3D.h

lengthSquared

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

@property (nonatomic, readonly) float lengthSquared

Declared In

PXVector3D.h

w

The angle of rotation.

@property (nonatomic, assign) float w

Declared In

PXVector3D.h

x

The horizontal coordinate.

@property (nonatomic, assign) float x

Declared In

PXVector3D.h

y

The vertical coordinate.

@property (nonatomic, assign) float y

Declared In

PXVector3D.h

z

The depth coordinate.

@property (nonatomic, assign) float z

Declared In

PXVector3D.h

Class Methods

angleBetweenVectorA:vectorB:

Calculates the angle (in radians) between the two given vectors.

+ (float)angleBetweenVectorA:(PXVector3D *)vectorA vectorB:(PXVector3D *)vectorB

Parameters

vectorA

The first vector.

vectorB

The second vector.

Return Value

The angle in radians between the two vectors.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:3.0f y:4.0f z:5.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:-4.0f y:3.0f z:5.0f];
float angleInRadians = [PXVector3D angleBetweenVectorA:vec1 vectorB:vec2];
// in degrees the angle is 60.0f

Declared In

PXVector3D.m

distanceBetweenVectorA:vectorB:

Calculates the distance between the two given vectors.

+ (float)distanceBetweenVectorA:(PXVector3D *)vectorA vectorB:(PXVector3D *)vectorB

Parameters

vectorA

The first vector.

vectorB

The second vector.

Return Value

The distance between the two vectors.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:3.0f y:4.0f z:5.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:-4.0f y:3.0f z:5.0f];
float distance = [PXVector3D distanceBetweenVectorA:vec1 vectorB:vec2];
// The distance is 7.071068f

Declared In

PXVector3D.m

distanceSquaredBetweenVectorA:vectorB:

Calculates the squared distance between the two given vectors.

+ (float)distanceSquaredBetweenVectorA:(PXVector3D *)vectorA vectorB:(PXVector3D *)vectorB

Parameters

vectorA

The first vector.

vectorB

The second vector.

Return Value

The squared distance between the two vectors.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:3.0f y:4.0f z:5.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:-4.0f y:3.0f z:5.0f];
float distance = [PXVector3D distanceSquaredBetweenVectorA:vec1 vectorB:vec2];
// The squared distance is 50.0f

Declared In

PXVector3D.m

vector3DWithX:y:z:

Creates a vector at (x, y, z).

+ (PXVector3D *)vector3DWithX:(float)x y:(float)y z:(float)z

Parameters

x

The horizontal coordinate.

y

The vertical coordinate.

z

The depth coordinate.

Return Value

The created vector.

Example:

PXVector3D *vector = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f];
// vector will be (5,7,4)

Declared In

PXVector3D.m

vector3DWithX:y:z:w:

Creates a new vector at (x, y, z) with rotation w.

+ (PXVector3D *)vector3DWithX:(float)x y:(float)y z:(float)z w:(float)w

Parameters

x

The horizontal coordinate.

y

The vertical coordinate.

z

The depth coordinate.

w

The angle of rotation.

Return Value

The created vector.

Example:

PXVector3D *vector = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f w:0.0f];
// vector will be (5,7,4) with angle of rotation 0

Declared In

PXVector3D.m

Instance Methods

addVector:

Adds the values of the given vector to the corresponding values of this vector to create a new vector.

- (PXVector3D *)addVector:(PXVector3D *)vector

Parameters

vector

The vector to be added.

Return Value

The created vector.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:-3.0f y:10.0f z:-9.0f];
PXVector3D *vec3 = [vec1 addPoint:vec2];
// vec3 will be (2.0f, 17.0f, -5.0f)
PXVector3D *vec4 = [vec2 addPoint:vec3];
// vec4 will be (-1.0f, 27.0f, -14.0f)

Declared In

PXVector3D.m

crossProductWithVector:

Creates a vector that is perpendicular to the current vector and the given vector.

- (PXVector3D *)crossProductWithVector:(PXVector3D *)vector

Parameters

vector

The other vector.

Return Value

The created vector.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:0.7f y:0.4f z:0.591608f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:0.3f y:-0.4f z:-0.866025f];
PXVector3D *crossVector = [vec1 crossProductWithVector:vec2];
// crossVector will be (-0.109767f, 0.7837f, -0.4f)

Declared In

PXVector3D.m

decrementByVector:

Subtracts the values of this vector by the corresponding values of the given vector.

- (void)decrementByVector:(PXVector3D *)vector

Parameters

vector

The vector to use for subtraction.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:-3.0f y:10.0f z:-9.0f];
[vec1 decrementByVector:vec2];
// vec1 will be (8.0f, -3.0f, 13.0f)

Declared In

PXVector3D.m

dotProductWithVector:

Subtracts the values of this vector by the corresponding values of the given vector.

- (float)dotProductWithVector:(PXVector3D *)vector

Parameters

vector

The vector to use for subtraction.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:-3.0f y:10.0f z:-9.0f];
float dotProduct = [vec1 dotProductWithVector:vec2];
// dotProduct will be 19.0f

Declared In

PXVector3D.m

equalsVector:useAllFour:

Check to see if this vector is equal to another.

- (BOOL)equalsVector:(PXVector3D *)vector useAllFour:(BOOL)allFour

Parameters

vector

The vector for checking.

allFour

If YES then w is also used in the test, otherwise just x, y and z are used.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f w:0.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f w:1.0f];
BOOL isEqualWithoutAllFour = [vec1 equalsVector:vec2 useAllFour:NO];
BOOL isEqualWithAllFour = [vec1 equalsVector:vec2 useAllFour:YES];
// isEqualWithoutAllFour will be YES, isEqualWithAllFour will be NO.

Declared In

PXVector3D.m

incrementByVector:

Adds the values of this vector by the corresponding values of the given vector.

- (void)incrementByVector:(PXVector3D *)vector

Parameters

vector

The vector to use for addition.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:-3.0f y:10.0f z:-9.0f];
[vec1 incrementByVector:vec2];
// vec1 will be (2.0f, 17.0f, -5.0f)

Declared In

PXVector3D.m

initWithX:y:z:

Creates a new vector at (x, y, z).

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

Parameters

x

The horizontal coordinate.

y

The vertical coordinate.

z

The depth coordinate.

Example:

PXVector3D *vector = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f];
// vector will be (5,7,4)

Declared In

PXVector3D.m

initWithX:y:z:w:

Creates a new vector at (x, y, z) with rotation w.

- (id)initWithX:(float)_x y:(float)_y z:(float)_z w:(float)_w

Parameters

y

The vertical coordinate.

w

The angle of rotation.

Example:

PXVector3D *vector = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f w:0.0f];
// vector will be (5,7,4) with angle of rotation 0
z

The depth coordinate.

x

The horizontal coordinate.

Declared In

PXVector3D.m

nearEqualsVector:tolerance:useAllFour:

Check to see if each of this vector’s values are within a tolerance range of another

- (BOOL)nearEqualsVector:(PXVector3D *)vector tolerance:(float)tolerance useAllFour:(BOOL)allFour

Parameters

vector

The vector for checking.

tolerance

The tolerance for the check.

allFour

If YES then w is also used in the test, otherwise just x, y and z are used.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f w:0.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:4.2f y:7.4f z:4.1f w:0.9f];
BOOL isNearlyEqualByHalf = [vec1 nearEqualsVector:vec2 withTolerance:0.5f useAllFour:YES];
BOOL isNearlyEqualByOne = [vec1 nearEqualsVector:vec2 withTolerance:1.0f useAllFour:YES];
// isNearlyEqualByHalf will be NO, isNearlyEqualByOne will be YES.

Declared In

PXVector3D.m

negate

Negates the x, y and z values of the vector.

- (void)negate

Discussion

Example:

PXVector3D *vector = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f];
// vector will be (5,7,4)
[vector negate];
// vector will be (-5,-7,-4)

Declared In

PXVector3D.m

normalize

Normalizes the x, y and z values of the vector.

- (float)normalize

Return Value

The length of the vector.

Example:

PXVector3D *vector = [[PXVector3D alloc] initWithX:3.0f y:4.0f z:5.0f];
// vector will be (3.0f, 4.0f, 5.0f)
[vector normalize];
// vector will be (0.424264f, 0.565685f, 0.707107f)

Declared In

PXVector3D.m

project

Scales the x, y and z values of the vector by 1 / w.

- (void)project

Discussion

Example:

PXVector3D *vector = [[PXVector3D alloc] initWithX:3.0f y:4.0f z:5.0f w:10.0f];
// vector will be (3.0f, 4.0f, 5.0f, 10.0f)
[vector project];
// vector will be (0.3f, 0.4f, 0.5f, 10.0f)

Declared In

PXVector3D.m

scaleBy:

Scales the x, y and z values of the vector by the scalar.

- (void)scaleBy:(float)scalar

Discussion

Example:

PXVector3D *vector = [[PXVector3D alloc] initWithX:3.0f y:4.0f z:5.0f];
// vector will be (3.0f, 4.0f, 5.0f, 10.0f)
[vector scaleBy:0.1f];
// vector will be (0.3f, 0.4f, 0.5f, 10.0f)

Declared In

PXVector3D.m

setX:y:z:

Sets the vector to (x, y, z).

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

Parameters

x

The horizontal coordinate.

y

The vertical coordinate.

z

The depth coordinate.

Example:

PXVector3D *vector = [[PXVector3D alloc] init];
[vector setX:5.0f y:7.0f z:4.0f];
// vector will be (5,7,4) with angle of rotation 0

Declared In

PXVector3D.m

setX:y:z:w:

Sets the vector to (x, y, z) with rotation w.

- (void)setX:(float)_x y:(float)_y z:(float)_z w:(float)_w

Parameters

y

The vertical coordinate.

w

The angle of rotation.

Example:

PXVector3D *vector = [[PXVector3D alloc] init];
[vector setX:5.0f y:7.0f z:4.0f w:0.0f];
// vector will be (5,7,4) with angle of rotation 0
z

The depth coordinate.

x

The horizontal coordinate.

Declared In

PXVector3D.m

subtractVector:

Subtracts the values of the given vector to the corresponding values of this vector to create a new vector.

- (PXVector3D *)subtractVector:(PXVector3D *)vector

Parameters

vector

The vector to be added.

Return Value

The created vector.

Example:

PXVector3D *vec1 = [[PXVector3D alloc] initWithX:5.0f y:7.0f z:4.0f];
PXVector3D *vec2 = [[PXVector3D alloc] initWithX:-3.0f y:10.0f z:-9.0f];
PXVector3D *vec3 = [vec1 subtractVector:vec2];
// vec3 will be (-8.0f, 3.0f, -13.0f)
PXVector3D *vec4 = [vec2 subtractVector:vec3];
// vec4 will be (-5.0f, -7.0f, -4.0f)

Declared In

PXVector3D.m