Пример #1
0
        private static Tuple <double, Point[]> GetPercentCovered(Point[] polygon, Rect rect)
        {
            // Figure out the intersected polygon
            Point[] intersection = Math2D.GetIntersection_Polygon_Polygon(
                polygon,
                new Point[] { rect.TopLeft, rect.TopRight, rect.BottomRight, rect.BottomLeft });

            if (intersection == null || intersection.Length == 0)
            {
                return(null);
            }

            // Calculate the area of the polygon
            double areaPolygon = Math2D.GetAreaPolygon(intersection);

            double areaRect = rect.Width * rect.Height;

            if (areaPolygon > areaRect)
            {
                if (areaPolygon > areaRect * 1.01)
                {
                    throw new ApplicationException(string.Format("Area of intersected polygon is larger than the rectangle that clipped it: polygon={0}, rectangle={1}", areaPolygon.ToString(), areaRect.ToString()));
                }
                areaPolygon = areaRect;
            }

            return(Tuple.Create(areaPolygon / areaRect, intersection));
        }