Region.IsVisible(PointF) has very slow performance for large floating point values

前端 未结 2 1072
北荒
北荒 2020-12-11 18:12

I have run into a strange performance issue, and it would be great with an explanation to the behavior I\'m experiencing.

I\'m using System.Drawing.Region.IsVisible

2条回答
  •  不思量自难忘°
    2020-12-11 19:06

    We've have been struggling with the same issue: with a 50k points polygon it tooks 45 seconds to build the rectangles behind the IsVisible() method. We tried to cache this data, but finally we had over 1 millions rectangles; with multiple regions we had hundreds megabytes of data to cache.

    Finally we moved to the PNPoly algorithm, that takes just few ms:

    https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html

    Here the c# version:

        public bool IsVisible(Point p, List points)
        {
            int i, j = points.Count - 1;
            bool isVisible = false;
            for (i = 0; i < points.Count; i++)
            {
                if (points[i].Y < p.Y && points[j].Y >= p.Y 
                    || points[j].Y < p.Y && points[i].Y >= p.Y)
                {
                    if (points[i].X + (p.Y - points[i].Y) / (points[j].Y - points[i].Y) 
                        * (points[j].X - points[i].X) < p.X)
                    {
                        isVisible = !isVisible;
                    }
                }
                j = i;
            }
            return isVisible;
        }
    

提交回复
热议问题