PHP function/script to determine if a point falls inside a polygon

Practice




PHP function/script for determining if a point falls inside a polygon
can you give an example of a ready-made solution?

class Polygon
{

protected $polygon = array();


public function PointInPoligon($arrpoint, $arrpoligonpoints)
{
$this->set_polygon($this->convert_arr($arrpoligonpoints));

return $this->calc($arrpoint);
}

public function convert_arr($points)
{
if (count($points) < 6)
return false;

$polygon = array();

foreach ($points as $key=>$point)
{
if ($key%2 == 0) ;else $polygon[] = array($points[$key-1], $points[$key ]);

}

return $polygon;
}

/**
* Polygon itself, with basic vector-based structure
* Array: [ [1,1], [2,1], [3,0], [2,-1] ]
*
* @var $polygon array
*/

public function set_polygon($polygon)
{
if (count($polygon) < 3)
return false;
if (!isset($polygon[0]['x']))
{
foreach ($polygon as &$point)
{
$point = array('x' => $point[0], 'y' => $point[1]);
}
}
$this->polygon = $polygon;
}

/**
* Check if $polygon contains $test value
*
* @var $test array(x=>decimal, y=>decimal)
*/

public function calc($test)
{
$q_patt = array(array(0, 1), array(3, 2));
$end = end($this->polygon);
$pred_pt = end($this->polygon);
$pred_pt['x'] -= $test['x'];
$pred_pt['y'] -= $test['y'];
$pred_q = $q_patt[$pred_pt['y'] < 0][$pred_pt['x'] < 0];
$w = 0;
for ($iter = reset($this->polygon); $iter !== false; $iter = next($this->
polygon))
{
$cur_pt = $iter;
$cur_pt['x'] -= $test['x'];
$cur_pt['y'] -= $test['y'];
$q = $q_patt[$cur_pt['y'] < 0][$cur_pt['x'] < 0];
switch ($q - $pred_q)
{
case - 3:
++$w;
break;
case 3:
--$w;
break;
case - 2:
if ($pred_pt['x'] * $cur_pt['y'] >= $pred_pt['y'] * $cur_pt['x'])
++$w;
break;
case 2:
if (!($pred_pt['x'] * $cur_pt['y'] >= $pred_pt['y'] * $cur_pt['x']))
--$w;
break;
}
$pred_pt = $cur_pt;
$pred_q = $q;
}

return $w != 0;
}

}

$p = new Polygon();
$good = array(
350,
180,
280,
380,
520,
350,
600,
170);

$p->PointInPoligon(array(x => 65, y =>567),$goodl)


HERE'S AN EXAMPLE http://my-city.com.ua/online/

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)