how to move a point outside a sphere along a line

218 Views Asked by At

May the coordinates of a sphere with centre C and its radius r be given. Also a point P, which has to be moved outside the sphere (i.e. onto its surface), if the point is inside the sphere. P must be moved in the direction of CP = P - C.

I believe this approach is wrong: P_new = C + (r * CP). The distance between P_new and C has to be r. By multiplying r to the direction-vector-CP, only the axis x,y,z will be scaled, but the diagonal distance between C and P will not be r.

sphere

How do I move P_old along the line CP to P_new?

2

There are 2 best solutions below

0
On BEST ANSWER

The new point is just $$C+\frac{r\cdot CP}{|CP|}$$

0
On

You can think about it this way: The line on which you are allowed to move your point can be described like this $l: P(s) = P_{old} + s \cdot (C - P_{old})$. Every point on the sphere must satisfy $ |P - C| = r$. Now you can combine those two equations: Your goal is now to find $s$ such that $|P(s) - C| = r$. $$ |P(s) - C| = |P_{old} + s \cdot (C - P_{old}) - C| = |(P_{old} - C) - s \cdot(P_{old} - C)|$$ $$ = |(1 - s)(P_{old} - C)| = |1-s||P_{old} - C| \stackrel{!}{=} r$$

Thus you can calculate $s$ using the following equation: $$|s - 1| = r \div |P_{old} - C|$$

You know $r, P_{old}, C$. So you can calulate a value for the RHS and then by squaring both sides you get a quadratic equation for $s$ which should give you $2$ answers. (Because there are two solutions to your problem). Once you have a solution $s'$ you can use it to calculate $P_{new} = P_{old} + s' (P_{old} - C)$.