From b99de74993b7313ad133c5444af77dea8a00b595 Mon Sep 17 00:00:00 2001 From: Zdenek Borovec Date: Sat, 21 Sep 2024 23:58:14 +0200 Subject: [PATCH] compare points without getting distance --- Contour.cs | 12 ++++++------ Region.cs | 32 ++++++++++++++++---------------- SweepEdge.cs | 14 +++++++------- Utils.cs | 13 ++++++++++++- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/Contour.cs b/Contour.cs index 06b41a4..ddb8a7f 100644 --- a/Contour.cs +++ b/Contour.cs @@ -36,11 +36,11 @@ namespace Boprs /// public double SignedArea { get; private set; } - /// - /// Are the vertices of the contour arranged in clockwise manner? - /// - /// true if the contour is clockwise, false if counter-clockwise. - public bool IsClockwise { get => SignedArea < 0; } + /// + /// Are the vertices of the contour arranged in clockwise manner? + /// + /// true if the contour is clockwise, false if counter-clockwise. + public bool IsClockwise { get => SignedArea < 0; } /// /// Get a copy of the list of the vertices of this contour. @@ -74,7 +74,7 @@ namespace Boprs // The above algorithm deems clockwise area to be positive, // however we consider ccw contours to be positive, so we flip the value. - SignedArea = - areaSum / 2; + SignedArea = - areaSum / 2; } /// diff --git a/Region.cs b/Region.cs index 09e4e5f..99760da 100644 --- a/Region.cs +++ b/Region.cs @@ -117,7 +117,7 @@ namespace Boprs private int NextPos(int pos, List vertices, bool[] processed) { int newPos = pos + 1; - while(newPos < vertices.Count && Utils.DoubleEquals(vertices[pos].Point.GetDistanceTo(vertices[newPos].Point), 0)) + while(newPos < vertices.Count && Utils.PointEquals(vertices[pos].Point, vertices[newPos].Point)) { if (!processed[newPos]) { @@ -129,7 +129,7 @@ namespace Boprs } } newPos = pos - 1; - while (newPos >= 0 && Utils.DoubleEquals(vertices[pos].Point.GetDistanceTo(vertices[newPos].Point), 0)) + while (newPos >= 0 && Utils.PointEquals(vertices[pos].Point, vertices[newPos].Point)) { if (!processed[newPos]) { @@ -184,8 +184,8 @@ namespace Boprs int pos = i; processed[i] = true; Point2d target = acVx.Point; - List contourVertices = new List() { vertices[pos].Point }; - while (!Utils.DoubleEquals(vertices[pos].OtherVertex().Point.GetDistanceTo(target), 0)) + List contourVertices = new List() { vertices[pos].Point }; + while (!Utils.PointEquals(vertices[pos].OtherVertex().Point, target)) { int otherPos = vertices.IndexOf(vertices[pos].OtherVertex()); pos = NextPos(otherPos, vertices, processed); @@ -193,16 +193,16 @@ namespace Boprs processed[pos] = true; processed[otherPos] = true; - contourVertices.Add(vertices[pos].Point); + contourVertices.Add(vertices[pos].Point); } processed[vertices.IndexOf(vertices[pos].OtherVertex())] = true; // Instantiate the contour object from the found vertices. - Contour contour = new Contour(contourVertices); - contours.Add(contour); + Contour contour = new Contour(contourVertices); + contours.Add(contour); - // If the contour is a hole and winds ccw, or positive and winds cw, flip it. - if (shouldBeCW != contour.IsClockwise) + // If the contour is a hole and winds ccw, or positive and winds cw, flip it. + if (shouldBeCW != contour.IsClockwise) { contour.Reverse(); } @@ -210,13 +210,13 @@ namespace Boprs Contours = contours; } - /// - /// Compute the area of the region. - /// - /// - /// Requires contours to be computed. - /// - private void ComputeArea() + /// + /// Compute the area of the region. + /// + /// + /// Requires contours to be computed. + /// + private void ComputeArea() { Area = 0; foreach(Contour contour in Contours) diff --git a/SweepEdge.cs b/SweepEdge.cs index cbd329d..d3a4352 100644 --- a/SweepEdge.cs +++ b/SweepEdge.cs @@ -235,13 +235,13 @@ namespace Boprs Point2d intPt = intersector.GetIntersectionPoint(0); // If the point is inside of either edge (not on its end point), subdivide the edge. - if (!(Utils.DoubleEquals(intPt.GetDistanceTo(LeftVertex.Point), 0) || - Utils.DoubleEquals(intPt.GetDistanceTo(RightVertex.Point), 0))) + if (!(Utils.PointEquals(intPt, LeftVertex.Point) || + Utils.PointEquals(intPt, RightVertex.Point))) { newVertices.AddRange(SubdivideAt(intPt)); } - if (!(Utils.DoubleEquals(intPt.GetDistanceTo(otherEdge.LeftVertex.Point), 0) || - Utils.DoubleEquals(intPt.GetDistanceTo(otherEdge.RightVertex.Point), 0))) + if (!Utils.PointEquals(intPt, otherEdge.LeftVertex.Point) || + Utils.PointEquals(intPt, otherEdge.RightVertex.Point)) { newVertices.AddRange(otherEdge.SubdivideAt(intPt)); } @@ -256,14 +256,14 @@ namespace Boprs List newVertices = new List(); // The edges share the left vertex - if (Utils.DoubleEquals(LeftVertex.Point.GetDistanceTo(otherEdge.LeftVertex.Point), 0)) + if (Utils.PointEquals(LeftVertex.Point, otherEdge.LeftVertex.Point)) { EdgeType et = (TransitionInside == otherEdge.TransitionInside) ? EdgeType.OVERLAP_SAME : EdgeType.OVERLAP_DIFFERENT; // The edges share the right vertex as well, set the edge type for both this and the other // edge and return an empty list. - if (Utils.DoubleEquals(RightVertex.Point.GetDistanceTo(otherEdge.RightVertex.Point), 0)) + if (Utils.PointEquals(RightVertex.Point, otherEdge.RightVertex.Point)) { Type = et; otherEdge.Type = EdgeType.OVERLAP_SILENT; @@ -309,7 +309,7 @@ namespace Boprs /// EndPoint 2. internal SweepEdge(Point2d pt1, Point2d pt2) { - if (Utils.DoubleEquals(pt1.GetDistanceTo(pt2), 0)) + if (Utils.PointEquals(pt1, pt2)) { throw new ArgumentException("Edge end points cannot be the same."); } diff --git a/Utils.cs b/Utils.cs index fb3b12b..c2b5b23 100644 --- a/Utils.cs +++ b/Utils.cs @@ -25,7 +25,7 @@ namespace Boprs /// Largest difference between two doubles before they are considered equal. /// internal const double DOUBLEPREC = 1e-8; - + /// /// Signed area of the triangle (pt1, pt2, pt3). /// @@ -38,6 +38,17 @@ namespace Boprs return (pt1.X - pt3.X) * (pt2.Y - pt3.Y) - (pt2.X - pt3.X) * (pt1.Y - pt3.Y); } + /// + /// Are the points at roughly the same position? + /// + /// First point to compare. + /// Second point to compare. + /// true if the points represent the same position, false if not. + internal static bool PointEquals(Point2d p1, Point2d p2) + { + return DoubleEquals(p1.X, p2.X) && DoubleEquals(p1.Y, p2.Y); + } + /// /// Are the two doubles close enough to be almost equal? ///