简体   繁体   English

在给定距离的直线上查找点

[英]Finding points on a line with a given distance

I have a question i know a line i just know its slope(m) and a point on it A(x,y) How can i calculate the points(actually two of them) on this line with a distance(d) from point A ???我有一个问题 我知道一条线 我只知道它的斜率 (m) 和它上面的一个点 A(x,y) 我如何计算这条线上的点(实际上是其中两个)与点的距离(d)一种 ??? I m asking this for finding intensity of pixels on a line that pass through A(x,y) with a distance .Distance in this case will be number of pixels.我问这个是为了找到通过 A(x,y) 距离为 的线上的像素强度。在这种情况下,距离将是像素数。

I would suggest converting the line to a parametric format instead of point-slope.我建议将线转换为参数格式而不是点斜率。 That is, a parametric function for the line returns points along that line for the value of some parameter t.也就是说,该线的参数函数返回沿该线的点,以获得某些参数 t 的值。 You can represent the line as a reference point, and a vector representing the direction of the line going through that point.您可以将线表示为参考点,并用向量表示通过该点的线的方向。 That way, you just travel d units forward and backward from point A to get your other points.这样,您只需从 A 点向前和向后移动 d 个单位即可获得其他点。

Since your line has slope m, its direction vector is <1, m>.由于您的线具有斜率 m,因此其方向向量为 <1, m>。 Since it moves m pixels in y for every 1 pixel in x.因为它在 x 中每移动 1 个像素就在 y 中移动 m 个像素。 You want to normalize that direction vector to be unit length so you divide by the magnitude of the vector.您想将该方向向量归一化为单位长度,以便除以向量的大小。

magnitude = (1^2 + m^2)^(1/2)

    N = <1, m> / magnitude = <1 / magnitude, m / magnitude>

The normalized direction vector is N. Now you are almost done.归一化的方向向量是 N。现在你差不多完成了。 You just need to write the equation for your line in parameterized format:您只需要以参数化格式为您的行编写方程式:

f(t) = A + t*N

This uses vector math .这使用向量数学 Specifically, scalar vector multiplication (of your parameter t and the vector N) and vector addition (of A and t*N).具体来说, 标量向量乘法(参数 t 和向量 N)和向量加法(A 和 t*N)。 The result of the function f is a point along the line.函数 f 的结果是沿线的一个点。 The 2 points you are looking for are f(d) and f(-d).您正在寻找的 2 个点是 f(d) 和 f(-d)。 Implement that in the language of your choosing.用您选择的语言实现它。

The advantage to using this method, as opposed to all the other answers so far, is that you can easily extend this method to support a line with "infinite" slope.与迄今为止的所有其他答案相比,使用此方法的优点是您可以轻松扩展此方法以支持具有“无限”斜率的线。 That is, a vertical line like x = 3. You don't really need the slope, all you need is the normalized direction vector.也就是说,像 x = 3 这样的垂直线。你并不真正需要斜率,你需要的只是归一化的方向向量。 For a vertical line, it is <0, 1>.对于垂直线,它是 <0, 1>。 This is why graphics operations often use vector math, because the calculations are more straight-forward and less prone to singularities.这就是图形运算经常使用向量数学的原因,因为计算更直接,更不容易出现奇点。 It may seem a little complicated at first, but once you get the hang of vector operations, a lot of computer graphics tasks get a lot easier.乍一看似乎有点复杂,但是一旦掌握了矢量操作的窍门,许多计算机图形任务就会变得容易得多。

Let me explain the answer in a simple way . 让我以简单的方式解释答案

Start point - (x0, y0)起点 - (x0, y0)

End point - (x1, y1)终点 - (x1, y1)

We need to find a point (xt, yt) at a distance dt from start point towards end point.我们需要在从起点到终点的距离为 dt 处找到一个点(xt, yt)

点在远处的一条线上

The distance between Start and End point is given by d = sqrt((x1 - x0)^2 + (y1 - y0)^2)起点和终点之间的距离由d = sqrt((x1 - x0)^2 + (y1 - y0)^2)

Let the ratio of distances, t = dt / d让距离的比值, t = dt / d

Then the point (xt, yt) = (((1 - t) * x0 + t * x1), ((1 - t) * y0 + t * y1))然后点(xt, yt) = (((1 - t) * x0 + t * x1), ((1 - t) * y0 + t * y1))

When 0 < t < 1 , the point is on the line.0 < t < 1 ,点在直线上。

When t < 0 , the point is outside the line near to (x0, y0) .t < 0 ,该点位于(x0, y0)附近的线外。

When t > 1 , the point is outside the line near to (x1, y1) .t > 1 ,该点位于(x1, y1)附近的线外。

Let's call the point you are trying to find P, with coordinates px, py, and your starting point A's coordinates ax and ay.让我们用坐标 px、py 和起点 A 的坐标 ax 和 ay 来调用您尝试查找 P 的点。 Slope m is just the ratio of the change in Y over the change in X, so if your point P is distance s from A, then its coordinates are px = ax + s, and py = ay + m * s.斜率 m 只是 Y 的变化与 X 变化的比值,因此如果您的点 P 与 A 的距离为 s,则其坐标为 px = ax + s,而 py = ay + m * s。 Now using Pythagoras, the distance d from A to P will be d = sqrt(s * s + (m * s) * (m * s)).现在使用毕达哥拉斯,从 A 到 P 的距离 d 将是 d = sqrt(s * s + (m * s) * (m * s))。 To make P be a specific D units away from A, find s as s = D/sqrt(1 + m * m).要使 P 与 A 相距特定的 D 个单位,请找到 s 为 s = D/sqrt(1 + m * m)。

Here's a Python implementation to find a point on a line segment at a given distance from the initial point:这是一个 Python 实现,用于在距初始点给定距离的线段上找到一个点:

import numpy as np

def get_point_on_vector(initial_pt, terminal_pt, distance):
    v = np.array(initial_pt, dtype=float)
    u = np.array(terminal_pt, dtype=float)
    n = v - u
    n /= np.linalg.norm(n, 2)
    point = v - distance * n

    return tuple(point)

Based on the excellent answer from @Theophile here on math stackexchange.基于从@Theophile优秀的答案在这里的数学stackexchange。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM