For the issue below, is it a matter of I'm so close to zero but comparing zero with a tolerance won't work? The more precise the numbers, the more my check for an arc's point on a line fails and the less precise the more it works. The CAD drawing does have an arc that has a point on a line segment and that is where I get my input co-ordinates from for this test.
class Line
{
public Point Point1 {get;set;}
public Point Point2 {get;set;}
public Line(double x1, double y1, double x2, double y2)
{
Point1 = new Point(x1,y1); Point2 = new Point(x2,y2);
}
}
class Point
{
public double X {get;set;}
public double Y {get;set;}
public Point (double x, double y)
{
X = x; Y = y;
}
}
//4 decimal place numbers, works
Point arcEnd = new Point(3.8421, 16.9538); // these numbers don't
//3.84212141717697,
//16.9538136440052
Point arcStart = new Point(4.0921, 17.2038);
//test an arc point on/off the line
Line line = new Line(3.9336, 16.9538, 3.7171, 16.9538);
//these numbers don't 3.93362776812308, 16.9538136440053,
//3.71712141717697, 16.9538136440054
bool on_line = Sign(line.Point1, line.Point2, arcEnd) //true
//more precise numbers, from CAD / dxf drawing for line and arc, arc end
//point touches somewhere on the line (included in comments above, fail)
//so on_line = true for the above inputs and the Sign function gives zero,
//but when using the commented precise numbers sign gives back 1 and the
//value computed in sign is 3.0639866299190109E-14.
public static bool Sign(Point Point1, Point Point2, Point point)
{
double value = (Point2.X - Point1.X) * (p.Y - Point1.Y) - (Point2.Y - Point1.Y) * (p.X - Point1.X);
return Equals(Math.Sign(value), 0);
}
public static bool Equals(double d1, double d2, double tolerance=0.000001)
{
double a = Math.Abs(d1 - d2);
double b = Math.Abs(d1 * tolerance);
if (a <= b)
{
return true;
}
return false;
}
Reviewed formulas and stack overflow, the algorithm works a majority of the time but found cases it fails and I traced it to the included example and identified my check is giving back Sign = 1 not Sign = 0 for the above inputs and more precision.
Equals
function is a bit strange. Surely you wantif (a <= tolerance)
? – John_ReinstateMonica Aug 22 at 0:22d1
by thetolerance
value, and uses that as the threshold for equality. Personally, I think that's wrong, because how far apart two numbers need to be for them to not be equal any more shouldn't have anything to do with the magnitude of those numbers -- after all, doing it this way would mean that nothing could be equal to0
, while very large numbers can be far apart and still equal. – Peter Duniho Aug 22 at 0:37bool
in a method declared as returningint
) – Peter Duniho Aug 22 at 0:38