コード例 #1
0
ファイル: Intersect.cs プロジェクト: biorpg/RT.Util
        /// <summary>Returns a polygon formed by intersecting an arbitrary polygon with a convex polygon.</summary>
        public static PolygonD PolygonWithConvexPolygon(PolygonD mainPoly, PolygonD clipPoly)
        {
            if (mainPoly.Vertices.Count <= 2 || clipPoly.Vertices.Count <= 2)
            {
                throw new InvalidOperationException("One of the polygons has 2 vertices or fewer.");
            }
            var result         = new List <PointD>();
            var resultVertices = mainPoly.Vertices.ToList();

            foreach (var clipEdge in clipPoly.ToEdges())
            {
                var newVertices = new List <PointD>();
                var vertexStart = resultVertices[resultVertices.Count - 1];
                foreach (var vertexEnd in resultVertices)
                {
                    if (clipEdge.CrossZ(vertexStart) > 0)
                    {
                        if (clipEdge.CrossZ(vertexEnd) > 0)
                        {
                            newVertices.Add(vertexEnd);
                        }
                        else
                        {
                            newVertices.Add(Intersect.LineWithLine(clipEdge, new EdgeD(vertexStart, vertexEnd)));
                        }
                    }
                    else
                    {
                        if (clipEdge.CrossZ(vertexEnd) > 0)
                        {
                            newVertices.Add(Intersect.LineWithLine(clipEdge, new EdgeD(vertexStart, vertexEnd)));
                            newVertices.Add(vertexEnd);
                        }
                    }
                    vertexStart = vertexEnd;
                }
                resultVertices = newVertices;
            }
            return(new PolygonD(resultVertices));
        }
コード例 #2
0
ファイル: Intersect.cs プロジェクト: RT-Projects/RT.Util
 /// <summary>Returns a polygon formed by intersecting an arbitrary polygon with a convex polygon.</summary>
 public static PolygonD PolygonWithConvexPolygon(PolygonD mainPoly, PolygonD clipPoly)
 {
     if (mainPoly.Vertices.Count <= 2 || clipPoly.Vertices.Count <= 2)
         throw new InvalidOperationException("One of the polygons has 2 vertices or fewer.");
     var result = new List<PointD>();
     var resultVertices = mainPoly.Vertices.ToList();
     foreach (var clipEdge in clipPoly.ToEdges())
     {
         var newVertices = new List<PointD>();
         var vertexStart = resultVertices[resultVertices.Count - 1];
         foreach (var vertexEnd in resultVertices)
         {
             if (clipEdge.CrossZ(vertexStart) > 0)
             {
                 if (clipEdge.CrossZ(vertexEnd) > 0)
                     newVertices.Add(vertexEnd);
                 else
                     newVertices.Add(Intersect.LineWithLine(clipEdge, new EdgeD(vertexStart, vertexEnd)));
             }
             else
             {
                 if (clipEdge.CrossZ(vertexEnd) > 0)
                 {
                     newVertices.Add(Intersect.LineWithLine(clipEdge, new EdgeD(vertexStart, vertexEnd)));
                     newVertices.Add(vertexEnd);
                 }
             }
             vertexStart = vertexEnd;
         }
         resultVertices = newVertices;
     }
     return new PolygonD(resultVertices);
 }