ifsnop / martinez-rueda-php
Robust and tested library for boolean operations on polygons (union, intersection, difference, xor)
1.0.1
2026-05-10 09:48 UTC
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
- shipmonk/dead-code-detector: ^0.14.0
README
Robust and tested library for boolean operations on polygons (union, intersection, difference, xor) in PHP.
Some improvements over other libraries: This one is tested (see end-to-end directory) and optimized to use polygons with large amount of vertex (ie, binary search when doing intersections between segments).
This library can use arrays with polygons, geojson strings or geojson files directly.
Resources
- To understand the algorithm, you can go here
- Based somewhat on the F. Martinez (2008) algorithm:
- Website
- A new algorithm for computing Boolean operations on polygons 2009 Research GateScience Direct
- A simple algorithm for Boolean operations on polygons 2013
Examples
Some tests and demo implementation inside tests.php, extended coverage with PHPUnit:
- php tests.php
- ./vendor/bin/phpunit tests/
- ./vendor/phpstan/phpstan/phpstan --error-format=raw
For profiling:
- check that xdebug module is available and loaded: php -m | grep xdebug
- launch php with xdebug enabled:
php
-dxdebug.mode=profile
-dxdebug.start_with_request=yes
-dxdebug.output_dir=xdebug-profiler/
tests.php
Tutorials and notes
- Interactive tutorial Polygon Clipping (Part 1)
- Interactive tutorial Polygon Clipping (Part 2)
- Notes on the Martinez-Rueda Polygon Clipping algorithm
- Let's build! Boolean operations of polygonsPart 1 - IntroductionPart 2 - Intersections
Improvements
History
- This library is a port for PHP of pypolybool
- Previoulsy, was a port for JS of polybooljs
- Help from a .NET port