$xoffset) $points[$i]['x']+=$radius; elseif ($points[$i]['x']<$xoffset) $points[$i]['x']-=$radius; if ($points[$i]['y']>$yoffset) $points[$i]['y']+=$radius; elseif ($points[$i]['y']<$yoffset) $points[$i]['y']-=$radius; } /** loop through each set of coordinates - create a 2d normal for the polygon so we ** can determine which side of the line segment the point falls on. ** if all calculations return a negative number, the point is inside the polygon. ** if there are is 1 positive result, the polygon is outside and we return false. */ for ($i=0;$i0) return false; } //all must be negative, return true its inside! return true; } /** Points for our polygon. ** This is an octagon - code will work for any convex polygon. Point coordinate pairs must be listed in clockwise order. */ $points = array ( array( 'x' => 275, 'y' => 233 ), array( 'x' => 325, 'y' => 233 ), array( 'x' => 363, 'y' => 268 ), array( 'x' => 363, 'y' => 320 ), array( 'x' => 325, 'y' => 360 ), array( 'x' => 275, 'y' => 360 ), array( 'x' => 240, 'y' => 320 ), array( 'x' => 240, 'y' => 265 ) ); /* This array is a set of coordinate pairs. */ $coordset = array ( array ('x' => 361, 'y' => 296, 'where' => 'in (between 3 and 4)'), //in array ('x' => 243, 'y' => 247, 'where' => 'out'), //out array ('x' => 240, 'y' => 265, 'where' => 'on (vertex 8)'), //on (8) array ('x' => 301, 'y' => 297, 'where' => 'in (the middle)'), //in (middle) array ('x' => 280, 'y' => 230, 'where' => 'outside but in after raduis test)') //outside but in after radius test ); foreach ($coordset as $coords) echo 'Coordinate Pair ('.$coords['x'].','.$coords['y'].') '.(point_in_polygon($coords,$points)?'IS':'IS NOT').' inside the polygon.
'; ?>