Last active
August 29, 2015 14:06
-
-
Save grifdail/452ab1762a95ceb7f5b5 to your computer and use it in GitHub Desktop.
Change coordinate in 2D and 3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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