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

Function example_3

physics/n_body_simulation.py:303–334  ·  view source on GitHub ↗

Example 3: Random system with many bodies. No doctest provided since this function does not have a return value.

()

Source from the content-addressed store, hash-verified

301
302
303def example_3() -> BodySystem:
304 """
305 Example 3: Random system with many bodies.
306 No doctest provided since this function does not have a return value.
307 """
308
309 bodies = []
310 for _ in range(10):
311 velocity_x = random.uniform(-0.5, 0.5)
312 velocity_y = random.uniform(-0.5, 0.5)
313
314 # Bodies are created pairwise with opposite velocities so that the
315 # total impulse remains zero
316 bodies.append(
317 Body(
318 random.uniform(-0.5, 0.5),
319 random.uniform(-0.5, 0.5),
320 velocity_x,
321 velocity_y,
322 size=0.05,
323 )
324 )
325 bodies.append(
326 Body(
327 random.uniform(-0.5, 0.5),
328 random.uniform(-0.5, 0.5),
329 -velocity_x,
330 -velocity_y,
331 size=0.05,
332 )
333 )
334 return BodySystem(bodies, 0.01, 10, 0.1)
335
336
337if __name__ == "__main__":

Callers 1

Calls 3

BodyClass · 0.85
BodySystemClass · 0.85
appendMethod · 0.45

Tested by

no test coverage detected