From 0e613fb5266e8d885bbb4929035181601b8efdea Mon Sep 17 00:00:00 2001 From: BobLd Date: Thu, 23 Jan 2020 12:36:21 +0000 Subject: [PATCH] Handle cases with not enough points in minimal bounding rectangle --- .../Geometry/GeometryExtensions.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs b/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs index 37f641b0..f1a423b6 100644 --- a/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs +++ b/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs @@ -54,6 +54,28 @@ /// internal static PdfRectangle ParametricPerpendicularProjection(IReadOnlyList polygon) { + if (polygon == null || polygon.Count == 0) + { + throw new ArgumentException("ParametricPerpendicularProjection(): polygon cannot be null and must contain at least one point."); + } + + if (polygon.Count < 4) + { + if (polygon.Count == 1) + { + return new PdfRectangle(polygon[0], polygon[0]); + } + else if (polygon.Count == 2) + { + return new PdfRectangle(polygon[0], polygon[1]); + } + else + { + PdfPoint p3 = polygon[0].Add(polygon[1].Subtract(polygon[2])); + return new PdfRectangle(p3, polygon[1], polygon[0], polygon[2]); + } + } + PdfPoint[] MBR = new PdfPoint[0]; double Amin = double.MaxValue; @@ -136,6 +158,11 @@ /// internal static IEnumerable GrahamScan(IEnumerable points) { + if (points == null || points.Count() == 0) + { + throw new ArgumentException("GrahamScan(): points cannot be null and must contain at least one point."); + } + if (points.Count() < 3) return points; Func ccw = (PdfPoint p1, PdfPoint p2, PdfPoint p3) => @@ -173,6 +200,11 @@ } } + if (sortedPoints.Count < 2) + { + return new[] { P0, sortedPoints[0] }; + } + stack.Push(P0); stack.Push(sortedPoints[0]); stack.Push(sortedPoints[1]);