Introduction
Immutable 2D geometry for PHP. Points, segments, lines, circles, rectangles and polygons are value objects with float coordinates.
Requirements#
The library has no runtime dependencies. It only needs a current PHP version.
- PHP 8.4 or newer
Installation#
Install the library with Composer.
composer require phpmath/geometry
A first example#
Create two points, measure the distance between them and find the middle. Coordinates are floats, so integers are converted for you.
use PHPMath\Geometry\Point;
$a = new Point(0, 0);
$b = new Point(3, 4);
echo $a->distanceTo($b); // 5
$middle = $a->midpointTo($b);
echo $middle->x; // 1.5
echo $middle->y; // 2
Calling midpointTo() did not change $a or $b. It returned a new Point. This is true for every operation in the library.
Where to go next#
Start with the core concepts, or jump straight to the shapes you need.
Core conceptsImmutability, creating shapes and reading values.Points and segmentsDistance, midpoints, tolerance and segment intersections.LinesInfinite lines, angles, parallels and intersections.Circles and rectanglesArea, containment and intersections.PolygonsArea, centroid, convexity and orientation.API referenceEvery class and method with its parameters and errors.