MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / Body

Class Body

physics/n_body_simulation.py:28–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26
27
28class Body:
29 def __init__(
30 self,
31 position_x: float,
32 position_y: float,
33 velocity_x: float,
34 velocity_y: float,
35 mass: float = 1.0,
36 size: float = 1.0,
37 color: str = "blue",
38 ) -> None:
39 """
40 The parameters "size" & "color" are not relevant for the simulation itself,
41 they are only used for plotting.
42 """
43 self.position_x = position_x
44 self.position_y = position_y
45 self.velocity_x = velocity_x
46 self.velocity_y = velocity_y
47 self.mass = mass
48 self.size = size
49 self.color = color
50
51 @property
52 def position(self) -> tuple[float, float]:
53 return self.position_x, self.position_y
54
55 @property
56 def velocity(self) -> tuple[float, float]:
57 return self.velocity_x, self.velocity_y
58
59 def update_velocity(
60 self, force_x: float, force_y: float, delta_time: float
61 ) -> None:
62 """
63 Euler algorithm for velocity
64
65 >>> body_1 = Body(0.,0.,0.,0.)
66 >>> body_1.update_velocity(1.,0.,1.)
67 >>> body_1.velocity
68 (1.0, 0.0)
69
70 >>> body_1.update_velocity(1.,0.,1.)
71 >>> body_1.velocity
72 (2.0, 0.0)
73
74 >>> body_2 = Body(0.,0.,5.,0.)
75 >>> body_2.update_velocity(0.,-10.,10.)
76 >>> body_2.velocity
77 (5.0, -100.0)
78
79 >>> body_2.update_velocity(0.,-10.,10.)
80 >>> body_2.velocity
81 (5.0, -200.0)
82 """
83 self.velocity_x += force_x * delta_time
84 self.velocity_y += force_y * delta_time
85

Callers 3

example_1Function · 0.85
example_2Function · 0.85
example_3Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected