public void MergeInsideCompletely()
 {
     Vector2[] points1 = Rectangle(0, 0, 5, 5);
     Vector2[] points2 = Rectangle(1, 2, 2, 2);
     IPoly poly1 = new ComplexPoly(points1, new [] { points2 });
     IPoly poly2 = new Poly(points2);
     IPoly merged = merge(poly1, poly2);
     Assert.AreEqual(0, merged.Holes.Length, "Hole should be gone");
     Assert.AreEqual(25, merged.Boundary.Area);
 }
        public void Holeception()
        {
            Vector2[] points       = { V2(0, 0), V2(10, 0), V2(10, 10), V2(0, 10) };
            Vector2[] lakePoints   = { V2(2, 2), V2(7, 2), V2(7, 8), V2(2, 8) };
            Vector2[] islandPoints = { V2(4, 4), V2(6, 4), V2(6, 7), V2(4, 7) };

            IPoly innerPoly = new ComplexPoly(lakePoints, new [] {islandPoints });
            IPoly poly      = new ComplexPoly(new Poly(points), new [] { innerPoly });

            Assert.False(poly.IsPointInside(V2(3, 3)), "In the lake");
            Assert.True(poly.IsPointInside(V2(5, 5)), "On the island");
        }
        public void IsPointInside()
        {
            Vector2[] points     = { V2(0, 0), V2(5, 0), V2(5, 8), V2(0, 8) };
            Vector2[] holePoints = { V2(2, 3), V2(4, 3), V2(4, 5), V2(2, 5) };

            IPoly poly = new ComplexPoly(points, new Vector2[][] { holePoints });

            Assert.AreEqual(1, poly.Holes.Length);
            Assert.True(poly.IsPointInside(V2(1, 1)),  "Inside");
            Assert.True(poly.IsPointInside(V2(0, 0)),  "Border");
            Assert.False(poly.IsPointInside(V2(3, 4)), "Inside hole");
            Assert.True(poly.IsPointInside(V2(2, 3)),  "Hole border");
            Assert.False(poly.IsPointInside(V2(2, 3), false),  "Hole border with ignoreborder");
        }
        public void IsNeighbor()
        {
            Vector2[] points1 = { V2(0, 0), V2(1, 0),  V2(1, 1),  V2(0, 1) };
            Vector2[] points2 = { V2(0, 0), V2(1, 0),  V2(1, -1), V2(0, -1) };
            Vector2[] points3 = { V2(0, 0), V2(1, -1), V2(0, -1) };
            Vector2[] points4 = { V2(5, 0), V2(6, 0),  V2(6, -1), V2(5, -1) };

            Vector2[] outlinePoints = { V2(-2, -3), V2(2, -3), V2(2, 1), V2(-2, 1) };
            Vector2[] holePoints    = { V2(-1, -2), V2(1, -2), V2(1, 0), V2(0, 0), V2(-1, 0) };

            IPoly poly1 = new Poly(points1);
            IPoly poly2 = new Poly(points2);
            IPoly poly3 = new Poly(points3);
            IPoly poly4 = new Poly(points4);
            IPoly poly5 = new ComplexPoly(outlinePoints, new []{ holePoints });

            Assert.True(poly1.IsNeighbor(poly2), "Simple neighbors");
            Assert.False(poly1.IsNeighbor(poly3), "Only one shared point");
            Assert.False(poly1.IsNeighbor(poly4), "Simple not neighbors");
            Assert.True(poly2.IsNeighbor(poly5), "Border with the hole");
        }
        public void PreserveHoles()
        {
            // Needs additional point (3, 0) so can't use `Rectangle`.
            Vector2[] points1 = { V2(0, 0), V2(3, 0), V2(5, 0), V2(5, 8), V2(0, 8) };
            Vector2[] holePoints = Rectangle(2, 3, 2, 2);

            Vector2[] points2 = Rectangle(0, -3, 3, 3);
            Vector2[] hole2Points = Rectangle(1, -2, 1, 1);

            IPoly poly1 = new ComplexPoly(points1, new [] { holePoints });
            IPoly poly2 = new ComplexPoly(points2, new [] { hole2Points });
            IPoly merged = merge(poly1, poly2);

            Assert.True(merged.IsPointInside(V2(1, 1)), "Inside");
            Assert.True(merged.IsPointInside(V2(1, -1)), "Inside merged");
            Assert.AreEqual(2, merged.Holes.Length);
            Assert.False(merged.IsPointInside(V2(3, 4)), "Inside hole");
            Assert.False(merged.IsPointInside(V2(1.5f, -1.5f)), "Inside 2nd hole");
        }
        public void MergeInsideDivider()
        {
            Vector2[] points1 = Rectangle(0, 0, 6, 6);
            Vector2[] hole    = { V2(1, 1), V2(5, 1), V2(5, 2), V2(5, 4), V2(5, 5), V2(1, 5), V2(1, 4), V2(1, 2) };
            Vector2[] points2 = Rectangle(1, 2, 4, 2);
            IPoly poly1 = new ComplexPoly(points1, new [] { hole });
            IPoly poly2 = new Poly(points2);
            IPoly merged = merge(poly1, poly2);

            Assert.AreEqual(2, merged.Holes.Length, "Another hole should be be created");
            Assert.False(merged.IsPointInside(V2(1.5f, 1.5f)), "First hole");
            Assert.False(merged.IsPointInside(V2(1.5f, 4.5f)), "2nd hole");
            Assert.True(merged.IsPointInside(V2(1, 3)), "Merged area");

            foreach (var mergedHole in merged.Holes)
            {
                Assert.AreEqual(new HashSet<Vector2>(mergedHole.Points).Count, mergedHole.Points.Length,
                    "Merged polygon should only have unique points");    
            }
        }
        public void MergeInside()
        {
            Vector2[] points1 = Rectangle(0, 0, 6, 6);
            Vector2[] hole    = { V2(1, 1), V2(5, 1), V2(5, 2), V2(5, 4), V2(5, 5), V2(1, 5) };
            Vector2[] points2 = Rectangle(3, 2, 2, 2);
            IPoly poly1 = new ComplexPoly(points1, new [] { hole });
            IPoly poly2 = new Poly(points2);

            IPoly merged = merge(poly1, poly2);

            Assert.True(merged.IsPointInside(V2(0, 0)), "Border");
            Assert.True(merged.IsPointInside(V2(3, 2)), "Inner border");
            Assert.False(merged.IsPointInside(V2(2, 2)), "Inside hole");
        }