如何判断一个点是否属于某条线?
如何判断一个点是否属于某条线?
如果可能的话,示例值得赞赏。
How can I tell if a point belongs to a certain line?
Examples are appreciated, if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在最简单的形式中,只需将坐标代入直线方程并检查是否相等。
给定:
代入 X 和 Y:
所以是的,点在线上。
如果您的线以 (X1,Y1),(X2,Y2) 形式表示,那么您可以使用以下方法计算斜率:
然后用以下方法获取 Y 相交:
编辑:我修复了 Y 相交(已为 X1 / Y1 ...)
您必须检查
x1 - x2
是否不是0
。 如果是,则检查该点是否在线上只需检查点中的 Y 值是否等于x1
或x2
即可。 另外,检查点的 X 是否不是“x1”或“x2”。In the simplest form, just plug the coordinates into the line equation and check for equality.
Given:
Plug in X and Y:
So yes, the point is on the line.
If your lines are represented in (X1,Y1),(X2,Y2) form, then you can calculate slope with:
And then get the Y-Intersect with this:
Edit: I fixed the Y-Intersect (which has been X1 / Y1 ...)
You'll have to check that
x1 - x2
is not0
. If it is, then checking if the point is on the line is a simple matter of checking if the Y value in your point is equal to eitherx1
orx2
. Also, check that the X of the point is not 'x1' or 'x2'.我刚刚编写了一个函数,它可以处理一些额外的要求,因为我在绘图应用程序中使用了此检查:
I just wrote an function which handles a few extra requirements since I use this check in a drawing application:
确定点 R = (rx, ry) 是否位于点 P = (px, py) 和 Q = (qx, qy) 的连线上的最佳方法是检查矩阵的行列式是否为
(qx - px) ) * (ry - py) - (qy - py) * (rx - px) 接近于 0。与其他发布的解决方案相比,该解决方案有几个相关优点:首先,它不需要垂直线的特殊情况,其次,它不需要不除(通常是一个缓慢的操作),第三,当线几乎但不完全垂直时,它不会触发不良的浮点行为。
The best way to determine if a point R = (rx, ry) lies on the line connecting points P = (px, py) and Q = (qx, qy) is to check whether the determinant of the matrix
namely (qx - px) * (ry - py) - (qy - py) * (rx - px) is close to 0. This solution has several related advantages over the others posted: first, it requires no special case for vertical lines, second, it doesn't divide (usually a slow operation), third, it doesn't trigger bad floating-point behavior when the line is almost, but not quite vertical.
给定直线
L0
和L1
上的两个点以及要测试的点P
。向量
n
的范数是点P
到通过L0
和L1
的直线的距离。 如果该距离为零或足够小(在舍入误差的情况下),则该点位于直线上。符号
*
表示点积。示例
Given two points on the line
L0
andL1
and the point to testP
.The norm of the vector
n
is the distance of the pointP
from the line throughL0
andL1
. If this distance is zero or small enough (in the case of rounding errors), the point lies on the line.The symbol
*
represents the dot product.Example
我认为帕特里克·麦克唐纳先生提出了几乎正确的答案,这是对他答案的更正:
当然还有很多其他正确答案,尤其是乔什先生,但我发现这是最好的答案。
谢谢大家。
I think Mr.Patrick McDonald put the nearly correct answer and this is the correction of his answer:
and of course there are many other correct answers especially Mr.Josh but i found this is the best one.
Thankx for evryone.
这是直线方程。 x& y 是坐标。 每条线的特征在于其斜率 (m ) 及其与 y 轴的相交位置 (c)。
所以给定 m & c 对于直线,您可以通过检查方程是否满足 x = x1 和 y = y1 来确定点 (x1, y1) 是否在线上
This is the equation of a line. x & y are the co-ordinates. Each line is characterized by its slope (m ) and where it intersects the y-axis (c).
So given m & c for a line, you can determine if the point (x1, y1) is on the line by checking if the equation holds for x = x1 and y = y1
二维线通常使用两个变量 x 和 y 中的方程来表示,这是一个众所周知的方程
现在想象你的 GDI+ 线是从 (0,0) 到 (100, 100) 绘制的,那么 m=(0 -100)/(0-100) = 1 因此你的线的方程是 y-0=1*(x-0) => y=x
现在我们有了相关直线的方程,很容易测试一个点是否属于这条直线。 当您代入 x=x3 和 y=y3 时,如果给定点 (x3, y3) 满足直线方程,则该点属于该直线。 例如,点 (10, 10) 属于这条线,因为 10=10,但 (10,12) 不属于这条线,因为 12 != 10。
注意:对于垂直线,斜率 (m) 值为无限,但对于这种特殊情况,您可以直接使用垂直线方程 x=c,其中 c = x1 = x2。
尽管我不得不说我不确定这是否是最有效的方法。 当我有更多时间时,我会尝试找到更有效的方法。
希望这可以帮助。
A 2D line is generally represented using an equation in two variables x and y here is a well known equation
Now imagine your GDI+ line is drawn from (0,0) to (100, 100) then the value of m=(0-100)/(0-100) = 1 thus the equation for your line is y-0=1*(x-0) => y=x
Now that we have an equation for the line in question its easy to test if a point belongs to this line. A given point (x3, y3) belongs to this line if it satisfies the line equation when you substitute x=x3 and y=y3. For example the point (10, 10) belongs to this line since 10=10 but (10,12) does not belong to this line since 12 != 10.
NOTE: For a vertical line the value of the slope (m) is infinite but for this special case you may use the equation for a vertical line directly x=c where c = x1 = x2.
Though I have to say I am not sure if this is the most efficient way of doing this. I will try and find a more efficient way when I have some more time on hand.
Hope this helps.
如果您有一条由其端点定义的线
,并且您有一个想要检查的点
,那么您可以按如下方式定义一个函数:
并按如下方式调用它:
不过,您将需要检查是否被零除。
If you have a line defined by its endpoints
and you have a point that you want to check
then you could define a function as follows:
and call it as follows:
You will need to check for division by zero though.
直线方程为:
因此,如果点 (a,b) 满足该方程,则该点位于该直线上,即
b = ma + c
Equation of the line is:
So a point(a,b) is on this line if it satisfies this equation i.e.
b = ma + c
作为
slope/y-intercept
方法的替代方法,我选择了使用Math.Atan2
的方法:第一个检查
reference
确定 arcTan线段的起点到终点。然后从起点角度,确定向量
v
的arcTan。如果这些值相等,我们从终点的角度进行检查。
简单,可以处理水平、垂直以及介于两者之间的所有其他情况。
As an alternative to the
slope/y-intercept
method, I chose this approach usingMath.Atan2
:The first check
reference
determines the arcTan from the start point of the line segment to it's end-point.Then from the start point perspective, we determine the arcTan to the vector
v
.If those values are equal, we check from the perspective of the end-point.
Simple and handles horizontal, vertical and all else in between.