问题
The logic is: user is editing an existing polygonal area of which extreme points are stored in the database. After editing the modified co-ordinates are updated in the database. Now I need to find whether a certain point exists inside the modified polygon or not. For that I'm assuming I have to do the server-side checking. The client-side checking is easily done by javaScript and that I've done already. The real problem I'm having is to check this on the server-side. Any suggestions?
回答1:
I have implemented server side point in polygon in PHP.
<?php
//Point-In-Polygon Algorithm test parameters
$polySides = 4; //how many corners the polygon has
$polyX = array(4,9,11,2);//horizontal coordinates of corners
$polyY = array(10,7,2,2);//vertical coordinates of corners
$x = 3.5;//point
//$y = 13.5;//Outside
$y = 3.5;//Inside
function pointInPolygon($polySides,$polyX,$polyY,$x,$y) {
$j = $polySides-1 ;
$oddNodes = 0;
for ($i=0; $i<$polySides; $i++) {
if ($polyY[$i]<$y && $polyY[$j]>=$y
|| $polyY[$j]<$y && $polyY[$i]>=$y) {
if ($polyX[$i]+($y-$polyY[$i])/($polyY[$j]-$polyY[$i])*($polyX[$j]-$polyX[$i])<$x) {
$oddNodes=!$oddNodes; }}
$j=$i; }
return $oddNodes; }
//Test
if (pointInPolygon($polySides,$polyX,$polyY,$x,$y)){
echo "Is in polygon!";
}
else echo "Is not in polygon";
?>
This can be modified to suit language of choice ie. C#
来源:https://stackoverflow.com/questions/14394719/server-side-checking-of-a-points-existence-inside-a-polygonal-area-using-google