123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- <?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 - 1]['y']]))
- array_push($bords[$poly[$i - 1]['y']], round($poly[$i - 1]['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) {
- if (!isset($bords[$i][$j + 1])) {
- continue;
- }
- 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);
- }
- class Geometry
- {
-
- public static function decodePoint($coordinates)
- {
- $coordinates = explode(';', $coordinates);
- return [
- 'x' => intval($coordinates[0]),
- 'y' => intval($coordinates[1])
- ];
- }
-
- public static function decodeSquare($coordinates)
- {
- $coordinates = explode('|', $coordinates);
- $originString = explode(';', $coordinates[0]);
- return [
- 'x' => intval($originString[0]),
- 'y' => intval($originString[1]),
- 'width' => intval($coordinates[1]),
- 'height' => intval($coordinates[2])
- ];
- }
-
- public static function decodeEllipse($coordinates)
- {
- $coordinates = explode('|', $coordinates);
- $originString = explode(';', $coordinates[0]);
- return [
- 'center_x' => intval($originString[0]),
- 'center_y' => intval($originString[1]),
- 'radius_x' => intval($coordinates[1]),
- 'radius_y' => intval($coordinates[2])
- ];
- }
-
- public static function decodePolygon($coordinates)
- {
- $coordinates = explode('|', $coordinates);
- $points = [];
- foreach ($coordinates as $coordinate) {
- $point = explode(';', $coordinate);
- $points[] = [
- intval($point[0]),
- intval($point[1])
- ];
- }
- return $points;
- }
-
- public static function pointIsInSquare($properties, $point)
- {
- $left = $properties['x'];
- $right = $properties['x'] + $properties['width'];
- $top = $properties['y'];
- $bottom = $properties['y'] + $properties['height'];
- $xIsValid = $point['x'] >= $left && $point['x'] <= $right;
- $yIsValid = $point['y'] >= $top && $point['y'] <= $bottom;
- return $xIsValid && $yIsValid;
- }
-
- public static function pointIsInEllipse($properties, $point)
- {
- $dX = $point['x'] - $properties['center_x'];
- $dY = $point['y'] - $properties['center_y'];
- $dividend = pow($dX, 2) / pow($properties['radius_x'], 2);
- $divider = pow($dY, 2) / pow($properties['radius_y'], 2);
- return $dividend + $divider <= 1;
- }
-
- public static function pointIsInPolygon($properties, $point) {
- $points = $properties;
- $isInside = false;
- for ($i = 0, $j = count($points) - 1; $i < count($points); $j = $i++) {
- $xi = $points[$i][0];
- $yi = $points[$i][1];
- $xj = $points[$j][0];
- $yj = $points[$j][1];
- $intersect = (($yi > $point['y']) !== ($yj > $point['y'])) &&
- ($point['x'] < ($xj - $xi) * ($point['y'] - $yi) / ($yj - $yi) + $xi);
- if ($intersect) {
- $isInside = !$isInside;
- }
- }
- return $isInside;
- }
- }
|