Remove ordering from minimal bounding rectangle

This commit is contained in:
BobLd
2020-01-22 21:23:12 +00:00
committed by Eliot Jones
parent 0dad611cb1
commit 253ae32193
2 changed files with 11 additions and 31 deletions

View File

@@ -145,8 +145,8 @@
{ {
var builder = new StringBuilder(); var builder = new StringBuilder();
var maxX = double.MinValue;
var minX = double.MaxValue; var minX = double.MaxValue;
var maxX = double.MinValue;
var maxY = double.MinValue; var maxY = double.MinValue;
var minY = double.MaxValue; var minY = double.MaxValue;
@@ -226,8 +226,8 @@
{ {
var builder = new StringBuilder(); var builder = new StringBuilder();
var maxX = double.MinValue;
var minX = double.MaxValue; var minX = double.MaxValue;
var maxX = double.MinValue;
var minY = double.MaxValue; var minY = double.MaxValue;
var maxY = double.MinValue; var maxY = double.MinValue;
@@ -266,8 +266,6 @@
private Tuple<string, PdfRectangle> GetBoundingBoxOther(IReadOnlyList<Letter> letters) private Tuple<string, PdfRectangle> GetBoundingBoxOther(IReadOnlyList<Letter> letters)
{ {
var builder = new StringBuilder();
var points = letters.SelectMany(r => new[] var points = letters.SelectMany(r => new[]
{ {
r.StartBaseLine, r.StartBaseLine,
@@ -278,6 +276,7 @@
var convexHull = GeometryExtensions.GrahamScan(points).ToList(); var convexHull = GeometryExtensions.GrahamScan(points).ToList();
var minimalBoundingRectangle = GeometryExtensions.ParametricPerpendicularProjection(convexHull); var minimalBoundingRectangle = GeometryExtensions.ParametricPerpendicularProjection(convexHull);
var builder = new StringBuilder();
for (var i = 0; i < letters.Count; i++) for (var i = 0; i < letters.Count; i++)
{ {
builder.Append(letters[i].Value); builder.Append(letters[i].Value);

View File

@@ -45,17 +45,15 @@
/// <summary> /// <summary>
/// Algorithm to find a minimal bounding rectangle (MBR) such that the MBR corresponds to a rectangle /// Algorithm to find a minimal bounding rectangle (MBR) such that the MBR corresponds to a rectangle
/// with smallest possible area completely enclosing the polygon. /// with smallest possible area completely enclosing the polygon.
/// <para>From A Fast Algorithm for Generating a Minimal Bounding Rectangle by Lennert D. Den Boer.</para> /// <para>From 'A Fast Algorithm for Generating a Minimal Bounding Rectangle' by Lennert D. Den Boer.</para>
/// </summary> /// </summary>
/// <param name="polygon">
/// 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.
/// </param>
internal static PdfRectangle ParametricPerpendicularProjection(IReadOnlyList<PdfPoint> polygon) internal static PdfRectangle ParametricPerpendicularProjection(IReadOnlyList<PdfPoint> 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]; PdfPoint[] MBR = new PdfPoint[0];
double Amin = double.MaxValue; double Amin = double.MaxValue;
@@ -69,10 +67,10 @@
PdfPoint Q = new PdfPoint(); PdfPoint Q = new PdfPoint();
PdfPoint R0 = new PdfPoint(); PdfPoint R0 = new PdfPoint();
PdfPoint R1 = new PdfPoint(); PdfPoint R1 = new PdfPoint();
int nv = polygon.Count;
PdfPoint u = new PdfPoint(); PdfPoint u = new PdfPoint();
int nv = polygon.Count;
while (true) while (true)
{ {
var Pk = polygon[k]; var Pk = polygon[k];
@@ -133,23 +131,6 @@
return new PdfRectangle(MBR[2], MBR[3], MBR[1], MBR[0]); return new PdfRectangle(MBR[2], MBR[3], MBR[1], MBR[0]);
} }
private class PdfPointComparer : IComparer<PdfPoint>
{
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);
}
}
/// <summary> /// <summary>
/// Algorithm to find the convex hull of the set of points with time complexity O(n log n). /// Algorithm to find the convex hull of the set of points with time complexity O(n log n).
/// </summary> /// </summary>