Пример #1
0
        public void DoIntersect()
        {
            Polygon2f A = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1));
            Polygon2f B = CreatePolygon2.FromBox(new Vector2f(0), new Vector2f(2));

            Polygon2f C = CreatePolygon2.FromBox(new Vector2f(-1, 1), new Vector2f(1, 3));
            Polygon2f D = CreatePolygon2.FromBox(new Vector2f(-3, -1), new Vector2f(-2, 1));

            //Intersect
            Assert.IsTrue(PolygonBoolean2.DoIntersect(A, B));
            //Edge case
            Assert.IsFalse(PolygonBoolean2.DoIntersect(A, C));
            //Dont intersect
            Assert.IsFalse(PolygonBoolean2.DoIntersect(A, D));
        }
Пример #2
0
        public void Triangulate()
        {
            Polygon2f polygon = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1));

            Mesh2f mesh = ConstraintedTriangulation2.Triangulate(polygon);

            Assert.AreEqual(4, mesh.VerticesCount);
            Assert.AreEqual(6, mesh.IndicesCount);

            for (int i = 0; i < mesh.IndicesCount / 3; i++)
            {
                Vector2f   a   = mesh.Positions[mesh.Indices[i * 3 + 0]];
                Vector2f   b   = mesh.Positions[mesh.Indices[i * 3 + 1]];
                Vector2f   c   = mesh.Positions[mesh.Indices[i * 3 + 2]];
                Triangle2f tri = new Triangle2f(a, b, c);

                Assert.IsTrue(tri.SignedArea > 0);
            }
        }
Пример #3
0
        protected virtual void Update()
        {
            if (Input.GetKeyDown(KeyCode.F1))
            {
                ResetInput();
                MadePolygon = true;
                OnPolygonComplete(CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1)));
            }
            else if (Input.GetKeyDown(KeyCode.F2))
            {
                ResetInput();
                Polygon2f box  = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1));
                Polygon2f hole = CreatePolygon2.FromBox(new Vector2f(-0.5f), new Vector2f(0.5f));
                hole.MakeCW();
                box.AddHole(hole);
                MadePolygon = true;
                OnPolygonComplete(box);
            }
            else if (Input.GetKeyDown(KeyCode.F3))
            {
                ResetInput();
                Polygon2f circle = CreatePolygon2.FromCircle(new Vector2f(-1), 1, 16);
                MadePolygon = true;
                OnPolygonComplete(circle);
            }
            else if (Input.GetKeyDown(KeyCode.F4))
            {
                ResetInput();
                Polygon2f circle = CreatePolygon2.FromCircle(new Vector2f(0), 1, 16);
                Polygon2f hole   = CreatePolygon2.FromCircle(new Vector2f(0), 0.5f, 16);
                hole.MakeCW();
                circle.AddHole(hole);
                MadePolygon = true;
                OnPolygonComplete(circle);
            }
            else if (Input.GetKeyDown(KeyCode.F5))
            {
                ResetInput();
                Polygon2f capsule = CreatePolygon2.FromCapsule(new Vector2f(0), 1, 1, 16);
                MadePolygon = true;
                OnPolygonComplete(capsule);
            }
            else if (Input.GetKeyDown(KeyCode.F6))
            {
                ResetInput();
                Polygon2f cathedral = CreatePolygon2.FromCapsule(new Vector2f(0), 1, 1, 16);

                for (float x = -0.5f; x <= 0.5f; x += 1.0f)
                {
                    for (float y = -0.75f; y <= 0.75f; y += 0.5f)
                    {
                        Polygon2f pillar = CreatePolygon2.FromCircle(new Vector2f(x, y), 0.1f, 16);
                        pillar.MakeCW();
                        cathedral.AddHole(pillar);
                    }
                }

                MadePolygon = true;
                OnPolygonComplete(cathedral);
            }
            else
            {
                bool leftMouseClicked = Input.GetMouseButtonDown(0);

                if (leftMouseClicked)
                {
                    Vector2f point = GetMousePosition();
                    OnLeftClick(point);
                }

                if (Input.GetKeyDown(KeyCode.Space))
                {
                    ResetInput();
                    OnPolygonCleared();
                }
                else if (!MadePolygon)
                {
                    Vector2f point = GetMousePosition();
                    point = SnapToPolygon(point);

                    if (leftMouseClicked)
                    {
                        if (Points == null)
                        {
                            CreatePoints();
                            AddPoint(point);
                            AddPoint(point);
                        }
                        else
                        {
                            if (PolygonClosed())
                            {
                                ClosePolygon();
                                MadePolygon = true;

                                OnPolygonComplete(new Polygon2f(Points.ToArray()));
                            }
                            else
                            {
                                AddPoint(point);
                            }
                        }
                    }
                    else
                    {
                        MoveLastPoint(point);
                    }
                }
            }
        }