DCuser
DCuser

Reputation: 1003

Find 3D point along the line at given distance

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 origin O(0,0,0) and I would like to find the coordinates of a point B(x2,y2,z2) that is located on the line OA, and the distance OB is 1.2 times greater then OA.

So, my idea is to obtain the equation of the line formed by points O and A. The direction of OA is (-x1, -y1, -z1), so the equation of the line is:

x = -x1*t;

y = -y1*t;

z = -z1*t;

Distance OA is sqrt( (x1-0)^2 + (y1-0)^2 + (z1-0)^2). KNOWN

Distance OB is sqrt( (x2-0)^2 + (y2-0)^2 + (z2-0)^2). UNKNOWN

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

So, sqrt( (-x1*t-0)^2 + (-y1*t-0)^2 + (-z1*t-0)^2) = 1.2 * dist(OA).

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.

EDIT: 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

Upvotes: 3

Views: 5872

Answers (1)

bartlaarhoven
bartlaarhoven

Reputation: 845

It is even a lot easier; you can just multiply a, b and c by 1.2. This gives a line that is 1.2 times the size of the original line.

Upvotes: 4

Related Questions