Skip to content

Instantly share code, notes, and snippets.

@ronnywang
Created August 17, 2013 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronnywang/6257596 to your computer and use it in GitHub Desktop.
Save ronnywang/6257596 to your computer and use it in GitHub Desktop.
foreach (City::search("{$x} > x_min AND {$x} < x_max AND {$y} > y_min AND {$y} < y_max") as $city) {
$left_node = $right_node = 0;
$lastx = $lasty = null;
foreach ($city->bounds as $bound) {
if (is_null($lastx)) {
$lastx = $bound->x;
$lasty = $bound->y;
continue;
}
if (($bound->y >= $y and $y >= $lasty) or ($lasty >= $y and $y >= $bound->y)) {
if ($x >= $bound->x and $x >= $lastx) {
$right_node ++;
} elseif ($x <= $bound->x and $x <= $lastx) {
$left_node ++;
} else {
$deltax = $bound->x - $lastx;
$deltay = $bound->y - $lasty;
$tempx = ($y - $lasty) * $deltax / $deltay + $lastx;
if ($x >= $deltax) {
$right_node ++;
} else {
$left_node ++;
}
}
}
$lastx = $bound->x;
$lasty = $bound->y;
}
if ($left_node % 2 == 1 && $right_node % 2 == 1){
echo $city->city . ' ' . $city->sub_city;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment