compare points without getting distance

This commit is contained in:
Zdenek Borovec 2024-09-21 23:58:14 +02:00
parent 22cd0105b7
commit b99de74993
4 changed files with 41 additions and 30 deletions

View file

@ -117,7 +117,7 @@ namespace Boprs
private int NextPos(int pos, List<SweepVertex> vertices, bool[] processed) private int NextPos(int pos, List<SweepVertex> vertices, bool[] processed)
{ {
int newPos = pos + 1; 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]) if (!processed[newPos])
{ {
@ -129,7 +129,7 @@ namespace Boprs
} }
} }
newPos = pos - 1; 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]) if (!processed[newPos])
{ {
@ -185,7 +185,7 @@ namespace Boprs
processed[i] = true; processed[i] = true;
Point2d target = acVx.Point; Point2d target = acVx.Point;
List<Point2d> contourVertices = new List<Point2d>() { vertices[pos].Point }; List<Point2d> contourVertices = new List<Point2d>() { vertices[pos].Point };
while (!Utils.DoubleEquals(vertices[pos].OtherVertex().Point.GetDistanceTo(target), 0)) while (!Utils.PointEquals(vertices[pos].OtherVertex().Point, target))
{ {
int otherPos = vertices.IndexOf(vertices[pos].OtherVertex()); int otherPos = vertices.IndexOf(vertices[pos].OtherVertex());
pos = NextPos(otherPos, vertices, processed); pos = NextPos(otherPos, vertices, processed);

View file

@ -235,13 +235,13 @@ namespace Boprs
Point2d intPt = intersector.GetIntersectionPoint(0); Point2d intPt = intersector.GetIntersectionPoint(0);
// If the point is inside of either edge (not on its end point), subdivide the edge. // If the point is inside of either edge (not on its end point), subdivide the edge.
if (!(Utils.DoubleEquals(intPt.GetDistanceTo(LeftVertex.Point), 0) || if (!(Utils.PointEquals(intPt, LeftVertex.Point) ||
Utils.DoubleEquals(intPt.GetDistanceTo(RightVertex.Point), 0))) Utils.PointEquals(intPt, RightVertex.Point)))
{ {
newVertices.AddRange(SubdivideAt(intPt)); newVertices.AddRange(SubdivideAt(intPt));
} }
if (!(Utils.DoubleEquals(intPt.GetDistanceTo(otherEdge.LeftVertex.Point), 0) || if (!Utils.PointEquals(intPt, otherEdge.LeftVertex.Point) ||
Utils.DoubleEquals(intPt.GetDistanceTo(otherEdge.RightVertex.Point), 0))) Utils.PointEquals(intPt, otherEdge.RightVertex.Point))
{ {
newVertices.AddRange(otherEdge.SubdivideAt(intPt)); newVertices.AddRange(otherEdge.SubdivideAt(intPt));
} }
@ -256,14 +256,14 @@ namespace Boprs
List<SweepVertex> newVertices = new List<SweepVertex>(); List<SweepVertex> newVertices = new List<SweepVertex>();
// The edges share the left vertex // 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 et = (TransitionInside == otherEdge.TransitionInside) ?
EdgeType.OVERLAP_SAME : EdgeType.OVERLAP_DIFFERENT; EdgeType.OVERLAP_SAME : EdgeType.OVERLAP_DIFFERENT;
// The edges share the right vertex as well, set the edge type for both this and the other // The edges share the right vertex as well, set the edge type for both this and the other
// edge and return an empty list. // 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; Type = et;
otherEdge.Type = EdgeType.OVERLAP_SILENT; otherEdge.Type = EdgeType.OVERLAP_SILENT;
@ -309,7 +309,7 @@ namespace Boprs
/// <param name="pt2">EndPoint 2.</param> /// <param name="pt2">EndPoint 2.</param>
internal SweepEdge(Point2d pt1, Point2d pt2) 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."); throw new ArgumentException("Edge end points cannot be the same.");
} }

View file

@ -38,6 +38,17 @@ namespace Boprs
return (pt1.X - pt3.X) * (pt2.Y - pt3.Y) - (pt2.X - pt3.X) * (pt1.Y - pt3.Y); return (pt1.X - pt3.X) * (pt2.Y - pt3.Y) - (pt2.X - pt3.X) * (pt1.Y - pt3.Y);
} }
/// <summary>
/// Are the points at roughly the same position?
/// </summary>
/// <param name="p1">First point to compare.</param>
/// <param name="p2">Second point to compare.</param>
/// <returns><c>true</c> if the points represent the same position, <c>false</c> if not.</returns>
internal static bool PointEquals(Point2d p1, Point2d p2)
{
return DoubleEquals(p1.X, p2.X) && DoubleEquals(p1.Y, p2.Y);
}
/// <summary> /// <summary>
/// Are the two doubles close enough to be almost equal? /// Are the two doubles close enough to be almost equal?
/// </summary> /// </summary>