x = $a; $this->y = $b; } function getHash() { return $this->x."_".$this->y; // Identifiant texte du point } } class segment { public $a, $b; //Points function __construct($a, $b) { //Deux points $this->a = $a; $this->b = $b; } function __clone() { $this->a = clone $this->a; $this->b = clone $this->b; } function __toString() { return $this->a->getHash()."=".$this->b->getHash(); } function getHash() { return $this->a->getHash()."=".$this->b->getHash(); // Identifiant texte du segment } function getIHash() { return $this->b->getHash()."=".$this->a->getHash(); // Inverse } function estSuite($n) { if (($this->b->x == $n->a->x) and ($this->b->y == $n->a->y)) { return 1; } else { return 0; } } function aSuite($n) { if ($n == '') { return 0;} if (($this->b->x == $n->a->x) and ($this->b->y == $n->a->y) and ($n != '')) { return 1; } else { return 0; } } function count_in_array($a1) { // Comptage du nombre de segments identiques ou inverses dans un tableau $c = 0; $a2 = $a1; reset($a2); for ($i = 0; $i < count($a2); $i++) { $ts = current($a2); if ($ts === false) {return 0;} if (($ts->getHash() == $this->GetHash()) or ($ts->getIHash() == $this->getHash())) { $c++; } next($a2); } return $c; } } class polygone { public $pts = array(); function addv($a, $b) { // Ajout d'un point en fournissant ses coordonnées $this->pts[] = new point($a, $b); } function getPts() { // Tableau des points du polygone $nbpts = count($this->pts); $sp = array(); for ($i = 0; $i < ($nbpts - 1); $i++) { $np = $this->pts[$i]; $ph = $np->getHash(); $sp[$ph] = $np; } return $sp; } function getSegments() { // Tableau des segments du polygone $nbpts = count($this->pts); $s = array(); for ($i = 0; $i < ($nbpts - 1); $i++) { $ns = new segment($this->pts[$i], $this->pts[$i+1]); $sh = $ns->getHash(); $s[$sh] = $ns; } return $s; } function addSegment($ns) { $this->pts[] = new point($ns->a->x, $ns->a->y); $this->pts[] = new point($ns->b->x, $ns->b->y); } function union($p2) { $p1s = $this->getSegments(); $p2s = $p2->getSegments(); $nsp1 = count($p1s); // Nombre de segments du p1 $nsp2 = count($p2s); // Nombre de segments du p2 $ns1 = array(); // Tableau de segments partie 1 du polygone de fusion (segments uniques) // On explore le p1 for ($i = 0; $i <$nsp1; $i++) { $k1 = current($p1s)->getHash(); $ik1 = current($p1s)->getIHash(); if ((!array_key_exists($k1, $p2s)) and (!array_key_exists($ik1, $p2s))) { $ns1[] = current($p1s); // Si le segment n'est pas commun, on l'ajoute au polygone fusion } else { // Rien } next($p1s); //On avance d'un segment du p1 } //$ns2 = array(); // Nouvelle partie 2 du polygone de fusion (segments uniques) // On explore le p2 for ($i = 0; $i <$nsp2; $i++) { $k2 = current($p2s)->getHash(); $ik2 = current($p2s)->getIHash(); if ((!array_key_exists($k2, $p1s)) and (!array_key_exists($ik2, $p1s))) { $ns1[] = current($p2s); // Si le segment n'est pas commun, on l'ajoute au polygone fusion } else { // Rien } next ($p2s); // On avance d'un segment du p2 } // On construit le polygone de fusion $np = new polygone(); reset($ns1); // Mise à zéro du pointeur $n = count($ns1); // Nombre de sgements dans ns1 next($ns1); $b1 = 1; $b2 = 1; $ns2 = $ns1; $t1 = array_shift($ns1); // Récupération du premier segment $np->addSegment($t1); // Ajout à la solutions $t2 = 'rien'; $i1 = 0; while ($b1 == 1) { $i1 += 1; if ($i1 > $n) {$b1 = 0;} if ($t1 == 'rien') {$t1 = $t2;} $tr = Array($t1); // Sélection pour réduction $ns1 = array_diff($ns1, $tr); // Réduction du tableau reset($ns2); $i2 = 0; while ($b2 == 1) { $i2 += 1; if ($i2 > $n) {$b2 = 0;} if ($t2 == 'rien') {$t2 = next($ns2);} if ($t1->aSuite($t2) == 1) { $np->addSegment($t2); // Ajout à la solution $tr = Array($t2); // Sélection pour réduction $ns2 = array_diff($ns2, $tr); // Réduction du tableau $b2 = 0; } else { $t2 = 'rien'; } } $b2 = 1; $t1 = 'rien'; reset($ns1); if ($ns1 == array()) {$b1 = 0;} } return $np; } function union_multi($p) { $p1s = $this->getSegments(); $ns1 = array(); // Tableau de segments partie 1 du polygone de fusion (segments uniques) // Remplissage du tableaux de tous les segments $s = $p1s; for ($i = 0; $i < count($p); $i++) { $ts = $p[$i]->getSegments(); $s = array_merge($s, $ts); } $ns = count($s); // Nomnbre de segments total reset($s); // On explore le tableau de tous les segments pour ne garder que ceux qui ne sont pas en double for ($i = 0; $i <$ns; $i++) { $cs = current($s); $ct = $cs->count_in_array($s); if ($ct == 1) { $ns1[] = $cs; // Si le segment n'est pas commun, on l'ajoute au polygone fusion } else { // Rien } next($s); } // On construit le polygone de fusion $np = new polygone(); reset($ns1); // Mise à zéro du pointeur $n = count($ns1); // Nombre de sgements dans ns1 next($ns1); $b1 = 1; $b2 = 1; $ns2 = $ns1; $t1 = array_shift($ns1); // Récupération du premier segment $np->addSegment($t1); // Ajout à la solutions $t2 = 'rien'; $i1 = 0; while ($b1 == 1) { $i1 += 1; if ($i1 > $n) {$b1 = 0;} if ($t1 == 'rien') {$t1 = $t2;} $tr = Array($t1); // Sélection pour réduction $ns1 = array_diff($ns1, $tr); // Réduction du tableau reset($ns2); $i2 = 0; while ($b2 == 1) { $i2 += 1; if ($i2 > $n) {$b2 = 0;} if ($t2 == 'rien') {$t2 = next($ns2);} if ($t1->aSuite($t2) == 1) { $np->addSegment($t2); // Ajout à la solution $tr = Array($t2); // Sélection pour réduction $ns2 = array_diff($ns2, $tr); // Réduction du tableau $b2 = 0; } else { $t2 = 'rien'; } } $b2 = 1; $t1 = 'rien'; reset($ns1); if ($ns1 == array()) {$b1 = 0;} } return $np; } function toJSON() { $t = "[ ["; for ($i = 0; $i < count($this->pts); $i++) { $t .= "[".$this->pts[$i]->x.", ".$this->pts[$i]->y."]"; if ($i < (count($this->pts) - 1)) { $t .= ","; } } $t .= "] ]"; return $t; } function fromWKT($w) { $t = substr($w, 9); $t = substr($t, 0, strlen($t) - 2); $a = explode(",", $t); $pts = array(); for ($i = 0; $i < count($a); $i++) { $tp = explode(" ", $a[$i]); $this->addv($tp[0], $tp[1]); } } function touches($p2) { $p1s = $this->getSegments(); $p2s = $p2->getSegments(); $nsp1 = count($p1s); // Nombre de segments du p1 $nsp2 = count($p2s); // Nombre de segments du p2 $ns1 = array(); // Tableau de segments partie 1 du polygone de fusion (segments uniques) // On explore le p1 for ($i = 0; $i <$nsp1; $i++) { $k1 = current($p1s)->getHash(); $ik1 = current($p1s)->getIHash(); if ((array_key_exists($k1, $p2s)) or (array_key_exists($ik1, $p2s))) { return true; } next($p1s); //On avance d'un segment du p1 } return false; } } ?>