Browse Source

Add human-readable output mode with polar coordinates

Add --readable (-r) flag for headless mode that displays:
- Positions in AU instead of meters
- Velocities in km/s instead of m/s
- Polar coordinates (r, θ) in AU and degrees

Changes:
- New command line flag: --readable / -r
- Helper functions for formatted output
- Polar angle calculated from atan2(y,x) and converted to 0-360°
- Periodic updates show: Body(r=X.XX AU, θ=Y.Y°)
- Initial/final states show full cartesian + polar coordinates

Makes verification much easier - can instantly see orbital positions
and confirm bodies return to starting angles after full orbits.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
main
cinnaboot 6 months ago
parent
commit
40a83ad241
  1. 80
      src/main.cpp

80
src/main.cpp

@ -5,16 +5,53 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
// Constants for unit conversion
const double AU = 1.496e11; // 1 AU in meters
const double KM = 1000.0; // 1 km in meters
// Helper functions for human-readable output
void print_position_readable(const char* name, Vec3 pos) {
double dist_au = vec3_magnitude(pos) / AU;
double x_au = pos.x / AU;
double y_au = pos.y / AU;
double z_au = pos.z / AU;
// Calculate polar coordinates (angle in XY plane)
double angle_rad = atan2(pos.y, pos.x);
double angle_deg = angle_rad * 180.0 / M_PI;
if (angle_deg < 0.0) angle_deg += 360.0; // Convert to 0-360 range
printf("%s:\n", name);
printf(" Position: (%.6f, %.6f, %.6f) AU\n", x_au, y_au, z_au);
printf(" Polar (XY plane): r=%.6f AU, θ=%.2f°\n", dist_au, angle_deg);
}
void print_velocity_readable(const char* name, Vec3 vel) {
double speed_km_s = vec3_magnitude(vel) / KM;
printf(" Velocity magnitude: %.3f km/s\n", speed_km_s);
printf(" Velocity: (%.3f, %.3f, %.3f) km/s\n",
vel.x / KM, vel.y / KM, vel.z / KM);
}
void print_body_readable(CelestialBody* body) {
print_position_readable(body->name, body->position);
print_velocity_readable(body->name, body->velocity);
}
int main(int argc, char** argv) {
// Parse command line arguments
const char* config_file = "configs/solar_system.txt";
bool headless = false;
bool readable = false; // Human-readable output (AU, km, etc.)
int sim_duration_days = 365; // Default: 1 year for headless mode
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--headless") == 0 || strcmp(argv[i], "-h") == 0) {
headless = true;
} else if (strcmp(argv[i], "--readable") == 0 || strcmp(argv[i], "-r") == 0) {
readable = true;
} else if (strcmp(argv[i], "--days") == 0 && i + 1 < argc) {
sim_duration_days = atoi(argv[++i]);
} else if (argv[i][0] != '-') {
@ -27,6 +64,9 @@ int main(int argc, char** argv) {
if (headless) {
printf("Mode: Headless (terminal output only)\n");
printf("Duration: %d days\n", sim_duration_days);
if (readable) {
printf("Output: Human-readable units (AU, km/s)\n");
}
}
// Create simulation with time step of 60 seconds
@ -51,11 +91,15 @@ int main(int argc, char** argv) {
printf("\n=== Initial State ===\n");
for (int i = 0; i < sim->body_count; i++) {
CelestialBody* body = &sim->bodies[i];
printf("%s:\n", body->name);
printf(" Position: (%.3e, %.3e, %.3e) m\n",
body->position.x, body->position.y, body->position.z);
printf(" Velocity: (%.3e, %.3e, %.3e) m/s\n",
body->velocity.x, body->velocity.y, body->velocity.z);
if (readable) {
print_body_readable(body);
} else {
printf("%s:\n", body->name);
printf(" Position: (%.3e, %.3e, %.3e) m\n",
body->position.x, body->position.y, body->position.z);
printf(" Velocity: (%.3e, %.3e, %.3e) m/s\n",
body->velocity.x, body->velocity.y, body->velocity.z);
}
}
printf("\n=== Running Simulation ===\n");
@ -66,8 +110,16 @@ int main(int argc, char** argv) {
if (sim->time >= next_output_time) {
printf("Day %.1f: ", sim->time / SECONDS_PER_DAY);
for (int i = 0; i < sim->body_count && i < 3; i++) {
double dist = vec3_magnitude(sim->bodies[i].position);
printf("%s=%.3e m ", sim->bodies[i].name, dist);
Vec3 pos = sim->bodies[i].position;
double dist = vec3_magnitude(pos);
if (readable) {
double angle_rad = atan2(pos.y, pos.x);
double angle_deg = angle_rad * 180.0 / M_PI;
if (angle_deg < 0.0) angle_deg += 360.0;
printf("%s(r=%.4f AU, θ=%.1f°) ", sim->bodies[i].name, dist / AU, angle_deg);
} else {
printf("%s=%.3e m ", sim->bodies[i].name, dist);
}
}
printf("\n");
next_output_time += output_interval;
@ -77,11 +129,15 @@ int main(int argc, char** argv) {
printf("\n=== Final State (Day %.1f) ===\n", sim->time / SECONDS_PER_DAY);
for (int i = 0; i < sim->body_count; i++) {
CelestialBody* body = &sim->bodies[i];
printf("%s:\n", body->name);
printf(" Position: (%.3e, %.3e, %.3e) m\n",
body->position.x, body->position.y, body->position.z);
printf(" Velocity: (%.3e, %.3e, %.3e) m/s\n",
body->velocity.x, body->velocity.y, body->velocity.z);
if (readable) {
print_body_readable(body);
} else {
printf("%s:\n", body->name);
printf(" Position: (%.3e, %.3e, %.3e) m\n",
body->position.x, body->position.y, body->position.z);
printf(" Velocity: (%.3e, %.3e, %.3e) m/s\n",
body->velocity.x, body->velocity.y, body->velocity.z);
}
}
destroy_simulation(sim);

Loading…
Cancel
Save