I am trying to verify when 3 points (double) are collinear in 2-D. I have found different Pascal functions that return true if this is verified; those functions use integer to specify X and Y coordinates. I need a more precise calculation at least to the first 3 digits of the decimal part of X and Y expressed as double type. Who can help me with this?
I found this function:
function Collinear(x1, y1, x2, y2, x3, y3: Double): Boolean;
begin
Result := (((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) = 0);
end;
But I guess the calculation would never be 0. Should I use something like that?
function Collinear(x1, y1, x2, y2, x3, y3: Double): Boolean;
begin
Result := (((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) < 0.01);
end;
David's code will work, but you should get the tolerance as a function of the parameters, like so:
If you don't do that and use a constant instead you can run into weird errors with large values for x1..y3.