寻找给定距离的直线上的点

发布于 2024-07-30 20:30:11 字数 126 浏览 9 评论 0原文

我有一个问题我知道一条线我只知道它的斜率(m)和它上面的一个点A(x,y)我如何计算这条线上的点(实际上是其中的两个)与点的距离(d) A ??? 我要求查找通过 A(x,y) 且距离为 的线上的像素强度。在这种情况下,距离将是像素数。

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 ???
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情魔剑神 2024-08-06 20:30:23

我们将您要查找的点称为 P,坐标为 px、py,起始点 A 的坐标为 ax 和 ay。 斜率 m 只是 Y 的变化与 X 的变化之比,因此如果点 P 距 A 的距离为 s,则其坐标为 px = ax + s,并且 py = ay + m * s。 现在使用毕达哥拉斯,从 A 到 P 的距离 d 将为 d = sqrt(s * s + (m * s) * (m * s))。 要使 P 距 A 特定的 D 个单位,请求 s 为 s = D/sqrt(1 + m * m)。

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. 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. Now using Pythagoras, the distance d from A to P will be 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).

时光倒影 2024-08-06 20:30:21

下面是一个 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)

基于 @Theophile 的出色答案 这里在数学堆栈交换上。

Here's a Python implementation to find a point on a line segment at a given distance from the initial point:

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.

执妄 2024-08-06 20:30:20

让我简单地解释一下答案

起点 - (x0, y0)

终点 - (x1, y1)

我们需要在 a 处找到一个点 (xt, yt)从起点到终点的距离 dt。

起点和终点之间的距离由下式给出:d = sqrt((x1 - x0)^2 + (y1 - y0)^2)

令比率距离,t = dt / d

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

0 t < 1,点在线上。

t < 0,该点位于(x0, y0)附近的线之外。

t > 时 1,该点位于(x1, y1)附近的线之外。

Let me explain the answer in a simple way.

Start point - (x0, y0)

End point - (x1, y1)

We need to find a point (xt, yt) at a distance dt from start point towards end point.

Point on a line at a distance

The distance between Start and End point is given by d = sqrt((x1 - x0)^2 + (y1 - y0)^2)

Let the ratio of distances, t = dt / d

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

When 0 < t < 1, the point is on the line.

When t < 0, the point is outside the line near to (x0, y0).

When t > 1, the point is outside the line near to (x1, y1).

贱人配狗天长地久 2024-08-06 20:30:16

我建议将线转换为参数格式而不是点斜率。 也就是说,直线的参数函数返回沿该直线的点的某个参数 t 的值。 您可以将线表示为参考点,并用向量表示穿过该点的线的方向。 这样,您只需从 A 点前后移动 d 个单位即可获得其他积分。

由于直线的斜率为 m,因此其方向向量为 <1, m>。 因为它在 x 中每移动 1 个像素,就在 y 中移动 m 个像素。 您希望将该方向向量标准化为单位长度,以便除以向量的大小。

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

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

归一化方向向量是 N。现在您就快完成了。 您只需以参数化格式编写直线方程:

    f(t) = A + t*N

这使用向量数学。 具体来说,标量向量乘法(参数 t 和向量 N)和 向量加法(A 和 t*N)。 函数 f 的结果是沿直线的点。 您要寻找的 2 个点是 f(d) 和 f(-d)。 用您选择的语言实现它。

与迄今为止的所有其他答案相比,使用此方法的优点是您可以轻松扩展此方法以支持具有“无限”斜率的线。 也就是说,像 x = 3 这样的垂直线。您实际上不需要斜率,您需要的只是标准化方向向量。 对于垂直线,它是<0,1>。 这就是图形运算经常使用向量数学的原因,因为计算更直接并且不易出现奇异点。
乍一看可能有点复杂,但是一旦掌握了向量运算的窍门,许多计算机图形任务就会变得容易多了。

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. 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.

Since your line has slope m, its direction vector is <1, m>. Since it moves m pixels in y for every 1 pixel in x. 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. 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). The result of the function f is a point along the line. The 2 points you are looking for are f(d) and 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. For a vertical line, it is <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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文