diff --git a/CelestialObject.c b/CelestialObject.c new file mode 100644 index 0000000000000000000000000000000000000000..21d56a32b8105a20d13e9cec325c462db92428cf --- /dev/null +++ b/CelestialObject.c @@ -0,0 +1,16 @@ +#include "CelestialObject.h" + +#include <stdlib.h> + +#define G 6.67e-11 +#define SUN_MASS 1.989e30 + +CelestialObject *celestial_object_create(double mass, Vector2 *position) { + CelestialObject *object = (CelestialObject*)malloc(sizeof(CelestialObject)); + + object->mass = mass; + object->previous_position = NULL; + object->current_position = position; + + return object; +} diff --git a/CelestialObject.h b/CelestialObject.h new file mode 100644 index 0000000000000000000000000000000000000000..89b260b1bdc6d9ee2d4945a057de3bab6dc56947 --- /dev/null +++ b/CelestialObject.h @@ -0,0 +1,16 @@ +#ifndef CELESTIAL_OBJECT_H +#define CELESTIAL_OBJECT_H + +#include "Vectors/Vector2.h" +#include "gfx/gfx.h" + +typedef struct CelestialObject CelestialObject; +typedef struct CelestialObject { + double mass; + Vector2 *previous_position; + Vector2 *current_position; +} CelestialObject; + +CelestialObject *celestial_object_create(double mass, Vector2 *position); + +#endif diff --git a/CelestialObject/CelestialObject.c b/CelestialObject/CelestialObject.c deleted file mode 100644 index a26b074576d31a98294897944d9619c6a4e42932..0000000000000000000000000000000000000000 --- a/CelestialObject/CelestialObject.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "CelestialObject.h" - -#include <stdlib.h> - -#define G 6.67e-11 -#define M_SOLEIL 1.989e30 - -// TODO : magic diff --git a/CelestialObject/CelestialObject.h b/CelestialObject/CelestialObject.h deleted file mode 100644 index dd9c195bba6ac38972e5f26205086465ccc0a0cc..0000000000000000000000000000000000000000 --- a/CelestialObject/CelestialObject.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef CELESTIAL_OBJECT_HEADER -#define CELESTIAL_OBJECT_HEADER - -#include "../gfx/gfx.h" -#include "../Vectors/Vector2.h" - -// CelestialObject -typedef struct CelestialObject { - double mass; - Vector2 pos; // x(t) - Vector2 prec_pos; // x(t - interval) -} CelestialObject; - -typedef struct System { - CelestialObject star; // ex. The sun - // Créer une liste - // list<CelestialObject> planets; - uint32_t nb_planets; // The number of orbiting planets - CelestialObject *planets; // An array of orbiting planets -} system_t; - -// Those function are not mandatory to implement, -// it's rather a hint of what you should have. -CelestialObject create_planet(double mass, Vector2 pos); -system_t create_system(double interval); -void show_system(struct gfx_context_t *ctxt, system_t *system); -void update_system(system_t *system, double interval); -void free_system(system_t *system); - -#endif diff --git a/Makefile b/Makefile index 28106c853b5d670b25d9f80cbdb0dc5ba987f09f..d09695742e0bf2a37cf2962d9c478115dd5bfe1a 100644 --- a/Makefile +++ b/Makefile @@ -5,23 +5,16 @@ CFLAGS:=-g -Ofast -Wall -Wextra -fsanitize=address -fsanitize=leak -std=gnu11 #The flags passed to the linker LDFLAGS:=-lm -lSDL2 #Path to the lib Vec2 -VPATH:=Vectors gfx CelestialObject +VPATH:=Vectors gfx -main: main.o Vector2.o gfx.o CelestialObject.o - $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) - -run_tests: tests - ./$< - -tests: vec_tests.o vector2.o +main: main.o Vector2.o gfx.o CelestialObject.o SolarSystem.o $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) CelestialObject.o: CelestialObject.h - +SolarSystem.o: SolarSystem.h Vector2.o: Vector2.h - gfx.o: gfx.h clean: diff --git a/Makefile.old2 b/Makefile.old2 deleted file mode 100644 index a8b94fe5c6c0daa5ecc4697d73edb9cf5699d22e..0000000000000000000000000000000000000000 --- a/Makefile.old2 +++ /dev/null @@ -1,14 +0,0 @@ -#The compiler -CC:=gcc -#The flags passed to the compiler -CFLAGS:=-g -Ofast -Wall -Wextra -fsanitize=address -fsanitize=leak -std=gnu17 -VPATH:=Vectors - -main: main.o Vector2.o - $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) - -tests: vec_tests.o Vector2.o - $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) - -clean: - rm -f *.o main tests diff --git a/SolarSystem.c b/SolarSystem.c new file mode 100644 index 0000000000000000000000000000000000000000..82f269c5e5013d5d14d65cc22184e7c7519c629d --- /dev/null +++ b/SolarSystem.c @@ -0,0 +1,77 @@ +#include "SolarSystem.h" + +#include <stdlib.h> + +#include "CelestialObject.h" +#include "Vectors/Vector2.h" +#include "gfx/gfx.h" + +#define SCREEN_WIDTH 1000 +#define SCREEN_HEIGHT 1000 + +void __update(SolarSystem *solar_system) { +} + +void __draw(SolarSystem *solar_system, struct gfx_context_t *ctxt) { + for (int32_t i = 0; i < solar_system->celestial_objects_length; i += 1) { + CelestialObject *object = solar_system->celestial_objects[i]; + + Vector2 *tmp = object->current_position->multiply(object->current_position, 1.0 / (227.9 * 1E9 * 1.1)); + Vector2 *tmp2 = object->current_position->fit_canvas(tmp, SCREEN_WIDTH, SCREEN_HEIGHT); + + if (i == 0) { + draw_full_circle(ctxt, tmp2->x, tmp2->y, 70, COLOR_YELLOW); + } + + if (i == 1) { + draw_full_circle(ctxt, tmp2->x, tmp2->y, 15, COLOR_BLUE); + } + + if (i == 2) { + draw_full_circle(ctxt, tmp2->x, tmp2->y, 8, COLOR_RED); + } + + if (i == 3) { + draw_full_circle(ctxt, tmp2->x, tmp2->y, 15, COLOR_GREEN); + } + + free(tmp); + free(tmp2); + } +} + +SolarSystem *solar_system_create(double interval) { + SolarSystem *solar_system = (SolarSystem *)malloc(sizeof(SolarSystem)); + solar_system->update = __update; + solar_system->draw = __draw; + + solar_system->celestial_objects_length = 4; + solar_system->celestial_objects = (SolarSystem **)malloc(sizeof(SolarSystem *) * solar_system->celestial_objects_length); + + const double SUN_MASS = 1.989 * 1E30; + Vector2 *sun_position = vector2_create(0, 0); + solar_system->celestial_objects[0] = celestial_object_create(SUN_MASS, sun_position); + + const double EARTH_MASS = 5.972 * 1E24; + double e_1 = 0.01671123; + double semi_major_1 = 149.6 * 1E9; + double perihelion_1 = semi_major_1 * (1 - e_1); + Vector2 *earth_position = vector2_create(-perihelion_1, 0); + solar_system->celestial_objects[1] = celestial_object_create(EARTH_MASS, earth_position); + + const double MARS_MASS = 6.39 * 1E23; + double e_2 = 0.0934; + double semi_major_2 = 227.9 * 1E9; + double perihelion_2 = semi_major_2 * (1 - e_2); + Vector2 *mars_position = vector2_create(-perihelion_2, 0); + solar_system->celestial_objects[2] = celestial_object_create(MARS_MASS, mars_position); + + const double VENUS_MASS = 4.867 * 1E24; + double e_3 = 0.00678; + double semi_major_3 = 108.2095 * 1E9; + double perihelion_3 = semi_major_3 * (1 - e_3); + Vector2 *venus_position = vector2_create(-perihelion_3, 0); + solar_system->celestial_objects[3] = celestial_object_create(VENUS_MASS, venus_position); + + return solar_system; +} diff --git a/SolarSystem.h b/SolarSystem.h new file mode 100644 index 0000000000000000000000000000000000000000..44d2c5878961e4d5eeef998be91ad9b6ef71f1a7 --- /dev/null +++ b/SolarSystem.h @@ -0,0 +1,22 @@ +#ifndef SOLAR_SYSTEM_H +#define SOLAR_SYSTEM_H + +#include "CelestialObject.h" +#include "gfx/gfx.h" + +typedef struct SolarSystem SolarSystem; +struct SolarSystem { + // CelestialObject *star; + uint32_t celestial_objects_length; + CelestialObject **celestial_objects; + + void (*update)(SolarSystem *solar_system); + void (*draw)(SolarSystem *solar_system, struct gfx_context_t *ctxt); +}; + +SolarSystem *solar_system_create(double interval); +// void show_system(struct gfx_context_t *ctxt, system_t *system); +// void update_system(system_t *system, double interval); +// void free_system(system_t *system); + +#endif diff --git a/Vectors/Vector2.c b/Vectors/Vector2.c index f43d18069fa56e451adfc6efa2d0eaf02ea9c38a..981c9d518bcd5de980e2b89810078ba9f23dc40f 100644 --- a/Vectors/Vector2.c +++ b/Vectors/Vector2.c @@ -41,9 +41,11 @@ bool __is_similiar(Vector2 *a, Vector2 *b, double epsilon) { return norm < epsilon; } -// coordinates *__get_coordinates(Vector2 *v, uint32_t width, uint32_t height) { -// return NULL; -// } +Vector2 *__fit_canvas(Vector2 *v, uint32_t width, uint32_t height) { + double x = round((width - 1) / 2.0 + v->x * (width - 1) / 2.0); + double y = round((height - 1) / 2.0 + v->y * (height - 1) / 2.0); + return vector2_create(x, y); +} void __print(Vector2 *v) { printf("(%lf, %lf)\n", v->x, v->y); @@ -59,6 +61,7 @@ Vector2 *vector2_create(double x, double y) { v->norm_sqr = __norm_sqr; v->normalize = __normalize; v->is_similiar = __is_similiar; + v->fit_canvas = __fit_canvas; v->print = __print; v->x = x; diff --git a/Vectors/Vector2.h b/Vectors/Vector2.h index e6cd40347ea3ec81bf66d47ca9cd0488ec7b60e7..c8aaf1b643fbd633b68f28f50511c62cf634249f 100644 --- a/Vectors/Vector2.h +++ b/Vectors/Vector2.h @@ -17,6 +17,7 @@ struct Vector2 { double (*norm)(Vector2 *v); Vector2 *(*normalize)(Vector2 *v); bool (*is_similiar)(Vector2 *a, Vector2 *b, double epsilon); + Vector2 *(*fit_canvas)(Vector2 *v, uint32_t width, uint32_t height); void (*print)(Vector2 *v); }; diff --git a/main.c b/main.c index 308aaf6b07492fc2844181a2565b5af378a2736e..a7cd861cd9cad739011c90dcca1ce1807c5342c2 100644 --- a/main.c +++ b/main.c @@ -1,43 +1,44 @@ +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <time.h> -#include "gfx/gfx.h" -// #include "planet/planet.h" +#include "CelestialObject.h" +#include "SolarSystem.h" #include "Vectors/Vector2.h" +#include "gfx/gfx.h" #define SCREEN_WIDTH 1000 #define SCREEN_HEIGHT 1000 +const double SUN_MASS = 1.989 * 1E30; +const double EARTH_MASS = 5.972 * 1E24; + int main() { // srand(time(NULL)); srand(0); + printf("%lf\n", SUN_MASS); + + struct gfx_context_t *ctxt = gfx_create("Solar 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; + } - // if (!ctxt) { - // fprintf(stderr, "Graphics initialization failed!\n"); - // return EXIT_FAILURE; - // } + SolarSystem *solar_system = solar_system_create(0); - // // TODO : create your system + while (true) { + gfx_present(ctxt); + gfx_clear(ctxt, COLOR_BLACK); - // while (true) - // { - // gfx_present(ctxt); - // // TODO : draw the current state of your system - // // TODO : update your system - - // gfx_clear(ctxt, COLOR_BLACK); - // draw_full_circle(ctxt, 100, 100, 100, COLOR_YELLOW); + solar_system->draw(solar_system, ctxt); - // if (gfx_keypressed() == SDLK_ESCAPE) - // { - // break; - // } - // } + if (gfx_keypressed() == SDLK_ESCAPE) { + break; + } + } - // // TODO : Free your system gfx_destroy(ctxt); return EXIT_SUCCESS; } diff --git a/tests b/tests deleted file mode 100644 index d58e5be4cc0ebcf7ae5e0265d630a81075c9cc6a..0000000000000000000000000000000000000000 Binary files a/tests and /dev/null differ