diff --git a/README.md b/README.md index 2424337bb0583ee5640e0e4af6c109872fd63903..bcde7754f08e21747f36071fdc40104f47707057 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ -# +# Repository du travail pratique de phyique : simulation de ligne de champ + +Auteur: Gawen Ackermann et Florian Burgener + ## Installation Pour compiler et exécuter le projet, exécuter dans l'ordre les commandes suivantes : @@ -11,5 +14,7 @@ make ### Sur Windows avec MinGW +``` mingw32-make.exe pacman -S mingw-w64-x86_64-SDL2 +``` diff --git a/src/Graphics.c b/src/Graphics.c index 87d6c99a147080d603666740c33c8534df492e17..f65d459afcf73b6fc0982530225950dec4b348c7 100644 --- a/src/Graphics.c +++ b/src/Graphics.c @@ -10,6 +10,7 @@ #include <stdint.h> #include <stdlib.h> +#include "Point.h" #include "utils.h" /// Create a fullscreen graphic window. @@ -105,6 +106,45 @@ SDL_Keycode gfx_keypressed() { // -------------- + +// void gfx_draw_line(Graphics *graphics, Point p0, Point p1, uint32_t color) { +// int dx = abs(p1.x - p0.x); +// int sx = p0.x < p1.x ? 1 : -1; + +// int dy = -abs(p1.y - p0.y); +// int sy = p0.y < p1.y ? 1 : -1; + +// int error = dx + dy; + +// while (true) { +// gfx_putpixel(graphics, p0.x, p0.y, color); + +// if (p0.x == p1.x && p0.y == p1.y) { +// break; +// } + +// int e2 = 2 * error; + +// if (e2 >= dy) { +// if (p0.x == p1.x) { +// break; +// } + +// error += dy; +// p0.x += sx; +// } + +// if (e2 <= dx) { +// if (p0.y == p1.y) { +// break; +// } + +// error += dx; +// p0.y += sy; +// } +// } +// } + void gfx_draw_line(Graphics *graphics, coordinates_t p0, coordinates_t p1, uint32_t color) { int dx = abs(p1.column - p0.column); int sx = p0.column < p1.column ? 1 : -1; @@ -143,19 +183,19 @@ void gfx_draw_line(Graphics *graphics, coordinates_t p0, coordinates_t p1, uint3 } } -void gfx_draw_circle(Graphics *graphics, coordinates_t c, uint32_t r, uint32_t color) { +void gfx_draw_circle(Graphics *graphics, Point c, uint32_t r, uint32_t color) { int x = 0, y = r, d = r - 1; while (y >= x) { - gfx_putpixel(graphics, c.column + x, c.row + y, color); - gfx_putpixel(graphics, c.column + y, c.row + x, color); - gfx_putpixel(graphics, c.column - x, c.row + y, color); - gfx_putpixel(graphics, c.column - y, c.row + x, color); - - gfx_putpixel(graphics, c.column + x, c.row - y, color); - gfx_putpixel(graphics, c.column + y, c.row - x, color); - gfx_putpixel(graphics, c.column - x, c.row - y, color); - gfx_putpixel(graphics, c.column - y, c.row - x, color); + gfx_putpixel(graphics, c.x + x, c.y + y, color); + gfx_putpixel(graphics, c.x + y, c.y + x, color); + gfx_putpixel(graphics, c.x - x, c.y + y, color); + gfx_putpixel(graphics, c.x - y, c.y + x, color); + + gfx_putpixel(graphics, c.x + x, c.y - y, color); + gfx_putpixel(graphics, c.x + y, c.y - x, color); + gfx_putpixel(graphics, c.x - x, c.y - y, color); + gfx_putpixel(graphics, c.x - y, c.y - x, color); if ((2 * x) <= d) { d -= 2 * x + 1; diff --git a/src/Graphics.h b/src/Graphics.h index d0cd90fcdeb6652e977040e710f0475e1b84396e..0e79513f48484aa584d876cb869b919bb67be724 100644 --- a/src/Graphics.h +++ b/src/Graphics.h @@ -5,6 +5,7 @@ #include <stdbool.h> #include <stdint.h> +#include "Point.h" #include "utils.h" #define MAKE_COLOR(r, g, b) \ @@ -39,7 +40,7 @@ extern void gfx_destroy(struct gfx_context_t *ctxt); extern void gfx_present(struct gfx_context_t *ctxt); extern SDL_Keycode gfx_keypressed(); -void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color); -void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, uint32_t r, uint32_t color); +void gfx_draw_line(Graphics *graphics, coordinates_t p0, coordinates_t p1, uint32_t color); +void gfx_draw_circle(Graphics *graphics, Point c, uint32_t r, uint32_t color); #endif diff --git a/src/Makefile b/src/Makefile index 1a5b1b8f659be16e57a33190a3e6184e26765527..312961bd91804e14951ef144eed01ea381c6bba5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,12 +14,18 @@ default: $(TARGET) Charge.o: Charge.c Charge.h $(CC) ${CFLAGS} -c $< +constants.o: constants.c constants.h + $(CC) ${CFLAGS} -c $< + Graphics.o: Graphics.c Graphics.h $(CC) ${CFLAGS} -c $< main.o: main.c $(CC) ${CFLAGS} -c $< +Point.o: Point.c Point.h + $(CC) ${CFLAGS} -c $< + random_number.o: random_number.c random_number.h $(CC) ${CFLAGS} -c $< @@ -35,7 +41,7 @@ utils.o: utils.c utils.h Vector2.o: Vector2.c Vector2.h $(CC) ${CFLAGS} -c $< -$(TARGET): Charge.o main.o utils.o Vector2.o Graphics.o Rectangle.o Simulation.o random_number.o +$(TARGET): Charge.o constants.o Graphics.o main.o Point.o random_number.o Rectangle.o Simulation.o utils.o Vector2.o $(CC) -Wall -o $@ $^ $(LIBS) clean: diff --git a/src/Point.c b/src/Point.c new file mode 100644 index 0000000000000000000000000000000000000000..01760731af34c6c68ffb1d4473ab8c6cec5dbc8e --- /dev/null +++ b/src/Point.c @@ -0,0 +1,5 @@ +#include "Point.h" + +Point point_init(int x, int y) { + return (Point){.x = x, .y = y}; +} diff --git a/src/Point.h b/src/Point.h new file mode 100644 index 0000000000000000000000000000000000000000..abe7b02b7231271425eec4584ed2b4a28fa97c32 --- /dev/null +++ b/src/Point.h @@ -0,0 +1,11 @@ +#ifndef POINT_H +#define POINT_H + +typedef struct Point { + int x; + int y; +} Point; + +Point point_init(int x, int y); + +#endif diff --git a/src/Simulation.c b/src/Simulation.c index b6f868fd7121b76b1847e17b3cbd87658fd7d695..b75f1a2ca8f48ec9e079799a3a603cf96baff3fc 100644 --- a/src/Simulation.c +++ b/src/Simulation.c @@ -3,17 +3,12 @@ #include <stdlib.h> #include "Charge.h" -#include "Vector2.h" #include "Graphics.h" +#include "Vector2.h" +#include "constants.h" #include "random_number.h" #include "utils.h" -static const int MIN_CHARGES = 2; -static const int MAX_CHARGES = 5; -// const int CHARGE_CIRCLE_RADIUS = 20; -const double K = 8.988e9; -const double ELEMENTARY_CHARGE = 1.602e-19; - static Charge generate_random_charge() { Vector2 position = vector2_init(random_number_between_0_and_1(), random_number_between_0_and_1()); int sign = rand() % 2 == 0 ? 1 : -1; @@ -90,7 +85,7 @@ static void draw_field_line_with_direction(Graphics *graphics, Rectangle *univer while (true) { Vector2 next_point; - if (!compute_next_point(universe, charges_length, charges, current_point, 0.027, dx, direction, &next_point)) { + if (!compute_next_point(universe, charges_length, charges, current_point, (double)CHARGE_CIRCLE_RADIUS / SCREEN_WIDTH, dx, direction, &next_point)) { break; } diff --git a/src/charge.c b/src/charge.c index 20f9e24e28ef49e44c5dbc08e6cf7173689d3580..1ec39cd807e2646c5d97061b5610e9666ac270a3 100644 --- a/src/charge.c +++ b/src/charge.c @@ -1,11 +1,11 @@ #include "Charge.h" +#include "Graphics.h" #include "Rectangle.h" #include "Vector2.h" -#include "Graphics.h" +#include "constants.h" #include "utils.h" - -const int CHARGE_CIRCLE_RADIUS = 20; +#include "Point.h" Charge charge_init(double q, Vector2 position) { return (Charge){.q = q, .position = position}; @@ -15,7 +15,7 @@ void charge_draw(Charge charge, Graphics *graphics, Rectangle *universe) { coordinates_t c = position_to_coordinates(SCREEN_WIDTH, SCREEN_HEIGHT, universe, charge.position); int radius = CHARGE_CIRCLE_RADIUS; - gfx_draw_circle(graphics, c, radius, COLOR_WHITE); + gfx_draw_circle(graphics, point_init(c.column, c.row), radius, COLOR_WHITE); int color = charge.q > 0 ? COLOR_RED : COLOR_BLUE; int half_length = (int)(radius * .6); diff --git a/src/charge.h b/src/charge.h index 418ae7be11d2baba13edba0536bf108db094e795..b3b765fce873370d3341e34ac7d4c584fd9e279f 100644 --- a/src/charge.h +++ b/src/charge.h @@ -1,11 +1,9 @@ #ifndef CHARGE_H #define CHARGE_H -#include "Vector2.h" #include "Graphics.h" #include "Rectangle.h" - -extern const int CHARGE_CIRCLE_RADIUS; +#include "Vector2.h" typedef struct Charge { double q; diff --git a/src/constants.c b/src/constants.c new file mode 100644 index 0000000000000000000000000000000000000000..328cccaca13a202de96ff8c79b05591a336e19a4 --- /dev/null +++ b/src/constants.c @@ -0,0 +1,17 @@ +#include "constants.h" + +const int SCREEN_WIDTH = 750; +const int SCREEN_HEIGHT = 750; + +const int UNIVERSE_X0 = 0; +const int UNIVERSE_Y0 = 0; +const int UNIVERSE_X1 = 1; +const int UNIVERSE_Y1 = 1; + +const int CHARGE_CIRCLE_RADIUS = 20; + +const int MIN_CHARGES = 2; +const int MAX_CHARGES = 5; + +const double K = 8.988e9; +const double ELEMENTARY_CHARGE = 1.602e-19; diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 0000000000000000000000000000000000000000..4f8d25e94932b6ddd232a4431d48f9bbaf5e46ec --- /dev/null +++ b/src/constants.h @@ -0,0 +1,20 @@ +#ifndef CONSTANTS_H +#define CONSTANTS_H + +extern const int SCREEN_WIDTH; +extern const int SCREEN_HEIGHT; + +extern const int UNIVERSE_X0; +extern const int UNIVERSE_Y0; +extern const int UNIVERSE_X1; +extern const int UNIVERSE_Y1; + +extern const int CHARGE_CIRCLE_RADIUS; + +extern const int MIN_CHARGES; +extern const int MAX_CHARGES; + +extern const double K; +extern const double ELEMENTARY_CHARGE; + +#endif diff --git a/src/main.c b/src/main.c index 8a96d3a8e737f9a24b25b25e6ee32bebd4aca658..f4e38d9aa2a8073249a49b8dbcea83708f42c751 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include "Graphics.h" #include "Rectangle.h" #include "Simulation.h" +#include "constants.h" #include "utils.h" // https://stackoverflow.com/questions/3417837/what-is-the-best-way-to-suppress-a-unused-variable-x-warning @@ -17,11 +18,6 @@ #define UNUSED(x) x #endif -static const int UNIVERSE_X0 = 0; -static const int UNIVERSE_Y0 = 0; -static const int UNIVERSE_X1 = 1; -static const int UNIVERSE_Y1 = 1; - double compute_delta_x(int width, int height) { return 1 / sqrt((width * width) + (height * height)); } diff --git a/src/utils.c b/src/utils.c index a7ec0d1eab0563585d716cbf1dc4818f027b78a6..f5e5f59751b38f1548ee300b4e47065d69ac881d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,9 +6,6 @@ #include "Rectangle.h" #include "Vector2.h" -const int SCREEN_WIDTH = 750; -const int SCREEN_HEIGHT = 750; - coordinates_t coordinates_create(int row_, int column_) { coordinates_t c = {.row = row_, .column = column_}; return c; diff --git a/src/utils.h b/src/utils.h index 0e5a5815e0b3c22933f433ee4b4b7e65ebd5106a..48d4b1340a09f5d6f66ca56c4f273377ae338ff0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -6,9 +6,6 @@ #include "Rectangle.h" #include "Vector2.h" -extern const int SCREEN_WIDTH; -extern const int SCREEN_HEIGHT; - typedef struct _coordinates_t { int32_t row; int32_t column;