Skip to content

Instantly share code, notes, and snippets.

@ridixcr
Forked from mourner/sunpos.js
Created March 2, 2017 20:15
Show Gist options
  • Save ridixcr/025a955c3e536682c783a9498537a62c to your computer and use it in GitHub Desktop.
Save ridixcr/025a955c3e536682c783a9498537a62c to your computer and use it in GitHub Desktop.
Minimal sun position calculating function
function getSunPosition(date, lat, lng) {
var r = Math.PI / 180, sin = Math.sin, cos = Math.cos, t = date / 315576e7 - 0.3,
m = r * (357.52911 + t * (35999.05029 - t * 1537e-7)), c = cos(r * (125.04 - 1934.136 * t)),
l = r * ((280.46646 + t * (36000.76983 + t * 3032e-7)) + (1.914602 - t * (4817e-6 - t * 14e-6)) *
sin(m) - (569e-5 + 478e-5 * c)) + (0.019993 - 101e-6 * t) * sin(2 * m) + 289e-6 * sin(3 * m),
e = r * (84381.448 - t * (46.815 - t * (59e-5 + 1813e-6 * t))) / 3600 + r * 256e-5 * c,
d = Math.asin(sin(e) * sin(l)),
h = r * (280.46061837 + 13184999.8983375 * t) + r * lng - Math.atan2(cos(e) * sin(l), cos(l)),
z = Math.atan2(sin(h), cos(h) * sin(r * lat) - (sin(d) / cos(d)) * cos(r * lat)),
a = Math.asin(sin(r * lat) * sin(d) + cos(r * lat) * cos(d) * cos(h));
return {azimuth: z, altitude: a};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment