diff --git a/src/UglyToad.PdfPig/Content/Word.cs b/src/UglyToad.PdfPig/Content/Word.cs index e6d26255..99cb8871 100644 --- a/src/UglyToad.PdfPig/Content/Word.cs +++ b/src/UglyToad.PdfPig/Content/Word.cs @@ -145,8 +145,8 @@ { var builder = new StringBuilder(); - var maxX = double.MinValue; var minX = double.MaxValue; + var maxX = double.MinValue; var maxY = double.MinValue; var minY = double.MaxValue; @@ -226,8 +226,8 @@ { var builder = new StringBuilder(); - var maxX = double.MinValue; var minX = double.MaxValue; + var maxX = double.MinValue; var minY = double.MaxValue; var maxY = double.MinValue; @@ -266,8 +266,6 @@ private Tuple GetBoundingBoxOther(IReadOnlyList letters) { - var builder = new StringBuilder(); - var points = letters.SelectMany(r => new[] { r.StartBaseLine, @@ -278,6 +276,7 @@ var convexHull = GeometryExtensions.GrahamScan(points).ToList(); var minimalBoundingRectangle = GeometryExtensions.ParametricPerpendicularProjection(convexHull); + var builder = new StringBuilder(); for (var i = 0; i < letters.Count; i++) { builder.Append(letters[i].Value); diff --git a/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs b/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs index c5e9bb7d..37f641b0 100644 --- a/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs +++ b/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs @@ -45,17 +45,15 @@ /// /// Algorithm to find a minimal bounding rectangle (MBR) such that the MBR corresponds to a rectangle /// with smallest possible area completely enclosing the polygon. - /// From A Fast Algorithm for Generating a Minimal Bounding Rectangle by Lennert D. Den Boer. + /// From 'A Fast Algorithm for Generating a Minimal Bounding Rectangle' by Lennert D. Den Boer. /// + /// + /// Polygon P is assumed to be both simple and convex, and to contain no duplicate (coincident) vertices. + /// The vertices of P are assumed to be in strict cyclic sequential order, either clockwise or + /// counter-clockwise relative to the origin P0. + /// internal static PdfRectangle ParametricPerpendicularProjection(IReadOnlyList polygon) { - // The vertices of P are assumed to be in strict cyclic sequential order, - // either clockwise or counter-clockwise relative to the origin P0. Polygon P is assumed to be - // both simple and convex, and to contain no duplicate (coincident) vertices. - polygon = polygon.Distinct().OrderBy(p => p.X).ThenBy(p => p.Y).ToList(); - var P0 = polygon[0]; - polygon = polygon.OrderBy(p => p, new PdfPointComparer(P0)).ToList(); - PdfPoint[] MBR = new PdfPoint[0]; double Amin = double.MaxValue; @@ -69,10 +67,10 @@ PdfPoint Q = new PdfPoint(); PdfPoint R0 = new PdfPoint(); PdfPoint R1 = new PdfPoint(); - - int nv = polygon.Count; PdfPoint u = new PdfPoint(); + int nv = polygon.Count; + while (true) { var Pk = polygon[k]; @@ -133,23 +131,6 @@ return new PdfRectangle(MBR[2], MBR[3], MBR[1], MBR[0]); } - private class PdfPointComparer : IComparer - { - PdfPoint P0; - - public PdfPointComparer(PdfPoint referencePoint) - { - P0 = referencePoint; - } - - public int Compare(PdfPoint a, PdfPoint b) - { - var det = Math.Round((a.X - P0.X) * (b.Y - P0.Y) - (b.X - P0.X) * (a.Y - P0.Y), 6); - if (det == 0) return 0; - return Math.Sign(det); - } - } - /// /// Algorithm to find the convex hull of the set of points with time complexity O(n log n). ///