Skip to content

Instantly share code, notes, and snippets.

@tai2
Last active May 20, 2025 08:39
Show Gist options
  • Save tai2/88f50432c73d227d83dca52359b2541b to your computer and use it in GitHub Desktop.
Save tai2/88f50432c73d227d83dca52359b2541b to your computer and use it in GitHub Desktop.
Explain the relation between Earth and Mars
float earthAngle = 0;
float marsAngle = 0;
float earthVelocity = 0.005;
void setup() {
size(1000, 1000);
noStroke();
}
void draw() {
if (keyPressed == true) {
return;
}
background(255, 255, 255);
noStroke();
float cx = width * 0.5;
float cy = height * 0.5;
fill(0xFF, 0xA5, 0x00);
circle(cx, cy, 10);
fill(0, 0, 255);
float earth_orbit_radius = width * 0.20;
float earthX = cx + earth_orbit_radius * sin(earthAngle);
float earthY = cy + earth_orbit_radius * cos(earthAngle);
circle(earthX, earthY, 10);
earthAngle += PI * 0.005;
float earth_tanX = cos(earthAngle);
float earth_tanY = -sin(earthAngle);
stroke(0, 0, 0);
noFill();
line(earthX - earth_tanX * 1000, earthY - earth_tanY * 1000, earthX + earth_tanX * 1000, earthY + earth_tanY * 1000);
stroke(0, 0, 0);
noFill();
circle(cx, cy, earth_orbit_radius * 2);
fill(255, 0, 0);
float mars_orbit_radius = width * 0.30;
float marsX = cx + mars_orbit_radius * sin(marsAngle);
float marsY = cy + mars_orbit_radius * cos(marsAngle);
circle(marsX, marsY, 10);
marsAngle += PI * earthVelocity * 0.5;
stroke(0, 0, 0);
noFill();
circle(cx, cy, mars_orbit_radius * 2);
float vx = earthX - marsX;
float vy = earthY - marsY;
float earth_mars_dist = sqrt(vx * vx + vy * vy);
vx /= earth_mars_dist;
vy /= earth_mars_dist;
float cross_prod = vx * earth_tanY - vy * earth_tanX;
float inner_prod = vx * earth_tanX + vy * earth_tanY;
if (cross_prod < 0) {
stroke(0, 255, 0);
} else {
stroke(255, 255, 0);
}
line(marsX - vx * 1000, marsY - vy * 1000, marsX + vx * 1000, marsY + vy * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment