コード例 #1
0
        /// <summary>
        /// This clips the subject polygon against the clip polygon (gets the intersection of the two polygons)
        /// </summary>
        /// <remarks>
        /// Based on the psuedocode from:
        /// http://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman
        /// </remarks>
        /// <param name="subjectPoly">Can be concave or convex</param>
        /// <param name="clipPoly">Must be convex</param>
        /// <returns>The intersection of the two polygons (or null)</returns>
        public static Vector2[] GetIntersectedPolygon(Vector2[] subjectPoly, Vector2[] clipPoly)
        {
            if (subjectPoly.Length < 3 || clipPoly.Length < 3)
            {
                throw new ArgumentException(string.Format("The polygons passed in must have at least 3 Vector2s: subject={0}, clip={1}", subjectPoly.Length.ToString(), clipPoly.Length.ToString()));
            }

            List<Vector2> outputList = subjectPoly.ToList();

            //	Make sure it's clockwise
            if (!IsClockwise(subjectPoly))
            {
                outputList.Reverse();
            }

            //	Walk around the clip polygon clockwise
            foreach (Edge clipEdge in IterateEdgesClockwise(clipPoly))
            {
                List<Vector2> inputList = outputList.ToList();		//	clone it
                outputList.Clear();

                if (inputList.Count == 0)
                {
                    //	Sometimes when the polygons don't intersect, this list goes to zero.  Jump out to avoid an index out of range exception
                    break;
                }

                Vector2 S = inputList[inputList.Count - 1];

                foreach (Vector2 E in inputList)
                {
                    if (IsInside(clipEdge, E))
                    {
                        if (!IsInside(clipEdge, S))
                        {
                            Vector2? Vector2 = GetIntersect(S, E, clipEdge.From, clipEdge.To);
                            if (Vector2 == null)
                            {
                                throw new ApplicationException("Line segments don't intersect");		//	may be colinear, or may be a bug
                            }
                            else
                            {
                                outputList.Add(Vector2.Value);
                            }
                        }

                        outputList.Add(E);
                    }
                    else if (IsInside(clipEdge, S))
                    {
                        Vector2? Vector2 = GetIntersect(S, E, clipEdge.From, clipEdge.To);
                        if (Vector2 == null)
                        {
                            throw new ApplicationException("Line segments don't intersect");		//	may be colinear, or may be a bug
                        }
                        else
                        {
                            outputList.Add(Vector2.Value);
                        }
                    }

                    S = E;
                }
            }

            //	Exit Function
            return outputList.ToArray();
        }
コード例 #2
0
 public List<Vector2> GetProfileBaseDots()
 {
     var result = new Vector2[7];
     foreach (var rect in ProfileRects)
     {
         Vector2 a = rect.Points[0], b = rect.Points.Last();
         var list = linkedPoints[rect.A];
         a.Y = 0.0f;
         foreach (var l in list)
             a.Y += ShapeInfo.Points[l].Value.Y;
         a.Y /= list.Count;
         result[rect.A] = a;
         list = linkedPoints[rect.B];
         b.Y = 0.0f;
         foreach (var l in list)
             b.Y += ShapeInfo.Points[l].Value.Y;
         b.Y /= list.Count;
         result[rect.B] = a;
     }
     return result.ToList();
 }