Finding a point on a line

后端 未结 1 1781
情深已故
情深已故 2020-12-09 12:20

I know the start and end points on a line segment. For this example say that the line segment has a distance of 5. Now I want to know the point that has a distance of thre

1条回答
  •  醉梦人生
    2020-12-09 12:37

    If your points are (x1, y1) and (x2, y2), and you want to find the point (x3, y3) that is n units away from point 2:

    d = sqrt((x2-x1)^2 + (y2 - y1)^2) #distance
    r = n / d #segment ratio
    
    x3 = r * x2 + (1 - r) * x1 #find point that divides the segment
    y3 = r * y2 + (1 - r) * y1 #into the ratio (1-r):r
    

    0 讨论(0)
提交回复
热议问题