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:
$componentsan 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
NANor infinite
Vector2::__construct()#
public function __construct(float $x, float $y)
Creates a two dimensional vector.
- Parameters:
$xthe first component,$ythe second component - Throws: InvalidComponentException if a component is
NANor infinite
Vector3::__construct()#
public function __construct(float $x, float $y, float $z)
Creates a three dimensional vector.
- Parameters:
$xthe first component,$ythe second component,$zthe third component - Throws: InvalidComponentException if a component is
NANor 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:
$vectorthe 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:
$vectorthe 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:
$scalethe 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:
$vectorthe 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:
$vectorthe other vector - Returns:
Vector3 - Throws:
TypeErrorif the argument is aVectorand not aVector3
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:
$vectorthe other vector, of the same size - Returns:
floatthe 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.
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.
- Returns: a new vector of the same type as this one
- Throws: InvalidComponentException if a component is
NANor infinite, or ZeroLengthException if the length is zero
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.
- Parameters:
$indexthe index - Returns:
float - Throws: IndexOutOfRangeException if the index does not exist
withElement()#
public function withElement(int $index, float $value): static
Returns a copy with one component replaced.
- Parameters:
$indexthe index,$valuethe new value - Returns: a new vector of the same type as this one
- Throws: IndexOutOfRangeException if the index does not exist, or InvalidComponentException if the value is
NANor infinite
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
count()#
public function count(): int
Returns the number of components. Implements Countable.
- Returns:
int
getIterator()#
public function getIterator(): Traversable
Returns an iterator over the components. Implements IteratorAggregate, so a vector works in foreach.
- Returns:
Traversable<int, float>
Vector2::withX()#
public function withX(float $x): Vector2
Returns a copy with the component at index 0 replaced.
- Parameters:
$xthe new value - Returns:
Vector2
Vector2::withY()#
public function withY(float $y): Vector2
Returns a copy with the component at index 1 replaced.
- Parameters:
$ythe new value - Returns:
Vector2
Vector3::withX()#
public function withX(float $x): Vector3
Returns a copy with the component at index 0 replaced.
- Parameters:
$xthe new value - Returns:
Vector3
Vector3::withY()#
public function withY(float $y): Vector3
Returns a copy with the component at index 1 replaced.
- Parameters:
$ythe new value - Returns:
Vector3
Vector3::withZ()#
public function withZ(float $z): Vector3
Returns a copy with the component at index 2 replaced.
- Parameters:
$zthe 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.
- Parameters:
$x,$y,$zand$wthe components - Throws: InvalidComponentException if a component is
NANor infinite
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:
$vectorthe three dimensional vector;$wthe fourth component, default1.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.
- Returns:
Vector3 - Throws: ZeroWComponentException if w is zero
Vector4::withX()#
public function withX(float $x): self
Returns a copy with another x component. The original does not change.
- Parameters:
$xthe new x component - Returns:
Vector4
Vector4::withY()#
public function withY(float $y): self
Returns a copy with another y component. The original does not change.
- Parameters:
$ythe new y component - Returns:
Vector4
Vector4::withZ()#
public function withZ(float $z): self
Returns a copy with another z component. The original does not change.
- Parameters:
$zthe new z component - Returns:
Vector4
Vector4::withW()#
public function withW(float $w): self
Returns a copy with another w component. The original does not change.
- Parameters:
$wthe 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