Esempio n. 1
0
        private static void PolygonTests()
        {
            double pi = Math.PI;
            List<MeshNode> nodes = new List<MeshNode> { new MeshNode(0, 0), new MeshNode(10, 0), new MeshNode(10, 10), new MeshNode(0, 10) };

            Polygon polygon = new Polygon(nodes);
            polygon.ImposeOwnership();
            List<Edge> edges = polygon.GetEdges();
            Debug.Assert(HelperMethods.Circa(
                HelperMethods.GetAngleDifference(edges[0].ToVector(), edges[1].ToVector()), pi / 2));
            Debug.Assert(Polygon.IsConcave(polygon.GetEdges()), "CodeTest.PolygonTests(): Polygon not concave!");
            Debug.Assert(polygon.PointInPolygon(new Vector2(10, 5), false), "CodeTest.PolygonTests(): 10,5 not in Polygon!");

            Random r = new Random();
            Vector2 point1 = new Vector2((float)(10 * r.NextDouble()), (float)(10 * r.NextDouble()));
            Debug.Assert(polygon.PointInPolygon(point1, false), "CodeTest.PolygonTests(): Random point not in Polygon!");

            Vector2 p1 = new Vector2(0, 0);
            Vector2 p2 = new Vector2(0, 10);
            Vector2 p3 = new Vector2(10, 0);
            Vector2 p4 = new Vector2(10, 10);
            Debug.Assert(polygon.PointInPolygon(p1, false), "PointInPolygon Test failed!");
            Debug.Assert(polygon.PointInPolygon(p2, false), "PointInPolygon Test failed!");
            Debug.Assert(polygon.PointInPolygon(p3, false), "PointInPolygon Test failed!");
            Debug.Assert(polygon.PointInPolygon(p4, false), "PointInPolygon Test failed!");

            List<MeshNode> nodes2 = new List<MeshNode> { nodes[1], new MeshNode(20, 0), new MeshNode(20, 10), nodes[2] };
            Polygon polygon2 = new Polygon(nodes2);
            polygon2.ImposeOwnership();
            List<Polygon> polygonNeighbours = polygon.GetAdjacentPolygons();
            List<Polygon> polygon2Neighbours = polygon2.GetAdjacentPolygons();

            Debug.Assert(polygonNeighbours.Count == 1 && polygonNeighbours.Contains(polygon2));
            Debug.Assert(polygon2Neighbours.Count == 1 && polygon2Neighbours.Contains(polygon));
            Edge commonEdge, commonEdge2;
            List<Edge> unitedEdges = polygon.GetUnion(polygon2, out commonEdge, out commonEdge2);
            Polygon unitedPolygon = new Polygon(unitedEdges);
            unitedPolygon.ImposeOwnership();
            polygon.Delete(true);
            polygon2.Delete(true);
            unitedPolygon.CleanUp(true);
            Debug.Assert(commonEdge.mStartNode == null);
            Debug.Assert(commonEdge.mEndNode == null);
            Debug.Assert(commonEdge2.mStartNode == null);
            Debug.Assert(commonEdge2.mEndNode == null);
            Debug.Assert(unitedPolygon.EdgesHaveOneOwner());
            Debug.Assert(unitedPolygon.NodesEdgesOwnerTest());
            Debug.WriteLine("Use breakpoint to check the last union test.");

            Debug.WriteLine("Polygon tests done.");
            unitedPolygon.Delete(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Unites two given polygons into one and returns it. The two polygons
        /// are deleted. Optionally applies a cleanup to the new polygon.
        /// </summary>
        /// <param name="unionEdges">the edges of the new united polygon</param>
        /// <param name="polygon1">the first of the polygons that are to be united</param>
        /// <param name="polygon2">the second of the polygons that are to be united</param>
        /// <param name="allowCleanUp">if the edges of the united polygon should
        ///  be cleaned up</param>
        /// <returns>the new united polygon</returns>
        private Polygon ApplyPolygonUnion(List<Edge> unionEdges, Polygon polygon1, Polygon polygon2, bool allowCleanUp)
        {
            Debug.Assert(! polygon1.CanBeCleanedUp(), "Cannot unite with a dirty polygon!");
            Debug.Assert(!polygon2.CanBeCleanedUp(), "Cannot unite with a dirty polygon!");
            Polygon unionPolygon = new Polygon(unionEdges);
            unionPolygon.ImposeOwnership();

            polygon1.Delete(true);
            RemovePolygon(polygon1);

            polygon2.Delete(true);
            RemovePolygon(polygon2);

            AddPolygon(unionPolygon);

            if (allowCleanUp) unionPolygon.CleanUp(true);
            return unionPolygon;
        }
Esempio n. 3
0
        private static void HelperMethodsTest()
        {
            // Angle:
            Debug.Assert(HelperMethods.Circa(
            HelperMethods.GetAngleDifference(new Vector2(1, 0), new Vector2(0, -1)), -Math.PI / 2));

            // PointOnLine:
            Debug.Assert(HelperMethods.PointOnLine(new Vector2(1, 1), new Vector2(0, 0), new Vector2(2, 2)));
            Debug.Assert(HelperMethods.PointOnLine(new Vector2(1, 1), new Vector2(2, 2), new Vector2(0, 0)));
            Debug.Assert(HelperMethods.PointOnLine(new Vector2(1, 1), new Vector2(2, 0), new Vector2(0, 2)));
            Debug.Assert(!HelperMethods.PointOnLine(new Vector2(2, 0), new Vector2(1, 1), new Vector2(0, 2)));
            Debug.Assert(HelperMethods.PointOnLine(new Vector2(1, 1), new Vector2(1, 1), new Vector2(0, 0)));
            Debug.Assert(HelperMethods.PointOnLine(new Vector2(0, 2), new Vector2(0, 0), new Vector2(0, 2)));
            Debug.Assert(HelperMethods.PointOnLine(new Vector2(0, 0), new Vector2(0, 2), new Vector2(0, 0)));

            // PointInPolygon:
            // These points are all inside:
            Polygon polygon = new Polygon(new List<MeshNode> { new MeshNode(0, 0), new MeshNode(2, 0), new MeshNode(2, 2), new MeshNode(1, 2), new MeshNode(0, 2), new MeshNode(0, 1) });
            polygon.ImposeOwnership();
            foreach (MeshNode node in polygon.GetNodes())
            {
                Debug.Assert(polygon.PointInPolygon(node.mVector, false));
            }
            Debug.Assert(polygon.PointInPolygon(new Vector2(1, 0), false));
            Debug.Assert(polygon.PointInPolygon(new Vector2(1, 1), false));
            Debug.Assert(polygon.PointInPolygon(new Vector2(2, 1), false));
            // Now for some points outside:
            Debug.Assert(!polygon.PointInPolygon(new Vector2(0, -1), false));
            Debug.Assert(!polygon.PointInPolygon(new Vector2(1, -1), false));
            Debug.Assert(!polygon.PointInPolygon(new Vector2(2, -1), false));
            Debug.Assert(!polygon.PointInPolygon(new Vector2(0, 3), false));
            Debug.Assert(!polygon.PointInPolygon(new Vector2(1, 3), false));
            Debug.Assert(!polygon.PointInPolygon(new Vector2(2, 3), false));

            polygon.Delete(true);
        }