From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 27 Nov 2023 02:00:39 +0000 (-0500) Subject: added setup file & optimized particle field line X-Git-Url: http://git.skullheadx.com/nixos/static/gitweb.js?a=commitdiff_plain;h=c421304e6f74f1a0dce648ff484743aa0759e431;p=Electric-Dipoles.git added setup file & optimized particle field line --- diff --git a/main.py b/main.py index 0c468e5..ac9a27c 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,6 @@ -import pygame - -pygame.init() - +from setup import * from particle import PointParticle -SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600 -screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) -pygame.display.set_caption("Electric Dipoles") - is_running = True clock = pygame.Clock() delta = 0 diff --git a/particle.py b/particle.py index 37ad2e5..afad6b5 100644 --- a/particle.py +++ b/particle.py @@ -1,5 +1,4 @@ -import pygame -import math +from setup import * class PointParticle: @@ -11,8 +10,9 @@ class PointParticle: RED = (255, 43, 0) radius = 25 - step_amount = 10 - default_num_field_lines = 16 + step_amount = 1 + default_num_field_lines = 8 + screen_rect = pygame.Rect(0,0,SCREEN_WIDTH, SCREEN_HEIGHT) def __init__(self, position, charge=1.6e-19): self.position = pygame.Vector2(position) @@ -28,8 +28,6 @@ class PointParticle: 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]): @@ -44,11 +42,10 @@ class PointParticle: 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 = math.radians(angle) end = False - for i in range(1000): + for i in range(2000): position += self.line_step.copy().rotate(math.degrees(direction)) electric_field_x_net = 0 @@ -67,22 +64,15 @@ 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() + if not self.screen_rect.collidepoint(position): 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) + for line in self.field_lines: + if len(line) > 1: + pygame.draw.lines(surf, self.colour, False, line, 5) def draw(self, surf): pygame.draw.circle(surf, self.colour, self.position, self.radius) pygame.draw.circle(surf, (0, 0, 0), self.position, self.radius, 3) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..66336fa --- /dev/null +++ b/setup.py @@ -0,0 +1,10 @@ +import pygame +import math + + +pygame.init() + + +SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600 +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) +pygame.display.set_caption("Electric Dipoles") \ No newline at end of file