diff --git a/charge/charge.c b/charge/charge.c index 34730d90f459abb17b1e241819ebd6440357cebb..79b5ed6823535731fb089890d450e30598be189d 100644 --- a/charge/charge.c +++ b/charge/charge.c @@ -32,8 +32,8 @@ bool pos_not_too_close(vec2 p, charge_t *c, int num_charges) { bool compute_e(charge_t c, vec2 p, double eps, vec2 *e) { vec2 qP = vec2_sub(p, c.pos); double norm = vec2_norm(qP); - double E = K * c.q / pow(norm, 2); // E = k * (Q / r^2) - *e = vec2_mul(vec2_div(qP, norm), E); // e = E * qP / ||qP|| + double E = K * (c.q * 10e-6) / pow(norm, 2); // E = k * (Q / r^2) + *e = vec2_mul(vec2_div(qP, norm), E); // e = E * qP / ||qP|| return (norm > eps); } diff --git a/charge/charge.h b/charge/charge.h index 8a3c229fe0a584b32f81b7e89b1382d6e3942ddf..e0d2b11f260be623f629a163020363de991a29e0 100644 --- a/charge/charge.h +++ b/charge/charge.h @@ -21,7 +21,7 @@ #define K 8.988e9 #define ELEMENTARY_CHARGE 1.602176634e-19 -#define CHARGE_RADIUS 10 +#define CHARGE_RADIUS 12 #define EPS 0.015 #define DELTA_T 10e-4 #define CHARGE_MASS 1500 diff --git a/draw/draw.c b/draw/draw.c index 5ac085d8de3ecbb72bb22b7d325f6283ea9bfce0..73defc04162967d19f982befb292a91e3b0e3835 100644 --- a/draw/draw.c +++ b/draw/draw.c @@ -27,7 +27,7 @@ void draw_circle(vec2 center, int rad) { // GLfloat radius = 0.8f; //radius GLfloat twicePi = 2.0f * 3.141592; - glBegin(GL_LINE_LOOP); + glBegin(GL_POLYGON); for (i = 0; i <= lineAmount; i++) { glVertex2f(center.x + (rad * cos(i * twicePi / lineAmount)), center.y + (rad * sin(i * twicePi / lineAmount))); @@ -52,20 +52,13 @@ void get_heatmap_color(double value, double *red, double *green, double *blue) { double aR = 1.0; double aG = 1.0; double aB = 1.0; // RGB for our 1st color - double bR = 0.67; - double bG = 0.12; + double bR = 0.0; + double bG = 0.0; double bB = 0.47; // RGB for our 2nd color - // if (value < 0.3) { - // *red = 1.0; - // *green = 1.0; - // *blue = 1.0; - // return; - // } - - *red = (bR - aR) * value + aR; // Evaluated as -255*value + 255. - *green = (bG - aG) * value + aG; // Evaluates as 0. - *blue = (bB - aB) * value + aB; // Evaluates as 255*value + 0. + *red = (bR - aR) * value + aR * 1.5; // Evaluated as -255*value + 255. + *green = (bG - aG) * value + aG * 1.5; // Evaluates as 0. + *blue = (bB - aB) * value + aB * 1.5; // Evaluates as 255*value + 0. } //===================== @@ -130,8 +123,9 @@ void draw_heatmap(charge_t *charges, int num_charges, int w, int h) { for (double y = 0; y < 1.0 - 1.0 / h; y += 1.0 / h) { for (double x = 0; x < 1.0 - 1.0 / w; x += 1.0 / w) { vec2 p = vec2_create(x, y); - compute_total_normalized_e(charges, num_charges, p, EPS, - &intensities[i]); + if (!compute_total_normalized_e(charges, num_charges, p, EPS, + &intensities[i])) + intensities[i] = vec2_create(0, 0); double in = vec2_norm(intensities[i]); if (in < min && in > 1) @@ -147,7 +141,7 @@ void draw_heatmap(charge_t *charges, int num_charges, int w, int h) { int x = i % w; int y = i / w; double r, g, b; - double a = (vec2_norm(intensities[i]) / max) * 20.0; + double a = (vec2_norm(intensities[i]) / max) * 40.0; get_heatmap_color(a, &r, &g, &b); glBegin(GL_POINTS); @@ -161,27 +155,25 @@ void draw_heatmap(charge_t *charges, int num_charges, int w, int h) { void draw_vector_field(charge_t *charges, int num_charges, vec2 *points, int nb_points, int w, int h) { - double min = DBL_MAX, max = 0.0; vec2 vs[nb_points]; for (int i = 0; i < nb_points; i++) { - compute_total_normalized_e(charges, num_charges, points[i], EPS, - &vs[i]); - - double in = vec2_norm(vs[i]); - if (in < min && in > 1) - min = in; - if (in > max) - max = in; + if (!compute_total_normalized_e(charges, num_charges, points[i], EPS, + &vs[i])) + vs[i] = vec2_create(0, 0); + + vs[i] = vec2_normalize(vs[i]); } for (int i = 0; i < nb_points; i++) { vec2 a = position_to_coordinates(w, h, points[i]); + glColor3f(0.18, 0.46, 0.18); + draw_circle(a, 3); + glLineWidth(2); glBegin(GL_LINES); - glColor3f(0.18, 0.46, 0.18); glVertex2d(a.x, a.y); - a = vec2_add(a, vec2_mul(vec2_div(vs[i], max), 40)); + a = vec2_add(a, vec2_mul(vs[i], 20)); glVertex2d(a.x, a.y); glEnd(); glLineWidth(1); diff --git a/main.c b/main.c index 1b37a23db44c29ec1066862dba5043c563fb26e4..4b32e03d596a4cd3c342a8f0c66487beb4b0f020 100644 --- a/main.c +++ b/main.c @@ -35,7 +35,7 @@ #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 800 #define NB_CHARGES 5 -#define GRID_SPACING 25 +#define GRID_SPACING 15 #define NB_POINTS \ ((WINDOW_WIDTH / GRID_SPACING) * (WINDOW_WIDTH / GRID_SPACING)) #define MAXIMUM_CHARGES 30 // Can be replace by a dynamic array @@ -47,14 +47,16 @@ vec2 default_charges_pos[MAXIMUM_CHARGES]; double default_charges[MAXIMUM_CHARGES]; charge_t *charges; -bool toggle_lines_view = true; -bool toggle_vector_view = false; +bool toggle_lines_view = false; +bool toggle_vector_view = true; bool toggle_heatmap_view = false; +bool animate = true; void update() { update_simulation(charges, charge_count); }; void update_timer() { - update(); + if (animate) + update(); glutTimerFunc(ONE_SECOND_IN_MILLISECONDS / REFRESH_RATE, update_timer, 0); } @@ -71,8 +73,8 @@ void draw_timer() { void generate_points(vec2 points[]) { int i = 0; - for (int y = 0; y < WINDOW_HEIGHT; y += GRID_SPACING) { - for (int x = 0; x < WINDOW_WIDTH; x += GRID_SPACING) { + for (int y = GRID_SPACING; y < WINDOW_HEIGHT; y += GRID_SPACING) { + for (int x = GRID_SPACING; x < WINDOW_WIDTH; x += GRID_SPACING) { points[i] = vec2_create(x / (double)WINDOW_WIDTH, y / (double)WINDOW_HEIGHT); i++; @@ -134,12 +136,12 @@ void handle_mouse_input(int button, int state, int x, int y) { if (button == GLUT_RIGHT_BUTTON) default_charges[charge_count - 1] = random_charge_q(false); - // Realloc charge array when a new one is added - charges = realloc(charges, sizeof(charge_t) * charge_count); - - // Redefine all charges infos - for (int i = 0; i < charge_count; i++) - charges[i] = charge_create(default_charges[i], default_charges_pos[i]); + charges[charge_count - 1] = + charge_create(default_charges[charge_count - 1], + default_charges_pos[charge_count - 1]); + charges[charge_count - 1].prev_pos = charges[charge_count - 1].pos; + charges[charge_count - 1].pos = + compute_first_pos(charges[charge_count - 1], charges, charge_count); glutPostRedisplay(); } @@ -162,6 +164,9 @@ void keyboard(unsigned char key, int UNUSED(x), int UNUSED(y)) { case 'h': toggle_heatmap_view = !toggle_heatmap_view; break; + case ' ': + animate = !animate; + break; } glutPostRedisplay(); @@ -177,7 +182,7 @@ void keyboard(unsigned char key, int UNUSED(x), int UNUSED(y)) { int main(int argc, char **argv) { srand(time(NULL)); - charges = malloc(sizeof(charge_t) * charge_count); + charges = malloc(sizeof(charge_t) * charge_count * 10); // Large list for further charges // Generate all default charges for (int i = 0; i < charge_count; i++) {