Skip to content

Commit

Permalink
Added solar system demo to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-mcdaniel committed Sep 18, 2024
1 parent 50b934a commit 57e5f59
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
33 changes: 18 additions & 15 deletions docs/sage/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@
main();`);
break;
case "solar-system":

// Get current year, month, day in JS
let today = new Date();
let year = today.getFullYear();
Expand All @@ -361,20 +360,25 @@
document.getElementById("input").value = `${year} ${month} ${day}`;

source.setValue(`// Draw an *accurate* model of the solar system!
// Change the date in the box below the output
// Change the date in the input box (next to buttons below)
// to see the positions of the planets on that date!
from std.time import Instant, Date;
from std.physics import Point3D;
from std.physics.astronomy import Planet, KeplerianElements;
extern fun eval(str: &mut Char, size: Int);
fun main() {
// Get the date from the user
let (mut year, mut month, mut day) = (0, 0, 0);
print("Enter year: "); input(&mut year); println();
print("Enter month: "); input(&mut month); println();
print("Enter day: "); input(&mut day); println();
let mut year = 0;
let mut month = 0;
let mut day = 0;
print("Enter year: ");
input(&mut year);
println();
print("Enter month: ");
input(&mut month);
println();
print("Enter day: ");
input(&mut day);
let d = Date.date(year, month, day);
print("Performing calculations for date ");
d.print();
Expand All @@ -393,32 +397,31 @@
];
println("Mercury is white, Venus is green, Earth is blue, Mars is red.");
let au_to_pixel_ratio = (WIDTH / 2 + HEIGHT / 2) / 2 * 0.8;
let radius_scale = 600;
let radius_scale = 700;
for let mut i=0; i<4; i+=1; {
let planet = Planet.PLANETS[i];
let pos = planet.position_on_date(d);
let color = colors[i];
let x = pos.x * au_to_pixel_ratio + WIDTH / 2;
let y = -pos.y * au_to_pixel_ratio + HEIGHT / 2;
let radius = radius_scale * m_to_au(planet.radius()) * au_to_pixel_ratio;
let radius = radius_scale * planet.radius() * au_to_pixel_ratio;
planet.print();
println(" is at ", pos, " (AU, heliocentric).");
println(" is at ", pos, " (AU, heliocentric).", " ", planet.radius());
canvas.draw_circle(color, x as Int, y as Int, radius as Int);
}
// Draw the sun
let sun_radius = 20 * m_to_au(6.96e8) * au_to_pixel_ratio;
canvas.draw_circle(Color.YELLOW, WIDTH / 2, HEIGHT / 2, sun_radius as Int);
println("Hello, world!");
return ();
}
fun m_to_au(m: Float): Float {
return m / 149597870700.0;
}
extern fun eval(str: &mut Char, size: Int);
impl Float {
fun abs(self): Float {
if self < 0.0 {
Expand Down
Binary file modified docs/sage/web/web_bg.wasm
Binary file not shown.
33 changes: 18 additions & 15 deletions examples/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@
main();`);
break;
case "solar-system":

// Get current year, month, day in JS
let today = new Date();
let year = today.getFullYear();
Expand All @@ -361,20 +360,25 @@
document.getElementById("input").value = `${year} ${month} ${day}`;

source.setValue(`// Draw an *accurate* model of the solar system!
// Change the date in the box below the output
// Change the date in the input box (next to buttons below)
// to see the positions of the planets on that date!
from std.time import Instant, Date;
from std.physics import Point3D;
from std.physics.astronomy import Planet, KeplerianElements;
extern fun eval(str: &mut Char, size: Int);
fun main() {
// Get the date from the user
let (mut year, mut month, mut day) = (0, 0, 0);
print("Enter year: "); input(&mut year); println();
print("Enter month: "); input(&mut month); println();
print("Enter day: "); input(&mut day); println();
let mut year = 0;
let mut month = 0;
let mut day = 0;
print("Enter year: ");
input(&mut year);
println();
print("Enter month: ");
input(&mut month);
println();
print("Enter day: ");
input(&mut day);
let d = Date.date(year, month, day);
print("Performing calculations for date ");
d.print();
Expand All @@ -393,32 +397,31 @@
];
println("Mercury is white, Venus is green, Earth is blue, Mars is red.");
let au_to_pixel_ratio = (WIDTH / 2 + HEIGHT / 2) / 2 * 0.8;
let radius_scale = 600;
let radius_scale = 700;
for let mut i=0; i<4; i+=1; {
let planet = Planet.PLANETS[i];
let pos = planet.position_on_date(d);
let color = colors[i];
let x = pos.x * au_to_pixel_ratio + WIDTH / 2;
let y = -pos.y * au_to_pixel_ratio + HEIGHT / 2;
let radius = radius_scale * m_to_au(planet.radius()) * au_to_pixel_ratio;
let radius = radius_scale * planet.radius() * au_to_pixel_ratio;
planet.print();
println(" is at ", pos, " (AU, heliocentric).");
println(" is at ", pos, " (AU, heliocentric).", " ", planet.radius());
canvas.draw_circle(color, x as Int, y as Int, radius as Int);
}
// Draw the sun
let sun_radius = 20 * m_to_au(6.96e8) * au_to_pixel_ratio;
canvas.draw_circle(Color.YELLOW, WIDTH / 2, HEIGHT / 2, sun_radius as Int);
println("Hello, world!");
return ();
}
fun m_to_au(m: Float): Float {
return m / 149597870700.0;
}
extern fun eval(str: &mut Char, size: Int);
impl Float {
fun abs(self): Float {
if self < 0.0 {
Expand Down
8 changes: 4 additions & 4 deletions src/frontend/std_lib.sg
Original file line number Diff line number Diff line change
Expand Up @@ -1429,16 +1429,16 @@ mod physics {

// Orbital radius in AU
fun orbital_radius(self): Float {
return Planet.ORBITAL_RADII_METERS[self.order_from_sun()];
return Planet.ORBITAL_RADII_AU[self.order_from_sun()];
}

fun orbital_period(self): Float {
return Planet.ORBITAL_PERIODS[self.order_from_sun()];
}

/// Get the radius of the planet in meters.
/// Get the radius of the planet in AU.
fun radius(self): Float {
match self {
(match self {
of Mercury => 2439700.0,
of Venus => 6051800.0,
of Earth => 6371000.0,
Expand All @@ -1447,7 +1447,7 @@ mod physics {
of Saturn => 58232000.0,
of Uranus => 25362000.0,
of Neptune => 24622000.0
}
}) / 149597870700.0
}

// Get the keplerian elements associated with the
Expand Down

0 comments on commit 57e5f59

Please sign in to comment.