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.

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.