We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there, please could somebody show me how to get the same x,y,z result but having the origin at c? I appreciate this is mainly a maths problem and sorry if has been answered before but I have been searching/trying to work this out for days now. Thank you
PVector a;
PVector b;
PVector c;
void setup(){
size(500,500,P3D);
a = new PVector(300, 154, 407);
b = new PVector(0, 0, 0);
c = new PVector(55, 233, 82);
}
void draw(){
background(255);
float rad = PVector.dist(a, b);
float theta = atan2(a.z, a.x);
float phi = acos(a.y/rad);
float x = rad * sin(phi) * cos(theta);
float y = rad * cos(phi);
float z = rad * sin(phi) * sin(theta);
println(x,y,z);
}
Answers
The solution is to subtract the origin coordinates from the vector
a
before calculating the spherical coordinates.When transforming back to cartesian coordinates you need to add the origin coordinates after calculating the cartesian coordinates.
If you are going to be doing a lot of these calculations then I suggest you creates functions to do it for you.
Out of curiosity I have created two functions to convert
cartesian --> spherical coordinates, and
spherical --> cartesian coordinates
If you want, I will post them here provided you assure me that it is not part of some academic assessment.
Thanks very much quark,
I assure you my academic years are well behind me and use processing purely as a hobby.
OK well here is the code. Hopefully the comments explain what is happening but if you have any questions please ask.
Thanks again quark :)