1. Go to this page and download the library: Download ifsnop/martinez-rueda-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ifsnop / martinez-rueda-php example snippets
$poly->regions // array de array de Point
$poly->isInverted // true = el interior lógico es el exterior geométrico
$poly->numPoints // total de puntos
$poly->getArray() // convierte a array [[x,y], ...]
$poly->getArrayClosed() // igual pero cierra cada anillo
// Todos aceptan dos Polygon y devuelven un Polygon
Algorithm::union(Polygon $a, Polygon $b): Polygon
Algorithm::intersect(Polygon $a, Polygon $b): Polygon
Algorithm::intersection(Polygon $a, Polygon $b): Polygon // alias de intersect
Algorithm::difference(Polygon $a, Polygon $b): Polygon // A \ B
Algorithm::differenceRev(Polygon $a, Polygon $b): Polygon // B \ A
Algorithm::xoring(Polygon $a, Polygon $b): Polygon
// Unión de múltiples polígonos (al menos uno requerido)
Algorithm::unionMany(array $polygons): Polygon
$segs1 = Algorithm::segments($polygon1); // Fase 1
$segs2 = Algorithm::segments($polygon2);
$combined = Algorithm::combine($segs1, $segs2); // Fase 2
$selected = Algorithm::selectUnion($combined); // Selección lógica
$result = Algorithm::polygon($selected); // Fase 3
$base = Polygon::create()->fillFromArray([
[[0,0], [20,0], [20,20], [0,20]]
]);
$hueco = Polygon::create()->fillFromArray([
[[5,5], [15,5], [15,15], [5,15]]
]);
// Base con un agujero cuadrado en el centro
$resultado = Algorithm::difference($base, $hueco);