コード例 #1
0
        /// <summary>
        /// Calculates the intersection between a polygon and a line.
        /// </summary>
        /// <param name="subjectPoly">An Array of polygon points.</param>
        /// <param name="linePoints">Two points that form a line.</param>
        /// <returns>Returns an Array of polygon points.</returns>
        public static Vector2[] GetIntersectedPolygon(Vector2[] subjectPoly, Vector2[] linePoints, out bool intersecting)
        {
            List<Vector2> outputList = subjectPoly.ToList();
            intersecting = true;

            Edge clipEdge = new Edge(linePoints[0], linePoints[1]);

            List<Vector2> inputList = outputList.ToList();
            outputList.Clear();

            Vector2 lastVert = inputList[inputList.Count - 1];
            int len = inputList.Count;

            for (int i = 0; i < len; i++)
            {
                Vector2 vert = inputList[i];

                if (IsInside(clipEdge, vert))
                {
                    if (!IsInside(clipEdge, lastVert))
                    {
                        Vector2? point = GetIntersect(lastVert, vert, clipEdge.From, clipEdge.To);
                        if (point != null)
                        {
                            outputList.Add(point.Value);
                        }
                    }

                    outputList.Add(vert);
                }
                else if (IsInside(clipEdge, lastVert))
                {
                    Vector2? point = GetIntersect(lastVert, vert, clipEdge.From, clipEdge.To);
                    if (point != null)
                    {
                        outputList.Add(point.Value);
                    }
                }

                lastVert = vert;
            }

            if (outputList.Count == 0)
            {
                intersecting = false;
            }
            //	Exit Function
            return outputList.ToArray();
        }
コード例 #2
0
 public void SetNewPath(Vector2[] path)
 {
     Path = path.ToList();
       ConvertToPath();
       currentPoint = 0;
 }