Example #1
0
        public ConvexHull FindHull()
        {
            if (this.points.Count <= 3)
            {
                return(new ConvexHull(this.points));
            }

            var pointsSortedByAngle = this.SortPointsByAngle();
            int index = 1;

            while (index + 1 < pointsSortedByAngle.Count)
            {
                var value = PointAngleComparer2D.Compare(pointsSortedByAngle[index - 1], pointsSortedByAngle[index + 1], pointsSortedByAngle[index]);
                if (value < 0)
                {
                    index++;
                }
                else //Also removes points that are on a line when value == 0
                {
                    pointsSortedByAngle.RemoveAt(index);
                    if (index > 1)
                    {
                        index--;
                    }
                }
            }

            pointsSortedByAngle.Add(pointsSortedByAngle.First());
            return(new ConvexHull(pointsSortedByAngle));
        }
Example #2
0
        private IList <Point> SortPointsByAngle()
        {
            var p0           = this.FindMinimalOrdinatePoint();
            var comparer     = new PointAngleComparer2D(p0);
            var sortedPoints = new List <Point>(this.points);

            sortedPoints.Remove(p0);
            sortedPoints.Insert(0, p0);
            sortedPoints.Sort(1, sortedPoints.Count - 1, comparer);
            return(sortedPoints);
        }