3 个点在 2d 中共线
我试图验证 3 个点(双)何时在二维中共线。我已经找到了 如果经过验证,则返回 true 的不同 Pascal 函数;这些函数使用整数来指定 X 和 Y 坐标。我需要更精确的计算 至少到X和Y表示的小数部分前3位 作为双类型。谁能帮我解决这个问题?
我发现了这个函数:
function Collinear(x1, y1, x2, y2, x3, y3: Double): Boolean;
begin
Result := (((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) = 0);
end;
但我猜计算永远不会是0。我应该使用类似的东西吗?
function Collinear(x1, y1, x2, y2, x3, y3: Double): Boolean;
begin
Result := (((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) < 0.01);
end;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当且仅当三个点共线时,您计算的标量积方程为零。但是,在有限精度机器上,您不想测试是否等于零,而是测试零到某个小容差。
由于方程可能为负数,也可能为正数,因此您的测试将不起作用。当方程计算结果为大负值时,它将返回误报。因此,您需要测试绝对值是否很小:
具体如何选择容差取决于您未提供的信息。价值观从何而来?它们是立体的吗?
The scalar product equation you calculate is zero if and only if the three points are co-linear. However, on a finite precision machine you don't want to test for equality to zero but instead you test for zero up to some small tolerance.
Since the equation can be negative as well as positive your test isn't going to work. It will return false positives when the equation evaluates to a large negative value. Thus you need to test that the absolute value is small:
Exactly how to choose tolerance depends on information you haven't provided. Where do the values come from? Are they dimensional?
David 的代码可以工作,但您应该获得作为参数函数的容差,如下所示:
如果您不这样做并使用常量,则可能会遇到 x1..y3 值较大的奇怪错误。
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.