I hope the following code might help you. I did this for own sketches and think that it is mathematically correct.

The main thing is the function cartesianToPolar that returns a PVector with (length, angleY, angleZ). The code in setup() is just to show you, how to use the function.

**Code:**

void setup() {

size(800, 800, P3D);

PVector location1 = new PVector(-3, 2, -3);

PVector location2 = new PVector(4, -1, -2);

PVector d = PVector.sub(location2, location1);

println("vector from location1 to location2 : " + d);

PVector polar = cartesianToPolar(d);

println("length of this vector : " + polar.x);

println("rotation angle around y-axis : " + polar.y);

println("rotation angle around z-axis : " + polar.z);

// draw the line

translate (width/2, height/2);

scale(100);

line(location1.x, location1.y, location1.z, location2.x, location2.y, location2.z);

// draw the rectangle

fill(255, 0, 0, 100);

noStroke();

rectMode(CORNERS);

translate(location1.x, location1.y, location1.z);

rotateY(polar.y);

rotateZ(polar.z);

rect(0, 0, polar.x, 1);

}

// Converts 3D cartesian coordinates to polar coordinates

//

// theVector : vector to convert

// returns : vector containing 'length', 'angleY' and 'angleZ',

// so that rotating a point (length, 0, 0) first

// around the y-axis with angleY and then around the

// z-axis with angleZ results again in point (x, y, z)

PVector cartesianToPolar(PVector theVector) {

PVector res = new PVector();

res.x = theVector.mag();

if (res.x > 0) {

res.y = -atan2(theVector.z, theVector.x);

res.z = asin(theVector.y / res.x);

}

else {

res.y = 0;

res.z = 0;

}

return res;

}