arnapou / geometry
Library - Geometry & Mathematics objects.
v1.0.2
2025-02-10 09:29 UTC
Requires
- php: ~8.3.0 || ~8.4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
README
This library is a simple tool which provides Geometry & Mathematics objects.
Installation
composer require arnapou/geometry
packagist 👉️ arnapou/geometry
Features
This lib provides immutable geometry Element like
- Point
- Circle + Ellipse + Arc
- Square + Rectangle
- Segment + Line
- Polyline
- Path
Plus Transformation like
- Rotation
- Scale
- Translation
In order to easy geometry code.
You can easily convert objects like this code which creates a diamond :
use Arnapou\Geometry\Element;
$diamond = (new Element\Circle(new Element\Point(0.0, 0.0), radius: 2.0))
->toPolyline(segments: 4)
->scale(2.0, Element\Line::horizontal())
->round(); // reduce floating-point issues
// Echo the following
// (2, 0) (0, 4) (-2, 0) (-0, -4)
echo implode(' ', array_map(fn(Element\Point $p) => "($p->x, $p->y)", $diamond->points)) . "\n";
There are also some common geometry features like
- Determining if a point lies on the interior of a shape:
$element->containsPoint($point)
- Calculating the intersection of two lines:
$point = $line1->intersect($line2)
- Calculating the intersection of two segments:
$point = $segment1->intersect($segment2)
Fluent syntax: it is easy to make your code understandable
use Arnapou\Geometry\Element\Line;
use Arnapou\Geometry\Element\Point;
use Arnapou\Geometry\Transformation\Scale;
$point = new Point(1.0, 2.0);
$circle = $point->toCircle(radius: 3.0);
$polyline = $point
->toRectangle(width: 3.0, height: 4.0)
->toPolyline()
->addElements($point->translate(-1.0, -1.0));
// Central Symmetry of elements
$elements = array_map(
new Scale(new Point(0.0, 0.0), -1.0),
[$circle, $polyline]
);
// Get a tangent segment
$tangent = $circle->getTangent(M_PI / 6, clockwise: true, length: 3.0);
// Move the segment to position its middle to the tangent point
$translation = $tangent->getMiddle()->toVector($tangent->a);
$tangent = $translation($tangent);
// Get the intersection point of the tangent with the X axis
$intersection = $tangent->toLine()->intersect(Line::horizontal());
// etc...
Turtle 🐢
We provide a Path builder named Turtle. This name is inspired from the old LOGO turtle old folks like me might remember... 😉
The idea is to build the path as if we were moving forward on the path we create
use Arnapou\Geometry\Core\Turtle;
// Rectangle.
$path = Turtle::start(50, 50)
->forward(20)->turn(90)
->forward(30)->turn(90)
->forward(20)->turn(90)
->forward(30)
->getPath(closed: true);
// Isosceles triangle with rounded corners.
$path = Turtle::start(50, 50)
->forward(50)->turn(120, radius: 20)
->forward(50)->turn(120, radius: 20)
->forward(50)->turn(120, radius: 20)
->getPath();
// Star with 11 branches.
$path = Turtle::start(50, 50)
->repeat(11, fn (Turtle $t) => $t->forward(40)->turn(180 - 180 / 11))
->getPath();
// etc...
Php versions
Date | Ref | 8.4 | 8.3 |
---|---|---|---|
29/01/2025 | 1.0.x, main | × | × |