如何比较LibGDX PolygonShapes?

时间:2016-04-23 17:45:41

标签: libgdx box2d equals

如何检查一个PolygonShape是否与另一个相同? .equals()不起作用。此代码不会打印"等于"。

PolygonShape test1 = new PolygonShape();
PolygonShape test2 = new PolygonShape();

test1.setAsBox(10, 20, new Vector2(0,0), 0);
test2.setAsBox(10, 20, new Vector2(0,0), 0);

if (test1.equals(test2))System.out.println("equals");

1 个答案:

答案 0 :(得分:0)

iv_canvas.setImageBitmap(baseBitmap); 课程没有预定义的equals功能,但编写自己的功能并不难。如下:

PolygonShape

注意:这只有在顶点排序相同的情况下才有效。可能的情况是,相同的顶点在两个不同的多边形中,但是通过某个索引移位。但是,由于您的示例仅使用boolean polygonEqual(PolygonShape a, PolygonShape b) { int n = a.getVertexCount(); //If the two shapes have differing numbers of vertices they can't be equal if (n != b.getVertexCount()) return false; //Check if all the vertices are equal Vector vA, vB; for (int i = 0; i < n; i++) { a.getVertex(i, vA); b.getVertex(i, vB); if (!a.equals(b)) return false; } //Now they must be equal return true; } ,因此此功能应足以作为比较。