Skip to content

Instantly share code, notes, and snippets.

@tai2
Last active March 1, 2025 02:11
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 earth_angle = 1;
float mars_angle = 0;
float earth_velocity = -0.005;
PVector sun;
float mars_circle_extent = 150;
float planet_extent = 10;
void setup() {
size(1000, 1000);
sun = new PVector(width * 0.5, height * 0.5);
}
void draw() {
if (keyPressed == true) {
return;
}
background(255, 255, 255);
noStroke();
// Draw the Sun
noStroke();
fill(0xFF, 0xA5, 0x00);
circle(sun.x, sun.y, planet_extent);
// Draw Earth
PVector earth = getEarthPos(earth_angle, getEarthOrbitRadius());
noStroke();
fill(0, 0, 255);
circle(earth.x, earth.y, planet_extent);
earth_angle += PI * earth_velocity;
// Draw the orbit of Earth
stroke(0, 0, 0);
noFill();
ellipse(sun.x, sun.y, getEarthOrbitRadius() * 2, 0.98 * getEarthOrbitRadius() * 2);
// Draw Mars
PVector mars = getMarsPos(mars_angle, getMarsOrbitRadius());
noStroke();
fill(255, 0, 0);
circle(mars.x, mars.y, planet_extent);
mars_angle += PI * earth_velocity * 365.2422 / 686.980;
// Draw the orbit of Mars
stroke(0, 0, 0);
noFill();
ellipse(sun.x, sun.y, getMarsOrbitRadius() * 2, 0.94 * getMarsOrbitRadius() * 2);
// Draw the direction of view from Earth
PVector sight = new PVector(earth.x - mars.x, earth.y - mars.y);
sight.normalize();
stroke(0, 255, 0);
line(
mars.x - sight.x * 1000, mars.y - sight.y * 1000,
mars.x + sight.x * 1000, mars.y + sight.y * 1000
);
// Draw the orbit of Mars based on Earth
PVector mars_circle_pos = new PVector(width * 0.9, height * 0.1);
stroke(0, 0, 0);
circle(mars_circle_pos.x, mars_circle_pos.y, mars_circle_extent);
noStroke();
fill(0, 0, 255);
circle(mars_circle_pos.x, mars_circle_pos.y, planet_extent);
noStroke();
fill(255, 0, 0);
circle(
mars_circle_pos.x + mars_circle_extent * 0.5 * sight.x,
mars_circle_pos.y + mars_circle_extent * 0.5 * sight.y,
planet_extent
);
}
PVector getEarthPos(float angle, float radius) {
return new PVector(
sun.x + radius * sin(angle),
sun.y + 0.98 * radius * cos(angle)
);
}
PVector getMarsPos(float angle, float radius) {
return new PVector(
sun.x + radius * sin(angle),
sun.y + 0.94 * radius * cos(angle)
);
}
float getEarthOrbitRadius() {
return width * 0.15;
}
float getMarsOrbitRadius() {
return getEarthOrbitRadius() * 2.26;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment