查找2个多边形是否使用剪切库相交

在意识到我的代码在检查多边形重叠方面效率很低之后,我已经转向了一个提到的库Clipper,但是发现很难知道这个操作使用了它。

我使用统一Vector2 []为我的形状描述:

//Square Vector2[] square = new Vector2[] { new Vector2(0,0), new Vector2(1,0), new Vector2(1,1), new Vector2(0,1) }; 

在这里输入图像说明

我想能够做上面的两个形状的布尔比较。 如果有人知道Clipper,我可以用什么方法来做这个检查?

我最近尝试了以下方法,但即使testing形状完全相互分离,结果也是如此:

 public bool CheckForOverLaps(Vector2[] shape1, Vector2[] shape2) { List<IntPoint> shape1IntPoints = Vector2ArrayToListIntPoint (shape1); List<IntPoint> shape2IntPoints = Vector2ArrayToListIntPoint (shape2); List<List<IntPoint>> solution = new List<List<IntPoint>> (); c.AddPath (shape1IntPoints, PolyType.ptSubject, true); c.AddPath (shape2IntPoints, PolyType.ptClip, true); c.Execute (ClipType.ctIntersection, solution, PolyFillType.pftNonZero, PolyFillType.pftNonZero); return solution.Count != 0; } private List<IntPoint> Vector2ArrayToListIntPoint (Vector2[] shape) { List<IntPoint> list = new List<IntPoint> (); foreach (Vector2 v in shape) { list.Add (new IntPoint (vx, vy)); } return list; } 

而testing:

  //Square Vector2[] square = new Vector2[] { new Vector2(0,0), new Vector2(2,0), new Vector2(2,2), new Vector2(0,2) }; //Square Vector2[] square2 = new Vector2[] { new Vector2 (1, 1), new Vector2 (3, 1), new Vector2 (3, 3), new Vector2 (1, 3) }; //Square Vector2[] square3 = new Vector2[] { new Vector2 (2, 0), new Vector2 (4, 0), new Vector2 (4, 2), new Vector2 (2, 2) }; //Square Vector2[] square4 = new Vector2[] { new Vector2 (4, 0), new Vector2 (6, 0), new Vector2 (6, 2), new Vector2 (4, 2) }; Assert.IsTrue(tc.CheckForOverLaps(square,square2)); Assert.IsFalse(tc.CheckForOverLaps(square,square3)); Assert.IsFalse(tc.CheckForOverLaps(square,square4)); 

非常感谢。

我最终得到了这个..我不得不清除()出剪刀下一个testing工作。 把所有东西都转换成IntPoint来提高效率。

 public bool CheckForOverLaps(List<IntPoint> shape1, List<IntPoint> shape2) { clipper.Clear(); solution.Clear (); clipper.AddPath (shape1, PolyType.ptSubject, true); clipper.AddPath (shape2, PolyType.ptClip, true); clipper.Execute (ClipType.ctIntersection, solution, PolyFillType.pftNonZero, PolyFillType.pftNonZero); return solution.Count != 0; }