在给定距离的直线上查找点 [英] Finding points on a line with a given distance

查看:36
本文介绍了在给定距离的直线上查找点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

由于您的线具有斜率 m,因此其方向向量是 <1, m>.因为它在 x 中每移动 1 个像素就在 y 中移动 m 个像素.您想将该方向向量归一化为单位长度,以便除以向量的大小.

<前>震级 = (1^2 + m^2)^(1/2)N=<1,m>/幅度=<1/幅度,米/幅度>

归一化的方向向量是 N.现在你差不多完成了.您只需要以参数化格式为您的行编写方程式:

<前>f(t) = A + t*N

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

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

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.

解决方案

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.

这篇关于在给定距离的直线上查找点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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