I am fairly new to Processing, and I would like to create a world map of where it’s daytime and where it’s nighttime on the planet. Essentially, trying to emulate this: Day and Night World Map
I have code to bring in two images (one of the earth during the day, and one of the earth during the night) and I know how to stitch them together, depending if it’s daytime or nighttime at a specific location. However, what I don’t have is an algorithm to figure out if it’s daytime or nighttime at a specific latitude or longitude (which I would then convert to a specific pixel location).
Does anyone know of an algorithm to do this? I’m think the easiest way to do this would be to: for every latitude, calculate at what longitude it’s currently sunrise and what longitude it’s currently sunset, and then shade the latitudal line between these two locations accordingly. Alternatively, I could try to calculate for every latitude what time is sunrise and what time is sunset, and then along that latitude, I could go to every longitude and determine the local time at that location and see if it’s night or day.
Thoughts? This seems like a tough calculation to accomplish that would require a lot of trigonometry that is beyond me. Alternatively, is there any sort of Astronomy library that has this information that would be easily accessible?
It might be worth looking into this Time API, where you can search for location via longitude and latitude.
It looks like a fee is involved though a free trial is available. At the very least it may provide some clues on how to proceed.
time is UTC time going from 0 to 1 from midnight each day. date is part of the year from 0 to 1 with 0 at winter solstice, 0.5 at summer solstice (so day number of the year plus 10 and divide by 365.25).
Oops, it looks right, but the rotation dealing with the Earth’s tilt isn’t quite right – it rotates the Earth along with rotating the axis, so midnight ends up during the day on the summer solstice. Gotta think it through a bit more…
Edit: fixed. The code posted above should be correct now.
I did this off the top of my head. It would be good if someone could verify that my math is correct.