Skip to content
Snippets Groups Projects
Commit 406f5005 authored by Florian Burgener's avatar Florian Burgener
Browse files

Refactoring 3

parent 25909e12
Branches
No related tags found
No related merge requests found
#
# 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
```
......@@ -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;
......
......@@ -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
......@@ -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:
......
#include "Point.h"
Point point_init(int x, int y) {
return (Point){.x = x, .y = y};
}
#ifndef POINT_H
#define POINT_H
typedef struct Point {
int x;
int y;
} Point;
Point point_init(int x, int y);
#endif
......@@ -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;
}
......
#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);
......
#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;
......
#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;
#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
......@@ -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));
}
......
......@@ -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;
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment