Advertisement
Guest User

Untitled

a guest
Dec 13th, 2012
1,959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.44 KB | None | 0 0
  1. <?php
  2.  
  3. $rect = array( array(array(0, 10), array(10, 0)),
  4.            array(array(19, 10), array(23, 0)),
  5.            array(array(10, 13), array(19, 0))
  6.        );
  7.  
  8.  
  9. $pset = array();
  10. foreach ($rect as $r) {
  11.     list($x1, $y1) = $r[0];
  12.     list($x2, $y2) = $r[1];
  13.     foreach (array($x1.":".$y1, $x2.":".$y1, $x2.":".$y2, $x1.":".$y2) as $pt) {
  14.         if (array_key_exists($pt, $pset)) {
  15.             unset($pset[$pt]);
  16.         } else {
  17.             $pset[$pt] = 1;
  18.         }
  19.     }
  20. }
  21. $pt_len = sizeof($pset);
  22. $points = array();
  23. foreach (array_keys($pset) as $pt) {
  24.     list($x, $y) = explode(":", $pt);
  25.     array_push($points, array(intval($x), intval($y)));
  26. }
  27.  
  28. $edges_h = array();
  29. $edges_v = array();
  30.  
  31. /* Sort by x coordinates. In case of equal x, sort by lowest y. */
  32. sort($points);
  33. $i = 0;
  34. while ($i < $pt_len) {
  35.     $curr_x = $points[$i][0];
  36.     while ($i < $pt_len && $points[$i][0] == $curr_x) {
  37.         list($a, $b) = $points[$i];
  38.         list($c, $d) = $points[$i + 1];
  39.         $edges_v[$a.":".$b] = $c.":".$d;
  40.         $edges_v[$c.":".$d] = $a.":".$b;
  41.         $i += 2;
  42.     }
  43. }
  44.  
  45. /* Sort by y coordinates. In case of equal y, sort by lowest x. */
  46. function y_then_x($a, $b)
  47. {
  48.     if ($a[1] < $b[1] || ($a[1] == $b[1] && $a[0] < $b[0]))
  49.         return -1;
  50.     return 1;
  51. }
  52. usort($points, "y_then_x");
  53. $i = 0;
  54. while ($i < $pt_len) {
  55.     $curr_y = $points[$i][1];
  56.     while ($i < $pt_len && $points[$i][1] == $curr_y) {
  57.         list($a, $b) = $points[$i];
  58.         list($c, $d) = $points[$i + 1];
  59.         $edges_h[$a.":".$b] = $c.":".$d;
  60.         $edges_h[$c.":".$d] = $a.":".$b;
  61.         $i += 2;
  62.     }
  63. }
  64.  
  65. /* Get all polygons. */
  66. $p = array();
  67. reset($edges_h);
  68. while (!empty($edges_h)) {
  69.     $k = key($edges_h);
  70.     $polygon = array(array($k, 0));
  71.     unset($edges_h[$k]);
  72.     while (1) {
  73.         list($curr, $e) = end($polygon);
  74.         if ($e == 0) {
  75.             $next_vertex = $edges_v[$curr];
  76.             unset($edges_v[$curr]);
  77.             array_push($polygon, array($next_vertex, 1));
  78.         } else {
  79.             $next_vertex = $edges_h[$curr];
  80.             unset($edges_h[$curr]);
  81.             array_push($polygon, array($next_vertex, 0));
  82.         }
  83.         if (end($polygon) == $polygon[0]) {
  84.             /* Closed polygon. */
  85.             array_pop($polygon);
  86.             break;
  87.         }
  88.     }
  89.     /* Remove implementation-markers from polygon. */
  90.     $poly = array();
  91.     foreach($polygon as $v) {
  92.         list($pt, $marker) = $v;
  93.         list($x, $y) = explode(":", $pt);
  94.         array_push($poly, array($x, $y));
  95.         unset($edges_h[$pt]);
  96.         unset($edges_v[$pt]);
  97.     }
  98.  
  99.     array_push($p, $poly);
  100. }
  101.  
  102. foreach($p as $pi) {
  103.     foreach($pi as $v) {
  104.         list($x, $y) = $v;
  105.         echo "($x, $y)  ";
  106.     }
  107.     echo "\n";
  108. }
  109.  
  110. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement