Skip to content
Snippets Groups Projects
Commit 773499df authored by florian.burgener's avatar florian.burgener
Browse files

Draw half field line

parent a3a7dc05
No related branches found
No related tags found
No related merge requests found
......@@ -14,15 +14,13 @@ const double THRESHOLD_CHARGES_DISTANCES = 0.5;
const double EPSILON = 0.05;
double compute_delta_x(int width, int height) {
printf("aaa %lf\n", 1.0 / sqrt((width * width) + (height * height)));
return 1.0 / sqrt((width * width) + (height * height));
}
bool compute_next_point(charge_t *charges, int num_charges, vector2_t current_pos, double eps, vector2_t *new_pos, double dx) {
vector2_t electric_field;
if (!compute_total_normalized_e(charges, num_charges, current_pos, eps, &electric_field)) {
if (compute_total_normalized_e(charges, num_charges, current_pos, eps, &electric_field)) {
*new_pos = vector2_add(current_pos, vector2_multiply(electric_field, dx));
printf("adf %lf\n", dx);
return true;
}
......@@ -49,10 +47,12 @@ bool compute_e(charge_t c, vector2_t p, double eps, vector2_t *e) {
*e = vector2_multiply(*e, -1);
}
return norm_r < eps;
return norm_r >= eps;
}
bool compute_total_normalized_e(charge_t *charges, int num_charges, vector2_t p, double eps, vector2_t *e) {
*e = vector2_create_zero();
for (int i = 0; i < num_charges; i += 1) {
vector2_t tmp;
if (!compute_e(charges[i], p, eps, &tmp)) {
......@@ -72,24 +72,18 @@ void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int num_char
while (true) {
vector2_t new_pos;
if (!compute_next_point(charges, num_charges, current_pos, EPSILON, &new_pos, dx)) {
if (!compute_next_point(charges, num_charges, current_pos, 4.5e-2, &new_pos, dx)) {
break;
}
vector2_print(current_pos);
vector2_print(new_pos);
if (is_out_of_bounds(new_pos, x0, x1, y0, y1)) {
break;
}
coordinates_t current_coordinates = position_to_coordinates(SCREEN_WIDTH, SCREEN_HEIGHT, x0, x1, y0, y1, current_pos);
printf("%d %d\n", current_coordinates.row, current_coordinates.column);
coordinates_t new_coordinates = position_to_coordinates(SCREEN_WIDTH, SCREEN_HEIGHT, x0, x1, y0, y1, new_pos);
printf("%d %d\n", new_coordinates.row, new_coordinates.column);
gfx_draw_line(ctxt, current_coordinates, new_coordinates, COLOR_BLUE);
current_pos = new_pos;
break;
}
}
......
......@@ -24,15 +24,14 @@ int main(int argc, char *argv[]) {
int num_charges = 2;
charge_t *charges = (charge_t *)malloc(sizeof(charge_t) * num_charges);
charges[0] = charge_create(-ELEMENTARY_CHARGE, vector2_create(.5, .5));
charges[0] = charge_create(-ELEMENTARY_CHARGE, vector2_create(.25, .5));
charges[1] = charge_create(ELEMENTARY_CHARGE, vector2_create(.75, .5));
double dx = compute_delta_x(SCREEN_WIDTH, SCREEN_HEIGHT);
gfx_clear(canvas, COLOR_BLACK);
draw_charges(canvas, charges, num_charges, x0, x1, y0, y1);
printf("adadf a %lf\n", dx);
draw_field_line(canvas, charges, num_charges, dx, vector2_create(.5, .2), x0, x1, y0, y1);
draw_field_line(canvas, charges, num_charges, dx, vector2_create(.5, .25), x0, x1, y0, y1);
gfx_present(canvas);
while (true) {
......
......@@ -5,8 +5,8 @@
#include "vector2.h"
const int SCREEN_WIDTH = 1000;
const int SCREEN_HEIGHT = 1000;
const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
coordinates_t coordinates_create(int row_, int column_) {
coordinates_t c = {.row = row_, .column = column_};
......
......@@ -49,5 +49,5 @@ vector2_t vector2_fit_canvas(vector2_t v, int32_t width, int32_t height) {
}
void vector2_print(vector2_t v) {
printf("(%lf, %lf)\n", v.x, v.y);
printf("(%.40lf, %.40lf)\n", v.x, v.y);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment