compare points without getting distance
This commit is contained in:
parent
22cd0105b7
commit
b99de74993
4 changed files with 41 additions and 30 deletions
12
Contour.cs
12
Contour.cs
|
@ -36,11 +36,11 @@ namespace Boprs
|
|||
/// </summary>
|
||||
public double SignedArea { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Are the vertices of the contour arranged in clockwise manner?
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the contour is clockwise, <c>false</c> if counter-clockwise.</returns>
|
||||
public bool IsClockwise { get => SignedArea < 0; }
|
||||
/// <summary>
|
||||
/// Are the vertices of the contour arranged in clockwise manner?
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the contour is clockwise, <c>false</c> if counter-clockwise.</returns>
|
||||
public bool IsClockwise { get => SignedArea < 0; }
|
||||
|
||||
/// <summary>
|
||||
/// 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
32
Region.cs
32
Region.cs
|
@ -117,7 +117,7 @@ namespace Boprs
|
|||
private int NextPos(int pos, List<SweepVertex> 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<Point2d> contourVertices = new List<Point2d>() { vertices[pos].Point };
|
||||
while (!Utils.DoubleEquals(vertices[pos].OtherVertex().Point.GetDistanceTo(target), 0))
|
||||
List<Point2d> contourVertices = new List<Point2d>() { 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute the area of the region.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Requires contours to be computed.
|
||||
/// </remarks>
|
||||
private void ComputeArea()
|
||||
/// <summary>
|
||||
/// Compute the area of the region.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Requires contours to be computed.
|
||||
/// </remarks>
|
||||
private void ComputeArea()
|
||||
{
|
||||
Area = 0;
|
||||
foreach(Contour contour in Contours)
|
||||
|
|
14
SweepEdge.cs
14
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<SweepVertex> newVertices = new List<SweepVertex>();
|
||||
|
||||
// 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
|
|||
/// <param name="pt2">EndPoint 2.</param>
|
||||
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.");
|
||||
}
|
||||
|
|
13
Utils.cs
13
Utils.cs
|
@ -25,7 +25,7 @@ namespace Boprs
|
|||
/// Largest difference between two doubles before they are considered equal.
|
||||
/// </summary>
|
||||
internal const double DOUBLEPREC = 1e-8;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Signed area of the triangle (pt1, pt2, pt3).
|
||||
/// </summary>
|
||||
|
@ -38,6 +38,17 @@ namespace Boprs
|
|||
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>
|
||||
/// Are the two doubles close enough to be almost equal?
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in a new issue