Skip to content

Instantly share code, notes, and snippets.

@grifdail
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grifdail/452ab1762a95ceb7f5b5 to your computer and use it in GitHub Desktop.
Save grifdail/452ab1762a95ceb7f5b5 to your computer and use it in GitHub Desktop.
Change coordinate in 2D and 3D
function carToPol(vec) {
var r = Math.sqrt(vec.x * vec.x + vec.y * vec.y);
var a = Math.atan2(vec.y, vec.x);
return {
r:r,
a:a
}
}
function polToCar (coor) {
return {
x: coor.r * Math.cos(coor.a),
y: coor.r * Math.sin(coor.a)
};
}
function carToCyl (vec) {
var coor = carToPol(vec);
coor.z = vec.z;
return coor;
}
function cylToCar (coor) {
var vec = polToCar(coor);
vec.z = coor.z;
return z;
}
function carToSphere (vec) {
var r = Math.sqrt(vec.x*vec.x + vec.y*vec.y + vec.z * vec.z);
var lon = Math.atan2(vec.y,vec.x);
var lat = Math.acos(vec.y/r);
return {
r:r,
lat:lat,
lon: lon
}
}
function sphereToCar (coor) {
return {
x: Math.cos(coor.lon) * coor.r,
y: Math.sin(coor.lon) * coor.r,
z: Math.cos(coor.lat) * coor.r
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment