-
Notifications
You must be signed in to change notification settings - Fork 68
/
dot-animate.py
110 lines (80 loc) · 2.2 KB
/
dot-animate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import math
import random
from math import cos, sin, sqrt
from random import randrange
import pygame
WIDTH = 800
HEIGHT = 800
CENTER = WIDTH // 2, HEIGHT // 2
centerX = WIDTH // 2
centerY = HEIGHT // 2
G = 0.2
M = 10e7
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
r0 = 10
pygame.init()
class Particle:
def __init__(self, x, y):
self.g = G
self.mass = 2
self.x = x
self.y = y
self.momentum_x = 500
self.momentum_y = 500
self.dt = 0.001
def move(self, x_y_central_mass):
x2 = x_y_central_mass[0]
y2 = x_y_central_mass[1]
hyp = (self.x - x2) ** 2 + (self.y - y2) ** 2
theta = math.atan2(y2 - self.y, x2 - self.x)
force = (self.g * self.mass * M) / hyp
force_x = force * math.cos(theta)
force_y = force * math.sin(theta)
self.momentum_x += force_x * self.dt
self.momentum_y += force_y * self.dt
self.x += self.momentum_x / self.mass * self.dt
self.y += self.momentum_y / self.mass * self.dt
return [self.x, self.y]
screen = pygame.display.set_mode((WIDTH, HEIGHT))
particles = []
r = 200
# Line
def generator():
for i in range(1000):
x = randrange(-500, 1000)
y = 100
p = Particle(x, y)
particles.append(p)
# Circle
# def generator():
# for i in range(1000):
# ang = random.uniform(0, 1) * 2 * math.pi
# hyp = sqrt(random.uniform(0, 1)) * r
# adj = cos(ang) * hyp
# opp = sin(ang) * hyp
# x = centerX + adj
# y = centerY + opp
# p = Particle(x, y)
# particles.append(p)
# Square
# def generator():
# for i in range(500):
# x = randrange(0, 500)
# y = randrange(0, 500)
# p = Particle(x, y)
# particles.append(p)
generator()
def draw():
for i in range(len(particles)):
pygame.draw.circle(screen, WHITE, (particles[i].move(CENTER)), 1)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(BLACK)
# Gravity point
# central_mass = pygame.draw.circle(screen, WHITE, CENTER, r0)
draw()
pygame.display.update()