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);
        }