Alright, so I’ve been Googling for several days and cannot figure how to solve what I think is rather simple…? I’ve also searched the forum here and will list at the end some of the links I’ve explored.

Context: In 2D I can find the angle from one point to another using atan2 and create a line from one point in the direction of the other point with any desired length. I have some sample code below that has an origin at the center of the window and a secondary point that is essentially the mouseX and mouseY values. A line points from mouseX and mouseY towards the origin.x and origin.y.

Problem: My question is what is the equivalent to the above 2D solution in 3D? Create a line from one point with a given radius towards another in 3D space. From my searching I can place a point with x, y, and z values on the surface of a sphere given an origin, radius, theta, and phi. I believe this is called spherical to Cartesian coordinates. However, I cannot seem to be able to draw a line from a 3D point on the sphere surface back to the origin. I can seem to get theta and phi from a point on a sphere surface that when converted to Cartesian coordinates returns the origin point.

2D example:

```
PVector origin; // center of window
PVector target; // mouse position
void setup() {
size(500, 500, P3D);
origin = new PVector(width/2, height/2);
target = new PVector();
}
void draw() {
background(255);
// set target to mouse position
target.x = mouseX;
target.y = mouseY;
target.z = 100;
// calculate angle from target/mouse to origin point
float angle = atan2(origin.y - target.y, origin.x - target.x);
float radius = 50;
// caluclate new point form target towards origin with length radius
float x = target.x + cos(angle) * radius;
float y = target.y + sin(angle) * radius;
// display origin and angle
fill(0);
stroke(0);
strokeWeight(10);
point(origin.x, origin.y, origin.z);
strokeWeight(5);
line(target.x, target.y, x, y);
// what is the equivalent in 3D?
// how can i draw a line with radius 50 from target pointing towards origin?
}
```

From my searching I’ve found the following:

```
float dx = origin.x - target.x;
float dy = origin.y - target.y;
float dz = origin.z - target.z;
float theta = atan2(dz, dx);
float phi = atan2(sqrt(sq(dz) + sq(dx)), dy)
// or:
float phi = atan2(sqrt(sq(dz) + sq(dx)), dy) * PI
// this can be converted to new points
// however this does not point back to origin
float x = target.x + radius * sin(phi) * cos(theta);
float y = target.y + radius * sin(phi) * sin(theta);
float z = target.z + radius * cos(phi);
```

I tried exploring the code from this question as well to no success:

Any tips or suggestions would be greatly appreciated.