Пример #1
0
        static void CreatePolygons(VertexStore a, List <IntPolygon> allPolys)
        {
            IntPolygon currentPoly = null;
            VertexData last = new VertexData();
            VertexData first = new VertexData();
            bool       addedFirst = false;
            double     x, y;

            int       index = 0;
            VertexCmd cmd;

            while ((cmd = a.GetVertex(index++, out x, out y)) != VertexCmd.NoMore)
            {
                if (cmd == VertexCmd.LineTo)
                {
                    if (currentPoly == null)
                    {
                        currentPoly = new IntPolygon();
                        allPolys.Add(currentPoly);
                    }
                    //
                    if (!addedFirst)
                    {
                        currentPoly.Add(new IntPoint((long)(last.x * 1000), (long)(last.y * 1000)));
                        addedFirst = true;
                        first      = last;
                    }
                    currentPoly.Add(new IntPoint((long)(x * 1000), (long)(y * 1000)));
                    last = new VertexData(cmd, x, y);
                }
                else
                {
                    addedFirst  = false;
                    currentPoly = new IntPolygon();
                    allPolys.Add(currentPoly);
                    if (cmd == VertexCmd.MoveTo)
                    {
                        last = new VertexData(cmd, x, y);
                    }
                    else
                    {
                        last = first;
                    }
                }
            }
        }
Пример #2
0
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                segment1 += IntVertex2.left;
                segment3 += IntVertex2.left;
                polygon1 += IntVertex2.left;
            }
            if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                segment1 += IntVertex2.right;
                segment3 += IntVertex2.right;
                polygon1 += IntVertex2.right;
            }
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                segment1 += IntVertex2.up;
                segment3 += IntVertex2.up;
                polygon1 += IntVertex2.up;
            }
            if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                segment1 += IntVertex2.down;
                segment3 += IntVertex2.down;
                polygon1 += IntVertex2.down;
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                segment2I += IntVertex2.left;
                segment4  += IntVertex2.left;
                polygon2  += IntVertex2.left;
            }
            if (Input.GetKeyDown(KeyCode.D))
            {
                segment2I += IntVertex2.right;
                segment4  += IntVertex2.right;
                polygon2  += IntVertex2.right;
            }
            if (Input.GetKeyDown(KeyCode.W))
            {
                segment2I += IntVertex2.up;
                segment4  += IntVertex2.up;
                polygon2  += IntVertex2.up;
            }
            if (Input.GetKeyDown(KeyCode.S))
            {
                segment2I += IntVertex2.down;
                segment4  += IntVertex2.down;
                polygon2  += IntVertex2.down;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                switch (counter)
                {
                case 0:
                    polygon1 = new IntPolygon
                    {
                        new IntVertex2(30, 30),
                        new IntVertex2(20, 35),
                        new IntVertex2(30, 40),
                        new IntVertex2(40, 35)
                    };
                    polygon2 = new IntPolygon
                    {
                        new IntVertex2(30, 30),
                        new IntVertex2(22, 34),
                        new IntVertex2(30, 38),
                        new IntVertex2(38, 34)
                    };
                    counter++;
                    break;

                case 1:
                    polygon1 = new IntPolygon
                    {
                        new IntVertex2(30, 30),
                        new IntVertex2(30, 40),
                        new IntVertex2(40, 40),
                        new IntVertex2(40, 30)
                    };
                    polygon2 = new IntPolygon
                    {
                        new IntVertex2(30, 30),
                        new IntVertex2(35, 30),
                        new IntVertex2(35, 35),
                        new IntVertex2(30, 35)
                    };
                    counter = 0;
                    break;
                }
            }

            texture.Clear();

            //IntSegment2 intersection;
            //if (segment1.Intersects(segment2, out intersection))
            //{
            //    texture.DrawLine(segment1, Color.red);
            //    texture.DrawLine(segment2, Color.red);
            //    texture.DrawLine(intersection, RandomE.colorHSV);
            //}
            //else
            //{
            //    texture.DrawLine(segment1, Color.green);
            //    texture.DrawLine(segment2, Color.green);
            //}

            //if (segment3.Intersects(segment4, out intersection))
            //{
            //    texture.DrawLine(segment3, Color.red);
            //    texture.DrawLine(segment4, Color.red);
            //    texture.DrawLine(intersection, RandomE.colorHSV);
            //}
            //else
            //{
            //    texture.DrawLine(segment3, Color.green);
            //    texture.DrawLine(segment4, Color.green);
            //}

            List <IntSegment2> intersections;

            if (polygon1.Intersects(polygon2, out intersections))
            {
                texture.DrawGradientIntPolygon(polygon1, Color.blue, Color.red);
                texture.DrawGradientIntPolygon(polygon2, Color.blue, Color.red);
                foreach (var i in intersections)
                {
                    texture.DrawLine(i, RandomE.colorHSV);
                }
            }
            else
            {
                texture.DrawGradientIntPolygon(polygon1, Color.blue, Color.green);
                texture.DrawGradientIntPolygon(polygon2, Color.blue, Color.green);
            }
        }