Skip to content
Snippets Groups Projects
Commit dab469d6 authored by BobLeHibou's avatar BobLeHibou
Browse files

ADD: V1.0 of all planet functions and vec2_coordinates_centered

parent 51201730
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -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);
}
}
......
......@@ -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)
};
}
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment