public void SplitAtJoin2()
        {
            var sortedVertices = new[]
            {
                new Vertex(0.0f, 0.0f), // 0 0
                new Vertex(1.0f, 0.5f), // 6 1
                new Vertex(1.5f, 1.0f), // 7 2
                new Vertex(2.0f, 0.0f), // 4 3
                new Vertex(3.0f, 0.5f), // 5 4
                new Vertex(3.5f, 0.0f), // 3 5
                new Vertex(4.0f, 4.0f), // 1 6
                new Vertex(5.0f, 0.0f), // 2 7
            };

            var polygon1 = Polygon.Build(sortedVertices)
                           .AddVertices(3, 0, 6, 7, 5)
                           .ClosePartialPolygon()
                           .AddVertices(4, 2, 1)
                           .Close();

            var triangleCollector = PolygonTriangulator.CreateTriangleCollector();

            var triangluator = new PolygonTriangulator(polygon1);
            var splits       = string.Join(" ", triangluator.GetSplits().OrderBy(x => x.Item1).ThenBy(x => x.Item2).Select(x => $"{x.Item1}-{x.Item2}"));

            Assert.AreEqual("0-1 1-3 3-4 4-5 5-6", splits);

            var specialSplits = new[] { (4, 5), (3, 4), (0, 1), (1, 3), (5, 6) }
Esempio n. 2
0
        private void triangulateButton_Click(object sender, EventArgs e)
        {
            this.vertexText.Text = string.Empty;
            var lines     = new List <string>();
            var collector = PolygonTriangulator.CreateTriangleCollector();

            try
            {
                var triangulator = new PolygonTriangulator(this.controller.Polygon);
                var splits       = triangulator.GetSplits();
                lines.Add("Splits");
                lines.AddRange(splits.Select(x => $"{x.Item1} - {x.Item2}"));
                lines.Add("");

                var monotones = Polygon.Split(this.controller.Polygon, splits, PolygonTriangulator.CreateTriangleCollector());
                lines.Add("Monotones");
                lines.AddRange(monotones.SubPolygonIds.Select(x => string.Join(" ", monotones.SubPolygonVertices(x))));
                lines.Add("");

                triangulator.BuildTriangles(collector);
            }
            catch (Exception ex)
            {
                this.vertexText.Text = ex.ToString();
            }

            var triangles = collector.Triangles;

            lines.Add("Triangles");
            for (int i = 0; i < triangles.Length; i += 3)
            {
                lines.Add($"{triangles[i + 0]} {triangles[i + 1]} {triangles[i + 2]} ");
            }

            this.vertexText.Text += string.Join(Environment.NewLine, lines);
            this.polygonPanel.AutoScale();
        }