我正在尝试测试一个点是否包含一个多边形,并查看是否包含多边形。我找到了一些代码,但我试过了,我不知道我做错了什么……
我得到了Vectors来保存x和y点:
Vector<Double> vxpoints;
Vector<Double> vxpoints;这就是我的方法“包含”
public boolean contains(double x, double y) {
int i,j = this.npoints - 1;
boolean oddNodes = false;
for(i=0;i<this.npoints;j=i++) {
if ((((this.vypoints.get(i) <= y) && (y < this.vypoints.get(j))) ||
((this.vypoints.get(j) <= y) && (y < this.vypoints.get(i)))) &&
(x < (this.vxpoints.get(j) - this.vxpoints.get(i)) * (y - this.vypoints.get(i)) / (this.vypoints.get(j) - this.vypoints.get(i)) + this.vxpoints.get(i)))
oddNodes = !oddNodes;
}
return oddNodes;当我测试它时,我使用“简单多边形”:(有到数组或点,我在我的类中转换成向量)
double xpoints[] = {100,100,200,200}; //Square
double ypoints[] = {100,200,100,200};
PolygonDouble test = new PolygonDouble(xpoints, ypoints);
//System.out.println(test.getNumberOfCoordinates());
if(test.contains(110,110))
System.out.println("Inside");
else
System.out.println("Outside");Output:--> Output,但如果我尝试使用点(110,111),则输出--> Output。
我不知道发生了什么.:S
发布于 2013-04-10 22:39:49
问题出在测试中使用的正方形的定义中。顶点的顺序错误。改变第三个和第四个顶点的顺序,测试应该会起作用。
double xpoints[] = {100,100,200,200}; //Square
double ypoints[] = {100,200,200,100};https://stackoverflow.com/questions/15928851
复制相似问题