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

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

问题描述

我有一个问题,我知道一条线,我只知道它的斜率(m)和它的一个点A(x,y)我如何计算这个点上的点(实际上是两个) )从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.

推荐答案

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

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.

由于您的线具有斜率m,因此其方向矢量<1,米>。因为它在x中每移动1个像素就会在y中移动m个像素。你想把这个方向矢量归一化为单位长度,所以你除以矢量的大小。

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>

标准化的方向向量是N.现在您快要完成了。您只需要以参数化格式编写您的线的公式:

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

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

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.

使用这种方法的优点与迄今为止的所有其他答案相反,可以轻松地将此方法扩展为支持无限斜坡线。也就是说,像x = 3这样的垂直线。您并不需要斜率,只需要标准化的方向矢量。对于垂直线,它是< 0,1>。这就是为什么图形操作经常使用矢量数学的原因,因为计算更直接,并且不易出现奇点。
起初看起来有点复杂,但是一旦你掌握了矢量操作的方法,很多计算机图形任务就会变得更容易。

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天全站免登陆