diff --git a/main.c b/main.c index 28d9b562b50743de0252a976c104fb8d2d0ba6f0..3022f4ad445f9b41ebd38693e20a5112bf6dc1f7 100755 --- a/main.c +++ b/main.c @@ -4,32 +4,40 @@ #include <stdio.h> #include <stdlib.h> #include <time.h> +#include <unistd.h> #define SCREEN_WIDTH 1000 #define SCREEN_HEIGHT 1000 +#define SUN_RADIUS 50 + +#define DELTA_T 3600.0 + int main() { srand(time(NULL)); - struct gfx_context_t *ctxt = gfx_create("Planetary system", SCREEN_WIDTH, SCREEN_HEIGHT); + struct gfx_context_t* ctxt = gfx_create("Planetary system", SCREEN_WIDTH, SCREEN_HEIGHT); if (!ctxt) { fprintf(stderr, "Graphics initialization failed!\n"); return EXIT_FAILURE; } - // TODO : create your system - system_t sys = create_system(0.125); + // begin : create your system + system_t sys = create_system(DELTA_T); // end : create system while (true) { gfx_present(ctxt); gfx_clear(ctxt, COLOR_BLACK); - // TODO : draw the current state of your system + // begin : draw the current state of your system + show_system(ctxt, sys); // end : draw state of system - // TODO : update your system + // begin : update your system + update_system(sys, DELTA_T); + usleep(100000); // end : update system if (gfx_keypressed() == SDLK_ESCAPE) { break; } } - // TODO : Free your system + // begin : Free your system free_system(sys); // end : free system gfx_destroy(ctxt); diff --git a/planet/planet.c b/planet/planet.c index 7e0de59d7119363077a90462ff3c580237b1e119..a3f1f8e7536a18d5e07f132fdf67214dcb2c74df 100755 --- a/planet/planet.c +++ b/planet/planet.c @@ -4,25 +4,6 @@ #include "constants.h" -/* -typedef struct _planet -{ - double mass; - vec2 pos; // x(t) - vec2 prec_pos; // x(t - dt) -} planet_t; -typedef struct _system -{ - planet_t star; // ex. The sun - uint32_t nb_planets; // The number of orbiting planets - planet_t *planets; // An array of orbiting planets -} system_t; -*/ - - -// TODO : complete all implementations - - planet_t create_planet(double mass, vec2 pos) { planet_t p; p.mass = mass; @@ -74,7 +55,17 @@ system_t create_system(double delta_t) { void show_system(struct gfx_context_t* ctxt, system_t* system) { - //TODO + // draw sun + const coordinates xy_sun = vec2_to_coordinates_centered(vec2_normalize(system->star.pos)); + draw_full_circle(ctxt, xy_sun.column, xy_sun.row, 50, COLOR_YELLOW); + // draw planets + for (uint32_t i = 0; i < sys.nb_planets; ++i) { + const coordinates xy = vec2_to_coordinates_centered( + vec2_normalize(sys.planets[i].pos), + ctxt->width, + ctxt->height); + draw_full_circle(ctxt, xy.column, xy.row, 20, COLOR_BLUE); + } } diff --git a/vec2/vec2.c b/vec2/vec2.c index cc2e543a24de1f1e8e8c830143da1ea7247666a8..b8991e65a424e11db7eb3a471edb33fd7bba75a3 100755 --- a/vec2/vec2.c +++ b/vec2/vec2.c @@ -7,7 +7,7 @@ /// @param y_ The second component. /// @return The newly created vector. vec2 vec2_create(double x_, double y_) { - return (vec2){ .x = x_ , .y = y_ }; + return (vec2) {.x = x_, .y = y_}; } /// Create a zero 2d vector. @@ -21,7 +21,7 @@ vec2 vec2_create_zero() { /// @param rhs The right operand. /// @return The sum in a new vector. vec2 vec2_add(vec2 lhs, vec2 rhs) { - return (vec2){ .x = lhs.x + rhs.x , .y = lhs.y + rhs.y }; + return (vec2) {.x = lhs.x + rhs.x, .y = lhs.y + rhs.y}; } /// Substract two vectors. @@ -29,7 +29,7 @@ vec2 vec2_add(vec2 lhs, vec2 rhs) { /// @param rhs The right operand. /// @return The difference in a new vector. vec2 vec2_sub(vec2 lhs, vec2 rhs) { - return (vec2){ .x = lhs.x - rhs.x , .y = lhs.y - rhs.y }; + return (vec2) {.x = lhs.x - rhs.x, .y = lhs.y - rhs.y}; } /// Multiply a vector by a scalar. @@ -37,7 +37,7 @@ vec2 vec2_sub(vec2 lhs, vec2 rhs) { /// @param rhs The right operand, a vector. /// @return The product in a new vector. vec2 vec2_mul(double scalar, vec2 rhs) { - return (vec2){ .x = scalar * rhs.x , .y = scalar * rhs.y }; + return (vec2) {.x = scalar * rhs.x, .y = scalar * rhs.y}; } /// Compute the dot product (scalar product) between two vectors. @@ -67,7 +67,7 @@ double vec2_norm(vec2 v) { /// @return The new normalized vector. vec2 vec2_normalize(vec2 v) { double norm = vec2_norm(v); - return (vec2){ .x = v.x / norm , .y = v.y / norm }; + return (vec2) {.x = v.x / norm, .y = v.y / norm}; } /// Check whether two vectors are approximately equals within a given tolerance. @@ -89,15 +89,25 @@ bool vec2_is_approx_equal(vec2 lhs, vec2 rhs, double eps) { /// @param height The screen height. /// @return The coordinates (rwo, column). coordinates vec2_to_coordinates(vec2 v, uint32_t width, uint32_t height) { - return (coordinates){ - .column = (uint32_t)(v.x * width) , - .row = (uint32_t)(v.y * height) + return (coordinates) { + .column = (uint32_t)(v.x * width), + .row = (uint32_t)(v.y * height) }; } /// Print a vector in the standard output. /// @param v The vector. -void vec2_print(vec2 v) -{ - printf("x = %g, y = %g\n", v.x, v.y); +void vec2_print(vec2 v) { + printf("x = %g, y = %g\n", v.x, v.y); +} + +/// Same as vec2_to_coordinates but (0,0) is at the center of the screen. +/// @param v The 2d vector: components between -1.0 and +1.0 +/// @param width The screen width. +/// @param height The screen height. +coordinates vec2_to_coordinates_centered(const vec2 v, const uint32_t width, const uint32_t height) { + return (coordinates) { + .column = (uint32_t)((v.x + 1.0) * width / 2), + .row = (uint32_t)((v.y + 1.0) * height / 2) + }; } diff --git a/vec2/vec2.h b/vec2/vec2.h index ed0403544e09df2d1a4af16e4f2a77f3d6b058de..96ce1b793dfa35269d1d9c13536c83a05c12897c 100755 --- a/vec2/vec2.h +++ b/vec2/vec2.h @@ -38,4 +38,6 @@ coordinates vec2_to_coordinates(vec2 v, uint32_t width, uint32_t height); void vec2_print(vec2 v); +coordinates vec2_to_coordinates_centered(const vec2 v, const uint32_t width, const uint32_t height); + #endif