how to find a point in the path of a line

前端 未结 3 1466
甜味超标
甜味超标 2021-01-29 04:10

I\'ve got two points between which im drawing a line (x1,y1 and x2,y2) but i need to know the coordinates of x3,y3 which is gapSize<

相关标签:
3条回答
  • 2021-01-29 04:44

    There are many ways to do this. Simplest (to me) is the following. I'll write it in terms of mathematics since I can't even spell C.

    Thus, we wish to find the point C = {x3,y3}, given points A = {x1,y1} and B = {x2,y2}.

    The distance between the points is

    d = ||B-A|| = sqrt((x2-x1)^2 + (y2-y1)^2)
    

    A unit vector that points along the line is given by

    V = (B - A)/d = {(x2 - x1)/d, (y2-y1)/d}
    

    A new point that lies a distance of gapSize away from B, in the direction of that unit vector is

    C = B + V*gapSize = {x2 + gapSize*(x2 - x1)/d, y2 + gapSize*(y2 - y1)/d}
    
    0 讨论(0)
  • 2021-01-29 04:47

    You can simply calculate the angle in radians as

    double rads = atan2(y2 - y1, x2 - x1);
    

    Then you get the coordinates as follows:

    double x3 = x2 + gapSize * cos(rads);
    double y3 = y2 + gapSize * sin(rads);
    

    Is this what you meant?

    0 讨论(0)
  • 2021-01-29 04:47

    Compute the distance between P1 and P2: d=sqrt( (y2-y1)^2 + (x2-x1)^2)

    Then x2 = (d*x1 + gapSize*x3) / (d+gapSize)

    So x3 = (x2 * (d+gapSize) - d*x1) / gapSize

    Similarly, y3 = (y2 * (d+gapSize) - d*y1) / gapSize

    Sorry for the math. I didn't try to code it but it sounds right. I hope this helps.

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