Introduction
Immutable 2D, 3D, 4D and n-dimensional vectors for PHP. Every vector is a readonly value object made of floats.
Requirements#
The library is plain PHP and has no runtime dependencies.
- PHP 8.4 or newer
Installation#
Install the library with Composer.
composer require phpmath/vector
A first example#
Create a vector from its components, then combine it with another one. A vector is a list of numbers, for example a point or a direction in space.
use PHPMath\Vector\Vector3;
$a = new Vector3(1, 0, 0);
$b = new Vector3(0, 1, 0);
$sum = $a->add($b);
echo $sum; // [1, 1, 0]
echo $a; // [1, 0, 0]
Calling add() did not change $a. It returned a new Vector3. This is true for every operation in the library.
Where to go next#
Start with the core concepts, or jump straight to the topic you need.
Core conceptsImmutability, creating vectors and reading values.ArithmeticAdd, subtract, scale and negate.Products and anglesDot product, cross product and the angle between vectors.Length and directionLength, normalisation, minimum and maximum.Working with elementsRead, replace, count and iterate components.Four dimensionsPoints and directions in homogeneous coordinates.API referenceEvery method with its parameters and errors.