From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Fri, 24 Nov 2023 15:09:00 +0000 (-0500) Subject: field lines updated X-Git-Url: http://git.skullheadx.com/nixos/static/gitweb.js?a=commitdiff_plain;h=bb9d878d428cc566159186005e076071cdac23c5;p=Electric-Dipoles.git field lines updated --- diff --git a/main.py b/main.py index fc0d85b..2e1fd6e 100644 --- a/main.py +++ b/main.py @@ -24,6 +24,8 @@ while is_running: particle.update(delta, particles) screen.fill((255, 255, 255)) + for particle in particles: + particle.draw_field_lines(screen) for particle in particles: particle.draw(screen) diff --git a/particle.py b/particle.py index f1c6f24..37ad2e5 100644 --- a/particle.py +++ b/particle.py @@ -10,23 +10,27 @@ class PointParticle: BLUE = (0, 153, 255) RED = (255, 43, 0) + radius = 25 step_amount = 10 + default_num_field_lines = 16 def __init__(self, position, charge=1.6e-19): self.position = pygame.Vector2(position) self.mouse_offset = pygame.Vector2(0, 0) self.charge = charge - self.radius = 25 self.dragging = False self.colour = self.RED if self.charge < 0 else self.BLUE self.sign = self.signs['−' if self.charge < 0 else '+'] - self.num_field_lines = int(abs(self.charge) / 1.6e-19 * 8) # default will be 8 lines - self.field_line_angles = list(range(0, 360, int(360 / self.num_field_lines))) # List of angles + self.num_field_lines = int(abs(self.charge) / 1.6e-19 * self.default_num_field_lines) + self.field_line_angles = list(range(0, 360, math.ceil(360 / self.num_field_lines))) # List of angles self.field_lines = [[] for _ in range(self.num_field_lines)] self.line_step = pygame.Vector2(self.step_amount, 0) if self.charge > 0 else pygame.Vector2(-self.step_amount, 0) + # Only if the field lines do not connect to the particle, then they are stored here + self.extra_field_lines = [[] for _ in range(self.num_field_lines)] + def update(self, delta, particles): if self.dragging or not (True in [p.dragging for p in particles]): if pygame.mouse.get_pressed(3)[0]: @@ -38,14 +42,13 @@ class PointParticle: elif self.dragging: self.dragging = False - if self.charge < 0: # Ensuring that only positive field lines are drawn - return for field_line_index, angle in enumerate(self.field_line_angles): self.field_lines[field_line_index].clear() + self.extra_field_lines[field_line_index].clear() position = self.position.copy() - direction = angle + direction = math.radians(angle) end = False - for i in range(500): + for i in range(1000): position += self.line_step.copy().rotate(math.degrees(direction)) electric_field_x_net = 0 @@ -64,14 +67,23 @@ class PointParticle: direction = math.atan2(electric_field_y_net, electric_field_x_net) self.field_lines[field_line_index].append(position.copy()) + if math.hypot(electric_field_x_net, electric_field_y_net) > 1.5e-11 or (i==1000-1 and self.charge < 0): + self.extra_field_lines[field_line_index] = self.field_lines[field_line_index].copy() + end = True + break if end: break - + def draw_field_lines(self, surf): + if self.charge > 0: + for line in self.field_lines: + if len(line) > 1: + pygame.draw.lines(surf, self.colour, False, line, 5) + else: + for line in self.extra_field_lines: + if len(line) > 1: + pygame.draw.lines(surf, self.colour, False, line, 5) def draw(self, surf): - for line in self.field_lines: - if len(line) > 1: - pygame.draw.lines(surf, self.colour, False, line, 5) pygame.draw.circle(surf, self.colour, self.position, self.radius) pygame.draw.circle(surf, (0, 0, 0), self.position, self.radius, 3) surf.blit(self.sign, self.sign.get_rect(center=self.position))