和
py r2y
以编程方式,它会是这样的:
boolean isPInR(double px, double py, double r1x, double r1y, double r2x, double r2y){
if(px > r1x && px < r2x && py < r1y && py > r2y){
//It is inside
return true;
}
return false;
}
编辑
如果您的多边形不是矩形,您可以使用 Java.awt.Polygon 类。在此类中,您将找到方法 contains(x,y) 如果具有 x 和 y 坐标的点位于多边形内部,则该方法返回 true。
此方法使用Ray-casting algorithm。为简化起见,此算法从您的点沿随机方向绘制一条线段。如果线段穿过多边形边界奇数次,则它在多边形内部。如果它穿过它偶数次,那么它就在外面。
要使用多边形类,您可以执行以下操作:
//This defines your polygon
int xCoord[] = {1,2,3,5,9,-5};
int yCoord[] = {18,-32,1,100,-100,0};
myPolygon = new Polygon(xCoord, yCoord, xCoord.length);
//This finds if the points defined by x and y coordinates is inside the polygon
Boolean isInside = myPolygon.contains(x,y);
别忘了
导入 java.awt.Polygon;
编辑
右坐标在 Double 中!
所以你需要使用Path2D.Double!
以import java.awt.geom.Path2D;开头
假设您从与以前类似的数组开始:
//This defines your polygon
Double xCoord[] = {1.00121,2,3.5464,5,9,-5};
Double yCoord[] = {18.147,-32,1,100,-100.32,0};
Path2D myPolygon = new Path2D.Double();
//Here you append all of your points to the polygon
for(int i = 0; i < xCoord.length; i++) {
myPolygon.moveTo(xCoord[i], yCoord[i]);
}
myPolygon.closePath();
//Now we want to know if the point x, y is inside the Polygon:
Double x; //The x coord
Double y; //The y coord
Boolean isInside = myPolygon.contains(x,y);
你可以选择 Double !