我如何才能知道一个点属于某一行?
How can I tell if a point belongs to a certain line?
例子是AP preciated,如果可能的话。
Examples are appreciated, if possible.
在最简单的形式,只需将坐标转换为直线方程,并检查是否相等。
In the simplest form, just plug the coordinates into the line equation and check for equality.
由于:
Point p (X=4, Y=5)
Line l (Slope=1, YIntersect=1)
插上X和Y:
Plug in X and Y:
Y = Slope * X + YIntersect
=> 5 = 1 * 4 + 1
=> 5 = 5
所以,是的,一点就行了。
So yes, the point is on the line.
如果您的线条被重新psented在$ P $(X1,Y1),(X2,Y2)的形式,那么你就可以计算出斜率为:
If your lines are represented in (X1,Y1),(X2,Y2) form, then you can calculate slope with:
Slope = (y1 - y2) / (x1-x2)
,然后得到的Y相交的:
And then get the Y-Intersect with this:
Y-Intersect = - Slope * X1 + Y1;
编辑:我固定的Y相交(已经进行了X1 / Y1 ...)
I fixed the Y-Intersect (which has been X1 / Y1 ...)
您将必须检查 X1 - X2
不是 0
。如果是,则检查是否点上线检查,如果你点的Y值等于或者 X1
或 X2 。此外,检查点的X不是X1或X2。
You'll have to check that x1 - x2
is not 0
. 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 either x1
or x2
. Also, check that the X of the point is not 'x1' or 'x2'.