3点在2d共线 [英] 3 points are collinear in 2d
问题描述
不同的Pascal函数返回true,如果这是验证的;那些函数使用整数来指定X和Y坐标。我需要一个更精确的计算
至少是X和Y的小数部分的前3位,Y表示
为double类型。谁能帮助我吗?
我发现这个功能:
函数Collinear(x1,y1,x2,y2,x3,y3:Double):Boolean;
begin
结果:=(((x2 - x1)*(y3 - y1) - (x3 - x1)*(y2 - y1))= 0);
结束
但我猜这个计算永远不会是0.我应该用这样的东西吗?
函数Collinear(x1,y1,x2,y2,x3,y3:Double):Boolean;
begin
结果:=(((x2 - x1)*(y3 - y1) - (x3 - x1)*(y2 - y1))<0.01);
结束
David的代码可以正常工作,作为参数的函数,如下所示:
函数Collinear(const x1,y1,x2,y2,x3,y3: Double):Boolean;一致;
var
容忍度:双;
begin
tolerance:= abs(max(x1,x2,x3,y1,y2,y3))* 0.000001;
结果:= abs((x2-x1)*(y3-y1) - (x3-x1)*(y2-y1))<公差;
结束
如果你不这样做,并使用常量,你可以遇到大值的奇怪错误对于x1..y3。
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:
function Collinear(const x1, y1, x2, y2, x3, y3: Double): Boolean; inline;
var
tolerance: double;
begin
tolerance := abs(max(x1,x2,x3,y1,y2,y3)) * 0.000001;
Result := abs((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) < tolerance;
end;
If you don't do that and use a constant instead you can run into weird errors with large values for x1..y3.
这篇关于3点在2d共线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!