123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- DEFINE('DEBUG', false);
- function poly_init($max) {
- return array_fill(0, $max["x"]-1,
- array_fill(0, $max["y"]-1, FALSE));
- }
- function poly_compile($poly, $max, $test = false) {
- $res = poly_init($max);
-
-
-
-
-
- $bord_lenght = $max['x'];
- if ($max['y'] > $bord_lenght) {
- $bord_lenght = $max['y'];
- }
-
- $bords = array_fill(0, $bord_lenght, array());
-
- if (is_array($bords[$poly[0]['y']]))
- array_push($bords[$poly[0]['y']], $poly[0]['x']);
-
- $i = 1;
- $old_pente=0;
- for ( ;
- $i<sizeof($poly) && (!empty($poly[$i]['x']) && !empty($poly[$i]['y'])); $i++) {
-
-
- if ($poly[$i-1]['y'] == $poly[$i]['y']) {
- if ($poly[$i-1]['x'] == $poly[$i]['x'])
- continue;
- else {
- if (is_array($bords[$poly[$i]['y']]))
- array_push($bords[$poly[$i]['y']],$poly[$i]['x']);
- $old_pente=0;
- continue;
- }
- }
-
-
-
- if (is_array($bords[$poly[$i]['y']]))
- array_push($bords[$poly[$i]['y']], $poly[$i]['x']);
- if (DEBUG) echo '('.$poly[$i]['x'].';'.$poly[$i]['y'].') ';
-
-
-
- $pente = ($poly[$i-1]['x']-$poly[$i]['x'])/
- ($poly[$i-1]['y']-$poly[$i]['y']);
-
-
-
- if ($i>1)
- if (($old_pente<0 && $pente>0)
- || ($old_pente>0 && $pente<0)) {
- if (is_array($bords[$poly[$i]['y']]))
- array_push($bords[$poly[$i]['y']],$poly[$i]['x']);
-
- if (DEBUG)
- echo '*('.$poly[$i]['x'].
- ';'.$poly[$i]['y'].') ';
- }
-
- $dy_inc = ($poly[$i]['y']-$poly[$i-1]['y']) > 0 ? 1 : -1;
- $x = $poly[$i-1]['x'];
-
-
-
- for ($dy = $poly[$i-1]['y']+$dy_inc;
- $dy != $poly[$i]['y'];
- $dy += $dy_inc) {
- $x += $pente*$dy_inc;
- array_push($bords[$dy], $x);
- }
- $old_pente = $pente;
- }
-
-
- if ($poly[$i-1]['y']!=$poly[0]['y']) {
-
- $rest = $poly[0]['y']-$poly[1]['y'];
- if ($rest!=0)
- $pente1 = ($poly[0]['x']-$poly[1]['x'])/($rest);
- else
- $pente1 = 0;
-
-
- $pente = ($poly[$i-1]['x']-$poly[0]['x'])/
- ($poly[$i-1]['y']-$poly[0]['y']);
-
- if (($pente1<0 && $pente>0) || ($pente1>0 && $pente<0)) {
- if (is_array($bords[$poly[$i]['y']]))
- array_push($bords[$poly[$i]['y']], round($poly[$i]['x']));
-
- }
-
- if (($old_pente<0 && $pente>0) || ($old_pente>0 && $pente<0)) {
- if (is_array($bords[$poly[$i-1]['y']]))
- array_push($bords[$poly[$i-1]['y']], round($poly[$i-1]['x']));
-
- }
-
- $dy_inc = ($poly[0]['y']-$poly[$i-1]['y']) > 0 ? 1 : -1;
- $x = $poly[$i-1]['x'];
- for ($dy = $poly[$i-1]['y']+$dy_inc;
- $dy != $poly[0]['y'];
- $dy += $dy_inc)
- {
- $x += $pente*$dy_inc;
- array_push($bords[$dy], round($x));
- }
- }
-
-
-
- $n = count($bords);
- for ($i = 0; $i<$n; $i++) {
-
- if (is_array($bords[$i])) {
- sort($bords[$i]);
- }
-
- for ($j = 0; $j<sizeof($bords[$i]);$j+=2)
- for ($k = round($bords[$i][$j]); $k<=$bords[$i][$j+1];$k++) {
- $res[$k][$i] = true;
- if ($test == 1) {
-
- echo $k.' '.$i; echo '<br />';
- }
- }
- }
- return $res;
- }
- function poly_dump(&$poly, $max, $format='raw') {
- if ($format == 'html') {
- $s = "<div style='font-size: 8px; line-height:3px'><pre>\n";
- }
- for ($i=0; $i<$max['y']; $i++) {
- for($j=0; $j<$max['x']; $j++)
- if($poly[$j][$i] == TRUE)
- $s .= ($format=='html'?"<b>1</b>":'1');
- else
- $s .= "0";
- $s .= ($format=='html'?"<br />\n":"\n");
- }
- $s .= ($format=='html'?"</pre></div>\n":"\n");
- return $s;
- }
- function poly_result(&$poly1, &$poly2, $max) {
- $onlyIn1 = 0;
- $surfaceOf1 = 0;
- $surfaceOf2 = 0;
- for ($i=0; $i<$max['x']; $i++)
- for($j=0; $j<$max['y']; $j++) {
- if (isset($poly1[$i][$j]) && ($poly1[$i][$j] == TRUE)) {
- $surfaceOf1++;
- if (isset($poly2[$i][$j]) && ($poly2[$i][$j] == FALSE))
- $onlyIn1++;
- }
- if (isset($poly2[$i][$j]) && ($poly2[$i][$j] == TRUE))
- $surfaceOf2++;
- }
-
- return array (
- "s1" => $surfaceOf1,
- "s2" => $surfaceOf2,
- "both" => $surfaceOf1 - $onlyIn1,
- "s1Only" => $onlyIn1,
- "s2Only" => $surfaceOf2 - ($surfaceOf1 - $onlyIn1));
- }
- function poly_touch(&$poly1, &$poly2, $max) {
- for ($i=0; $i<$max['x']; $i++) {
- for($j=0; $j<$max['y']; $j++) {
- if (isset($poly1[$i][$j]) && ($poly1[$i][$j] == true)
- && isset($poly2[$i][$j]) && ($poly2[$i][$j] == true)) {
- return true;
- }
- }
- }
- return FALSE;
- }
- function convert_coordinates($coords,$sep='|') {
- $points = array();
- $pairs = explode($sep,$coords);
- foreach ($pairs as $idx => $pcoord) {
- list($x,$y) = explode(';',$pcoord);
- $points[] = array('x'=>$x,'y'=>$y);
- }
- return $points;
- }
- function poly_get_max(&$coords1, &$coords2) {
- $mx = 0;
- $my = 0;
- foreach ($coords1 as $coord) {
- if ($coord['x'] > $mx) {
- $mx = $coord['x'];
- }
- if ($coord['y'] > $my) {
- $my = $coord['y'];
- }
- }
- foreach ($coords2 as $coord) {
- if ($coord['x'] > $mx) {
- $mx = $coord['x'];
- }
- if ($coord['y'] > $my) {
- $my = $coord['y'];
- }
- }
- return array('x'=>$mx,'y'=>$my);
- }
|