开发者

Distance from a point toward another point

Given point a point a (x1,y1) and a point c (x3,y3) we can calculate a slope m. Assuming we have a distance d I'm a bit stuck trying to figure out how to find a point b (x2,y2) which is d distance from x1,y1 in the direction of c.

Does anyone know how to calculate this?开发者_运维技巧 I thought about using the midpoint function but it's not quite there.

Help?


You can work out the full distance between a and c with:

        __________________________________
df =   / (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1)
     \/

This uses the standard "root of the sum of squares" method.

Then, if the actual partial distance you want is dp, the point can be found at (x2,y2) with:

x2 = x1 + dp/df * (x3-x1)
y2 = y1 + dp/df * (y3-y1)

which is simply moving the correct proportion dp/df in both dimensions.


You can get the direction from A to B by the following:

D = B - A

Then, you may normalize the direction (which means it is magnitude 1, or length 1):

N = D / D.Length

where

D.Length = sqrt(D.X * D.X + D.Y * D.Y)

To find a point on the line given by A and B, X units away from A in the direction of B, you would use the following:

Final = A + N * X
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜