# Creating a world map of where it's nighttime or daytime

**URL:** <https://discourse.processing.org/t/creating-a-world-map-of-where-its-nighttime-or-daytime/39584>\
**Category:** Coding Questions\
**Created:** [November 4, 2022, 7:36pm UTC](https://discourse.processing.org/t/creating-a-world-map-of-where-its-nighttime-or-daytime/39584 "2022-11-04T19:36:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David1](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@David1](https://discourse.processing.org/u/David1)\
**Post date:** [November 4, 2022, 7:36pm UTC](https://discourse.processing.org/t/creating-a-world-map-of-where-its-nighttime-or-daytime/39584/1 "2022-11-04T19:36:08Z")

</div>

Hello all,

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](https://www.timeanddate.com/worldclock/sunearth.html)

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?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [November 4, 2022, 8:49pm UTC](https://discourse.processing.org/t/creating-a-world-map-of-where-its-nighttime-or-daytime/39584/2 "2022-11-04T20:49:44Z")

</div>

Hello @David1

That sounds like an interesting project!!

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.

> **[World Time API](https://dev.timeanddate.com/time/)**
>
> World Clock API with current time, time zone, Daylight Saving Time (DST), and time zone converter for any location. Rest API developed by timeanddate.com.

Good luck!  
🤓

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [November 4, 2022, 9:59pm UTC](https://discourse.processing.org/t/creating-a-world-map-of-where-its-nighttime-or-daytime/39584/3 "2022-11-04T21:59:28Z")

</div>

`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).

```auto
// https://www.solarsystemscope.com/textures/download/2k_earth_daymap.jpg
// https://www.solarsystemscope.com/textures/download/2k_earth_nightmap.jpg

PImage daymap;
PImage nightmap;
PShader shdr;

void setup() {
  size( 2048, 1024, P2D );
  shdr = new PShader( g.parent, vertSrc, fragSrc );
  daymap = loadImage("2k_earth_daymap.jpg");
  nightmap = loadImage("2k_earth_nightmap.jpg");
  shdr.set("daymap", daymap);
  shdr.set("nightmap", nightmap);
}

void draw() {
  shdr.set( "time", frameCount/600. % 1 );
  shdr.set( "date", frameCount/3600. % 1 ); // Nov 4 is 0.87
  shader( shdr );
  rect( 0, 0, width, height );
}

String[] vertSrc = { """
#version 330
uniform mat4 transformMatrix;
in vec4 position;
void main() {
  gl_Position = transformMatrix * position;
}
""" };

String[] fragSrc = { """
#version 330
precision highp float;
uniform vec2 resolution;
uniform sampler2D daymap;
uniform sampler2D nightmap;
uniform float time;
uniform float date;
#define TAU 6.28318530178

out vec4 fragColor;

vec2 rot( in vec2 p, float a ) {
   return cos(a)*p + sin(a)*vec2(p.y, -p.x);
}

void main() {
  vec2 sp = gl_FragCoord.xy/resolution-0.5;
  vec3 p = vec3( -1, 0, 0 );
  p.xz = rot( p.xz, sp.y*TAU*0.5 ); // latitude
  p.xy = rot( p.xy, sp.x*TAU ); // longitude
  
  p.xy = rot( p.xy, -TAU*date ); // counter-rotate the sun
  p.xy = rot( p.xy, TAU*time ); // time of day
  p.zx = rot( p.zx, TAU/360*23.5 ); // tilt of the Earth
  p.xy = rot( p.xy, TAU*date ); // date of year

  vec2 uv = gl_FragCoord.xy/resolution;
  uv.y = 1.0 - uv.y;
  vec3 day = texture( daymap, uv ).rgb;
  vec3 night = texture( nightmap, uv ).rgb;
  vec3 col = mix( night, day, smoothstep( -0.1, 0.05, p.x ) );

  fragColor = vec4( col, 1. );
}
""" };

```

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [November 4, 2022, 10:55pm UTC](https://discourse.processing.org/t/creating-a-world-map-of-where-its-nighttime-or-daytime/39584/4 "2022-11-04T22:55:20Z")

</div>

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.
