Skip to content

Instantly share code, notes, and snippets.

@davidcalhoun
Last active November 21, 2019 04:57
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 davidcalhoun/d3416a79c2b8a342e63b9a0b47371300 to your computer and use it in GitHub Desktop.
Save davidcalhoun/d3416a79c2b8a342e63b9a0b47371300 to your computer and use it in GitHub Desktop.
satellite azim,elev to cartesian coords
const elevToHypLength = (elev, canvasSize) => {
return canvasSize - (canvasSize * elev) / 89.99999999;
}
const deg2rad = deg => (Math.PI * 2 * deg) / 360;
const toFixedFloat = (num = 0, digitsAfterDecimal = 0) => parseFloat(num.toFixed(digitsAfterDecimal))
const getXY = (azim, elev, canvasSize) => {
const hyp = elevToHypLength(elev, canvasSize / 2);
const angle = deg2rad(azim % 90);
const opp = toFixedFloat(Math.sin(angle) * hyp, 2);
const adj = toFixedFloat(Math.cos(angle) * hyp, 2);
// return x, y coords in the orientation of compass directions.
// 0 = N, 90 = E, 180 = S, 270 = W
// need to do some basic coord transforms because the triangle math won't work without 90 deg
// triangles (degress 0-90 only), also to account for compass directions, which are rotated and
// flipped, as opposed to standard math orientation
if (azim >= 0 && azim < 90) {
return [opp, adj];
}
if (azim >= 90 && azim < 180) {
return [adj, -opp];
}
if (azim >= 180 && azim < 270) {
return [-opp, -adj];
}
if (azim >= 270 && azim < 360) {
return [-adj, opp];
}
return [0, 0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment