API reference

Every public method of Vector, Vector2 and Vector3, grouped by purpose.

Every method returns a new vector of the same type unless the return type says otherwise. Vector lives in PHPMath\Vector\Vector, and Vector2 and Vector3 extend it. All methods below are available on all three classes unless a heading says otherwise. Methods that take a Vector also accept a Vector2 or Vector3.

Construction#

Create a vector from an array, or from named components with Vector2 and Vector3.

Vector::__construct()#

public function __construct(array $components)

Creates a vector. Values are converted to floats, the keys are discarded and a negative zero becomes 0.

  • Parameters: $components an array of ints or floats, with at least one value
  • Throws: EmptyVectorException if the array is empty
  • Throws: InvalidComponentException if a value is not an int or a float (a numeric string is rejected too), or is NAN or infinite

Vector2::__construct()#

public function __construct(float $x, float $y)

Creates a two dimensional vector.

  • Parameters: $x the first component, $y the second component
  • Throws: InvalidComponentException if a component is NAN or infinite

Vector3::__construct()#

public function __construct(float $x, float $y, float $z)

Creates a three dimensional vector.

  • Parameters: $x the first component, $y the second component, $z the third component
  • Throws: InvalidComponentException if a component is NAN or infinite

Arithmetic#

Add, subtract, scale and negate.

add()#

public function add(Vector $vector): static

Adds a vector component by component. The result never holds a negative zero.

  • Parameters: $vector the vector to add, of the same size
  • Returns: a new vector of the same type as this one
  • Throws: SizeMismatchException if the sizes differ

subtract()#

public function subtract(Vector $vector): static

Subtracts a vector component by component. The result never holds a negative zero.

  • Parameters: $vector the vector to subtract, of the same size
  • Returns: a new vector of the same type as this one
  • Throws: SizeMismatchException if the sizes differ

scale()#

public function scale(float $scale): static

Multiplies every component by a factor. The result never holds a negative zero.

  • Parameters: $scale the factor
  • Returns: a new vector of the same type as this one

negate()#

public function negate(): static

Reverses the direction. The length stays the same. Same as scale(-1.0), so a component of 0 stays 0.

  • Returns: a new vector of the same type as this one

Products and angles#

Dot product, cross product and the angle between two vectors.

dotProduct()#

public function dotProduct(Vector $vector): float

Multiplies the components pairwise and adds the results.

  • Parameters: $vector the other vector, of the same size
  • Returns: float
  • Throws: SizeMismatchException if the sizes differ

Vector3::crossProduct()#

public function crossProduct(Vector3 $vector): Vector3

Returns a vector that is perpendicular to this vector and the given one.

  • Parameters: $vector the other vector
  • Returns: Vector3
  • Throws: TypeError if the argument is a Vector and not a Vector3

angleBetween()#

public function angleBetween(Vector $vector): float

Returns the angle between two vectors in radians, from 0 to pi. It uses the stable formula 2 * atan2(|| |b| * a - |a| * b ||, || |b| * a + |a| * b ||), so parallel vectors give exactly 0 and opposite vectors give pi.

  • Parameters: $vector the other vector, of the same size
  • Returns: float the angle in radians
  • Throws: SizeMismatchException if the sizes differ (checked first), or ZeroLengthException if either vector has a length of zero

Length and direction#

Measure a vector and turn it into a unit vector.

getLength()#

public function getLength(): float

Returns the length of the vector.

  • Returns: float

getLengthSquared()#

public function getLengthSquared(): float

Returns the length multiplied by itself, without taking a square root.

  • Returns: float

normalize()#

public function normalize(): static

Scales the vector to a length of 1, keeping its direction. The components are divided by the largest absolute component first, so huge and tiny vectors do not overflow or underflow.

getMinimumValue()#

public function getMinimumValue(): float

Returns the lowest component.

  • Returns: float

getMaximumValue()#

public function getMaximumValue(): float

Returns the highest component.

  • Returns: float

Elements#

Read, replace, count and iterate the components. Vector2 and Vector3 add named accessors.

getElement()#

public function getElement(int $index): float

Returns the component at a zero based index.

withElement()#

public function withElement(int $index, float $value): static

Returns a copy with one component replaced.

isFinite()#

public function isFinite(): bool

Tells whether all components are finite. The constructor rejects NAN and infinite values, so this is only false for the result of an operation that overflowed. See Core concepts.

  • Returns: bool

getSize()#

public function getSize(): int

Returns the number of components.

  • Returns: int

count()#

public function count(): int

Returns the number of components. Implements Countable.

  • Returns: int

toArray()#

public function toArray(): array

Returns the components.

  • Returns: list<float>

getIterator()#

public function getIterator(): Traversable

Returns an iterator over the components. Implements IteratorAggregate, so a vector works in foreach.

  • Returns: Traversable<int, float>

Vector2::getX()#

public function getX(): float

Returns the component at index 0.

  • Returns: float

Vector2::withX()#

public function withX(float $x): Vector2

Returns a copy with the component at index 0 replaced.

  • Parameters: $x the new value
  • Returns: Vector2

Vector2::getY()#

public function getY(): float

Returns the component at index 1.

  • Returns: float

Vector2::withY()#

public function withY(float $y): Vector2

Returns a copy with the component at index 1 replaced.

  • Parameters: $y the new value
  • Returns: Vector2

Vector3::getX()#

public function getX(): float

Returns the component at index 0.

  • Returns: float

Vector3::withX()#

public function withX(float $x): Vector3

Returns a copy with the component at index 0 replaced.

  • Parameters: $x the new value
  • Returns: Vector3

Vector3::getY()#

public function getY(): float

Returns the component at index 1.

  • Returns: float

Vector3::withY()#

public function withY(float $y): Vector3

Returns a copy with the component at index 1 replaced.

  • Parameters: $y the new value
  • Returns: Vector3

Vector3::getZ()#

public function getZ(): float

Returns the component at index 2.

  • Returns: float

Vector3::withZ()#

public function withZ(float $z): Vector3

Returns a copy with the component at index 2 replaced.

  • Parameters: $z the new value
  • Returns: Vector3

Vector4#

A vector with four components, used for points and directions in homogeneous coordinates. It has everything Vector has, plus the methods below.

Vector4::__construct()#

public function __construct(float $x, float $y, float $z, float $w)

Creates a vector with four components.

Vector4::fromVector3()#

public static function fromVector3(Vector3 $vector, float $w = 1.0): self

Creates a Vector4 from a Vector3. The default w of 1 makes a point, a w of 0 makes a direction.

  • Parameters: $vector the three dimensional vector; $w the fourth component, default 1.0
  • Returns: Vector4

Vector4::toVector3()#

public function toVector3(): Vector3

Drops the w component and keeps x, y and z unchanged.

  • Returns: Vector3

Vector4::project()#

public function project(): Vector3

Divides x, y and z by w, which turns homogeneous coordinates into a three dimensional point.

Vector4::getX()#

public function getX(): float

Returns the x component.

  • Returns: float

Vector4::withX()#

public function withX(float $x): self

Returns a copy with another x component. The original does not change.

  • Parameters: $x the new x component
  • Returns: Vector4

Vector4::getY()#

public function getY(): float

Returns the y component.

  • Returns: float

Vector4::withY()#

public function withY(float $y): self

Returns a copy with another y component. The original does not change.

  • Parameters: $y the new y component
  • Returns: Vector4

Vector4::getZ()#

public function getZ(): float

Returns the z component.

  • Returns: float

Vector4::withZ()#

public function withZ(float $z): self

Returns a copy with another z component. The original does not change.

  • Parameters: $z the new z component
  • Returns: Vector4

Vector4::getW()#

public function getW(): float

Returns the w component.

  • Returns: float

Vector4::withW()#

public function withW(float $w): self

Returns a copy with another w component. The original does not change.

  • Parameters: $w the new w component
  • Returns: Vector4

Conversion#

Turn a vector into text.

__toString()#

public function __toString(): string

Returns the components between square brackets, separated by a comma and a space, for example [1, 2.5]. Numbers are plain decimals with at most 14 decimals, no exponent, no trailing zeros and no negative zero. Implements Stringable.

  • Returns: string

toString()#

public function toString(): string

Returns the same text as __toString().

  • Returns: string