]> Skullheadx's Git Forge - Electric-Dipoles.git/commitdiff
field lines updated
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 24 Nov 2023 15:09:00 +0000 (10:09 -0500)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 24 Nov 2023 15:09:00 +0000 (10:09 -0500)
main.py
particle.py

diff --git a/main.py b/main.py
index fc0d85b4ab9dd2d49ad6df6fcb8a8cbbef72d3af..2e1fd6e9e473f75aab5b2bd25cd3f84f61238aec 100644 (file)
--- 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)
 
index f1c6f2469da4ef0674baa7caab317f901977f4ba..37ad2e5cace9f331c4a71a91f413ae9deede9a4d 100644 (file)
@@ -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))