Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Find 3D point along the line at given distance

  1. Oct 22, 2012 #1
    Hello,

    I have asked this question already but have not been able to get conclusive answer.

    I have a problem and please let me know if my solution is correct.

    I have a known point, at location A(x1,y1,z1) and the point B(x2,y2,z2) and I would like to find the coordinates of a third point C(x3,y3,z3) that is located on the line AB, and the distance AC is 1.2 times greater then AB.

    So, my idea is to obtain the equation of the line formed by points A and B. The direction of AB is (x2-x1, y2-y1, z2-z1), so the equation of the line is:

    x = x1-(x2-x1)*t;

    y = y1-(y2-y1)*t;

    z = z1-(z2-z1)*t;

    Distance AB is sqrt( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2). KNOWN

    Distance AC is sqrt( (x3-x1)^2 + (y3-y1)^2 + (z3-z1)^2). UNKNOWN

    I can replace the x, y, z points determined for the line equation in the distance AC, and the result should be 1.2 times greater then the distance AB.

    So, sqrt( (x1-(x2-x1)*t-x1)^2 + (y1-(y2-y1)*t-0)^2 + (z1-(z2-z1)*t-0)^2) = 1.2 * dist(AB).

    I find t from here, solving the quadratic equation and I obtain the coordinates of the point by replacing the t in the equation of the line.

    Is this correct?

    Thank you for your time.
     
  2. jcsd
  3. Oct 22, 2012 #2

    chiro

    User Avatar
    Science Advisor

    Hey catalin.drago and welcome to the forums.

    With regards to your parameterization, you should have the parametrization x = a + (b-a)t instead of x = a - (b-a)t if your direction is along the vector (b-a) instead of in the reverse.

    Basically you want to find t that corresponds to the AB and then plug in t' = 1.2*t and then calculate the length of t' * ||n|| where n is your direction vector used for your line: in the above n = (b-a) but you can normalize this (and I suggest you do to make things easier).

    With regards to getting the value of t recall that when t = 0 you get <x1,y1,z1> = A and then for t you get <x1,y1,z1> + n*t = B but since n and A are known you can just get n*t = B - A and since ||n|| = 1 then t = ||B - A||.
     
  4. Oct 22, 2012 #3
    Thank you for the reply.

    So far I came up with the code below. I tried to verify the colinearity condition with the cross product of the vectors (P3-P1) and (P2-P1), but I don't get zero in all cases.

    What am I doing wrong?

    norm = sqrt((P2(1,1) - P1(1))^2 + (P2(1,2) - P1(2))^2 + (P2(1,2) - P1(1))^2);

    P3(1) = P1(1) + ((P2(1) - P1(1)) /norm) * rangeRatio;
    P3(2) = P1(2) + ((P2(2) - P1(2)) /norm) * rangeRatio;
    P3(3) = P1(3) + ((P2(3) - P1(3)) /norm) * rangeRatio;

    I have a point P1, and a point P2 and I would like to find a point P3 on the line of P1-P2, where dist P1-P3 is rangeRatio greater then the distance P1-P2

    Do you have any idea where I go wrong?

    P.S. It's Matlab code, so P1(1) is x, P1(2) is y, and P1(3) is z.

    Thank you.
     
  5. Oct 22, 2012 #4

    mathman

    User Avatar
    Science Advisor
    Gold Member

    Don't divide by the norm! Think about having range ratio = 1. If you don't divide by the norm you will get P2 as expected.
     
  6. Oct 23, 2012 #5
    Thank you for the reply.
    I have implemented the code without division by norm, and I have tested the colinearity by applying the cross product on MB * MA, and by checking the slope between the MB and MA (for e.g.: (y1 - y2) * (x1 - x3) - (y1 - y3) * (x1 - x2) should be zero is all three points are colinear).
    When I apply the cross product one axis is 0 and the rest very close to 0 (like x= 0 y= 0.005 and z = 0.02) and very rare I get all axis values equal to 0.
    The same happens also with the slope computation. Instead of getting always 0, I sometimes get values like 0.03 or 0.007.
    DO you have any idea why?
    Is it normal?
    Or can it be a tolerated error?
     
  7. Oct 23, 2012 #6

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    How many decimal places are you keeping as you do the calculations? It is typically a good idea to keep one or two more decimal places in each calculation than you want in the final answer, then round off at the end. Otherwise, "round off error" can build up from calculation to calculation.
     
  8. Oct 23, 2012 #7
    Hello,
    I tried rounding to 2 decimals and i still get in some cases non colinearity.
    This is my code:

    rangeRatio = 1.114;

    norm = sqrt((P2(1) - P1(1))^2 + (P2(2) - P1(2))^2 + (P2(3) - P1(3))^2);

    P3(1) = P1(1) + ((P2(1,1) - P1(1)) /norm) * rangeRatio;
    P3(2) = P1(2) + ((P2(1,2) - P1(2)) /norm) * rangeRatio;
    P3(3) = P1(3) + ((P2(1,3) - P1(3)) /norm) * rangeRatio;

    I tried also norm = 1, and i get slightly different results but still not always colinear.

    Thank you
     
  9. Oct 23, 2012 #8

    mathman

    User Avatar
    Science Advisor
    Gold Member

    To test it out I suggest using range ratio = 1. You then should get P2 exactly. If not, there is a bug in your code.

    Your last post has P2(1,k). Why the extra index?

    Do not divide by norm!
     
  10. Oct 24, 2012 #9
    Hello,

    I have tested with ratio 1 and i am getting P2, so it seems correct, but once I apply the ratio 1.11 things change.
    I am trying to test colinearity by applying the cross product on vectors (P3-P1) and (P2-P1). It's a correct test right?

    Would it be correct to verify colinearity by checking that the distance from P1 to P2 + distance P2 to P3 is equal to the distance from P1 to P1?

    Could the different results be caused by a numerical instability?
     
    Last edited: Oct 24, 2012
  11. Oct 24, 2012 #10

    chiro

    User Avatar
    Science Advisor

    You can check collinearity of n vectors by throwing them in a matrix and doing row reduction.

    But if you only compare two then you can either do dot or cross product (cross product is actually easier since with dot product you need to check that |<a,b>|/(||a||*||b||) = 1)
     
  12. Oct 24, 2012 #11
    I was trying to compute the area of the triangle formed by the points, thinking that if the area is 0 then the points are colinear, and I am getting the area either 0 or 0 real an dan imaginary part....is that normal?
    why am i getting an imaginary part?
     
  13. Oct 24, 2012 #12

    mathman

    User Avatar
    Science Advisor
    Gold Member

    My guess: roundoff like problems.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook