I want to determine if coordinates are inside a polygon, however I'm getting extra results that are not in the polygon. It's almost like it only checks for if EITHER the lat or lon is in range but not both. Any ideas??
public function subcacheClients (){
$clients = gzfile('tmp/whazzup/cache/clients.txt.gz');
for($i=1;$i<count($clients);$i++){
$clients[$i] = explode(':', $clients[$i]);
//polygon of Canada
$polylat = array(90,82,77,75,65,64,61,53,49,45,45,42,47,47.33,45,45,43.63,43.5,42.83,41.63,41.85,42.33,42.5,43.58,45.33,48,49,49,48.42,48.18,48.5,48.33,5,54,54.83,59.5,60.32,90);
$polylon = array(-141,-60,-75,-76,-57.75,-63,63,-54,-51,-51,-53,-67,-67.75,-69.33,-71.5,-75,-76.92,-79.2,-78.93,-82.5,-83.06,-83.03,-82.5,-82.12,-82.5,-88.33,-95.0,-123.33,-123.12,-123.35,-124.83,-128,-133.75,-136,-130,-135,-141,-141);
$inpolygon = $this->inPolygon($clients[$i][5], $clients[$i][6], $polylat, $polylon);
if($inpolygon == true && $clients[$i][5]>0 && $clients[$i][6]<0 && $clients[$i][3]=='ATC'){
echo $clients[$i][0];
file_put_contents('tmp/whazzup/cache/controllersOnline.txt', implode(':', $clients[$i]), FILE_APPEND);
}
}
}
public function inPolygon($lat, $lon, $polylat, $polylon) {
$j = count($polylat)-1;
$result = false;
for($i=0;$i<count($polylat);$i++){
if($polylat[$i]<$lat && $polylat[$j]>=$lat
|| $polylat[$j]<$lat && $polylat[$i]>=$lat){ //if the latitude at the beggining is bigger than and at the end is smaller than or vise versa
if($polylon[$i]+($lat-$polylat[$i])/($polylat[$j]-$polylat[$i])*($polylon[$j]-$polylon[$i])<$lon){ //
$result = true;
}
}
$j=$i;
}
return $result;
}