Skip to content

Instantly share code, notes, and snippets.

@suncn
Created February 5, 2020 08:54
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 suncn/506e242edf85eea8db37608c4efaff44 to your computer and use it in GitHub Desktop.
Save suncn/506e242edf85eea8db37608c4efaff44 to your computer and use it in GitHub Desktop.
判断点是否在地理围栏中
/**
*
* @param point 判断点
* @param points 多边形的顶点集合
* @return
*/
public boolean pointInPoly(Point point ,Point[] points){
boolean isContains=false;
int end=points.length-1;
for (int i=0;i<points.length;i++){
//通过判断点的y坐标和每条边的两个端点的y坐标比较,判断点是否在这条边的中间
if (point.getY()>points[i].getY()&&point.getY()<=points[end].getY()||point.getY()<=points[i].getY()&&point.getY()>=points[end].getY()){
//计算交点的x坐标值,如果小于x则记为一次(只需要判断射线一侧的点个数即可)
if((point.getY()-points[i].getY())/(points[end].getY()-points[i].getY())*(points[end].getX()-points[i].getX())+points[i].getX()<point.getX()){
isContains=!isContains;
}
}
//移动顶点位置
end=i;
}
return isContains;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment