我有一个凸多边形(通常只是一个旋转的方形),我知道所有4个点.如何确定给定点(黄色/绿色)是否在多边形内?
编辑:对于这个特定的项目,我无法访问JDK的所有库,例如AWT.
解决方法:
此页面:http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html显示了如何为任何多边形执行此操作.
我有一个Java实现,但它太大了,不能完整地发布在这里.但是,您应该能够解决这个问题:
class Boundary {
private final Point[] points; // Points making up the boundary
...
/**
* Return true if the given point is contained inside the boundary.
* See: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
* @param test The point to check
* @return true if the point is inside the boundary, false otherwise
*
*/
public boolean contains(Point test) {
int i;
int j;
boolean result = false;
for (i = 0, j = points.length - 1; i < points.length; j = i++) {
if ((points[i].y > test.y) != (points[j].y > test.y) &&
(test.x < (points[j].x - points[i].x) * (test.y - points[i].y) / (points[j].y-points[i].y) + points[i].x)) {
result = !result;
}
}
return result;
}
}
这是Point类的草图
/**
* Two dimensional cartesian point.
*/
public class Point {
public final double x;
public final double y;
...
}
标签:java,polygon,geometry 来源: https://codeday.me/bug/20190918/1811692.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。