예제 #1
0
 public float angleAtSegment(int segmentindex)
 {
     return(MathsTools.SignedAngle(
                points[(int)Mathf.Repeat(segmentindex - 1, segments.Length)],
                points[(int)Mathf.Repeat(segmentindex, segments.Length)],
                points[(int)Mathf.Repeat(segmentindex + 1, segments.Length)]
                ));
 }
예제 #2
0
    public static Polygon GetConvexHull(List <Vector2> _points)
    {
        List <Vector2> points = new List <Vector2>(_points);

        if (points.Count <= 1)
        {
            return(null);
        }
        if (points.Count <= 3)
        {
            return(new Polygon(points.ToArray()));
        }

        points.sortX();

        List <Vector2> hullPoints = new List <Vector2>();

        hullPoints.Add(points[0]);
        //points.RemoveAt(0);

        Vector2 currentPoint  = hullPoints[0];
        Vector2 previousPoint = currentPoint + Vector2.up;

        float lastAngle = 361;
        int   index     = -1;

        int safelock = 0;
        int initialNumberOFPoints = points.Count;

        do
        {
            for (int i = 0; i < points.Count; i++)
            {
                if (points[i] == currentPoint || points[i] == previousPoint)
                {
                    continue;
                }
                float angle = MathsTools.SignedAngle(previousPoint, currentPoint, points[i]);
                if (angle < lastAngle)
                {
                    lastAngle = lastAngle = angle;
                    index     = i;
                }
            }

            //if (points[index] == hullPoints[0]) break;
            hullPoints.Add(points[index]);
            previousPoint = currentPoint;
            currentPoint  = points[index];
            points.RemoveAt(index);
            lastAngle = 361;

            if (safelock++ > initialNumberOFPoints)
            {
                Debug.Log("infinite loop");
                break;
            }
        } while (currentPoint != hullPoints[0]);



        return(new Polygon(hullPoints.ToArray()));
    }