public void TestIntersectionWithPolylineComplex() { // polygon can intersect in no more than two points with a polyline (if polyline is polygon) // polygon to intersect with Polygon3D s = new Polygon3D(new List <Point3D> { s1_1, s1_2, s1_3, s1_4 }, "s"); // this is our base polyline which we will be moving left and up to test intersection with square Point3D p1 = new Point3D(-1.5, 0.5, 1); Point3D p2 = new Point3D(-1.5, 0.5, -1); Point3D p3 = new Point3D(0.5, -1.5, -1); Point3D p4 = new Point3D(0.5, -1.5, 1); List <Point3D> l = new List <Point3D> { p1, p2, p3, p4 }; Polygon3D pl = new Polygon3D(l, "pl"); // test base => no intersection HashSet <Point3D> res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // test base shift left (x+0.5) and up (y+0.5) => on intersection at (0,0,0) which is not counted for (int i = 0; i < l.Count; i++) { l[i] += 0.5 * uX; l[i] += 0.5 * uY; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(0, res.Count); // test base shift left (x+0.5) and up (y+0.5) => intersection at (0,1,0) & (1,0,0) which is counted for (int i = 0; i < l.Count; i++) { l[i] += 0.5 * uX; l[i] += 0.5 * uY; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 2); // check the points Assert.IsTrue(res.Contains(new Point3D(0, 1, 0))); Assert.IsTrue(res.Contains(new Point3D(1, 0, 0))); // test base shift left (x+0.5) and up (y+0.5) => intersection at (0,2,0) & (2,0,0) which is counted for (int i = 0; i < l.Count; i++) { l[i] += 0.5 * uX; l[i] += 0.5 * uY; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 2); // check the points Assert.IsTrue(res.Contains(new Point3D(0, 2, 0))); Assert.IsTrue(res.Contains(new Point3D(2, 0, 0))); // test base shift left (x+0.5) and up (y+0.5) => intersection at (0.5,2.5,0) & (2.5,0.5,0) which is counted for (int i = 0; i < l.Count; i++) { l[i] += 0.5 * uX; l[i] += 0.5 * uY; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 2); // check the points Assert.IsTrue(res.Contains(new Point3D(0.5, 2.5, 0))); Assert.IsTrue(res.Contains(new Point3D(2.5, 0.5, 0))); }
public void TestIntersectionWithPolyline() { // polygon can intersect in no more than two points with a polyline (if polyline is polygon) // polygon to intersect with Polygon3D s = new Polygon3D(new List <Point3D> { s1_1, s1_2, s1_3, s1_4 }, "s"); // this is our base polyline which we will be moving left and up to test intersection with square Point3D p1 = new Point3D(1, -1, 1); Point3D p2 = new Point3D(1, -1, -1); Point3D p3 = new Point3D(2, -1, -1); Point3D p4 = new Point3D(2, -1, 1); List <Point3D> l = new List <Point3D> { p1, p2, p3, p4 }; Polygon3D pl = new Polygon3D(l, "pl"); // test base => no intersection HashSet <Point3D> res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // test base shift left (x-1) => no intersection for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // test base shift left (x-1, 2 in total from base) => no intersection for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // test base shift left (x-1, 3 in total from base) => no intersection for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // move base back to original position and move up by one l[0] = p1 + uY; l[1] = p2 + uY; l[2] = p3 + uY; l[3] = p4 + uY; // test base position whereby base has moved Y+1 pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // intersects with two points on the edges of square but this is not counted // test base shift left (x-1) => two intersections on the edge of the square for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // test base shift left (x-1, 2 in total from base) => one intersection on the edge of the square for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // test base shift left (x-1, 3 in total from base) => no intersection for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // move base back to original position and move up by two l[0] = p1 + (2 * uY); l[1] = p2 + (2 * uY); l[2] = p3 + (2 * uY); l[3] = p4 + (2 * uY); // test base position whereby base has moved Y+2 pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 2); // check the points Assert.IsTrue(res.Contains(new Point3D(2, 1, 0))); Assert.IsTrue(res.Contains(new Point3D(1, 1, 0))); // test base shift left (x-1) => two intersections (one on the edge [NOW counted] and one in the middle) for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 2); // check the points Assert.IsTrue(res.Contains(new Point3D(0, 1, 0))); Assert.IsTrue(res.Contains(new Point3D(1, 1, 0))); // test base shift left (x-1 two in total) => one intersection one on the edge [not counted] for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); // test base shift left (x-1 three in total) => no intersection for (int i = 0; i < l.Count; i++) { l[i] -= uX; } pl = new Polygon3D(l, "pl"); res = s.IntersectionsWith(pl); Assert.AreEqual(res.Count, 0); }