检查是一个点(x,y)是在直线上绘制的两个点之间 [英] Check is a point (x,y) is between two points drawn on a straight line

查看:123
本文介绍了检查是一个点(x,y)是在直线上绘制的两个点之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画了两点之间的一条线A(x,y)--- B(x,y)
现在我有第三个点C(x,y)。我想知道如果C位于A和B之间的线上。
我想用java语言编写。我找到了几个类似的答案。但是,所有都有一些问题,没有人是完美的。

I have drawn a line between two points A(x,y)---B(x,y) Now I have a third point C(x,y). I want to know that if C lies on the line which is drawn between A and B. I want to do it in java language. I have found couple of answers similar to this. But, all have some problems and no one is perfect.

推荐答案

if (distance(A, C) + distance(B, C) == distance(A, B))
    return true; // C is on the line.
return false;    // C is not on the line.

或只是:

return distance(A, C) + distance(B, C) == distance(A, B);

这种方式非常简单。如果C位于 AB 行,您将得到以下情形:

The way this works is rather simple. If C lies on the AB line, you'll get the following scenario:

A-C------B

并且,无论它在哪一行, dist(AC)+ dist(CB)== dist(AB)。对于任何其他情况,你有一个描述的三角形和'dist(AC)+ dist(CB)> dist(AB)':

and, regardless of where it lies on that line, dist(AC) + dist(CB) == dist(AB). For any other case, you have a triangle of some description and 'dist(AC) + dist(CB) > dist(AB)':

A-----B
 \   /
  \ /
   C

事实上,如果C位于外推线上,这甚至有效:

In fact, this even works if C lies on the extrapolated line:

C---A-------B

只要距离保持无符号。距离 dist(AB)的计算公式如下:

provided that the distances are kept unsigned. The distance dist(AB) can be calculated as:

  ___________________________
 /           2              2
V (A.x - B.x)  + (A.y - B.y)

请记住浮点运算的固有限制(有限精度)。您可能需要选择足够接近的测试(例如,小于百万分之一的错误)以确保相等的正确运行。

Keep in mind the inherent limitations (limited precision) of floating point operations. It's possible that you may need to opt for a "close enough" test (say, less than one part per million error) to ensure correct functioning of the equality.

这篇关于检查是一个点(x,y)是在直线上绘制的两个点之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆