示例#1
0
 private void DeletePolygon(Polygon polygon)
 {
     polygons.Remove(polygon);
 }
示例#2
0
        public void ClipPolygon(Polygon polygon)
        {
            Vertex v;

            List <Vertex>[] verticesAddToThis    = new List <Vertex> [Vertices.Count];
            List <Vertex>[] verticesAddToPolygon = new List <Vertex> [polygon.Vertices.Count];
            for (int i = 0; i < polygon.Vertices.Count; i++)
            {
                verticesAddToPolygon[i] = new List <Vertex>();
            }
            for (int i = 0; i < Vertices.Count; i++)
            {
                verticesAddToThis[i] = new List <Vertex>();
                for (int j = 0; j < polygon.Vertices.Count; j++)
                {
                    if ((v = this.GetIntersectionPoint(Vertices[i], Vertices[(i + 1) % Vertices.Count], polygon.Vertices[j], polygon.Vertices[(j + 1) % polygon.Vertices.Count])) != null)
                    {
                        verticesAddToThis[i].Add(v);
                        verticesAddToPolygon[j].Add(v);
                    }
                }
            }
            List <Vertex> intersections = new List <Vertex>();
            bool          outVertex     = false;

            for (int i = 0, j = 0; j < Vertices.Count; i++, j++)
            {
                verticesAddToThis[i].Sort((v1, v2) =>
                {
                    double l1 = this.GetLineLength(Vertices[j], v1);
                    double l2 = this.GetLineLength(Vertices[j], v2);
                    if (l1 < l2)
                    {
                        return(-1);
                    }
                    if (l1 == l2)
                    {
                        return(0);
                    }
                    return(1);
                });
                foreach (Vertex ve in verticesAddToThis[i])
                {
                    j++;
                    Vertices.Insert(j, ve);
                    if (outVertex)
                    {
                        intersections.Add(ve);
                        outVertex = false;
                    }
                    else
                    {
                        outVertex = true;
                    }
                }
            }
            for (int i = 0, j = 0; j < polygon.Vertices.Count; i++, j++)
            {
                verticesAddToPolygon[i].Sort((v1, v2) =>
                {
                    double l1 = this.GetLineLength(polygon.Vertices[j], v1);
                    double l2 = this.GetLineLength(polygon.Vertices[j], v2);
                    if (l1 < l2)
                    {
                        return(-1);
                    }
                    if (l1 == l2)
                    {
                        return(0);
                    }
                    return(1);
                });
                polygon.Vertices.InsertRange(j + 1, verticesAddToPolygon[i]);
                j += verticesAddToPolygon[i].Count;
            }
            List <Vertex> newVertices = new List <Vertex>();

            while (intersections.Count > 0)
            {
                int    index    = Vertices.FindIndex(ve => ve == intersections[0]);
                Vertex vertex   = Vertices[index];
                bool   thisList = true;
                newVertices.Add(vertex);

                while (true)
                {
                    if (thisList)
                    {
                        index  = (index + 1) % Vertices.Count;
                        vertex = Vertices[index];
                        if (vertex.Intersection)
                        {
                            if (vertex == intersections[0])
                            {
                                break;
                            }
                            intersections.Remove(vertex);
                            thisList = false;
                            index    = polygon.Vertices.FindIndex(ve => ve == vertex);
                        }
                    }
                    else
                    {
                        index  = (index + 1) % polygon.Vertices.Count;
                        vertex = polygon.Vertices[index];
                        if (vertex.Intersection)
                        {
                            if (vertex == intersections[0])
                            {
                                break;
                            }
                            intersections.Remove(vertex);
                            thisList = true;
                            index    = Vertices.FindIndex(ve => ve == vertex);
                        }
                    }
                    newVertices.Add(vertex);
                }
                intersections.RemoveAt(0);
            }
            Vertices = newVertices;
        }