Browse Source

First commit

Stephen D 9 months ago
commit
d6e94d231d
2 changed files with 103 additions and 0 deletions
  1. 35 0
      RigidMass.py
  2. 68 0
      main.py

+ 35 - 0
RigidMass.py

@ -0,0 +1,35 @@
1
import math
2
3
G = 6.67408E-11
4
5
class RigidMass:
6
	def __init__(self, mass, vx, vy, x, y, tick):
7
		self.mass = mass
8
		self.vx = vx
9
		self.vy = vy
10
		self.x = x
11
		self.y = y
12
		self.dvx, self.dvy = 0, 0
13
		self.tick = tick #tick length, in seconds
14
15
	def gravity(self, p): #p is other particle
16
		v = -G * p.mass / self.distance_squared(p) * self.tick
17
18
		#print(self.distance_squared(p))
19
		#self.v = v #debugging
20
21
		dx = self.x - p.x
22
		dy = self.y - p.y
23
		hyp = math.sqrt(dx**2 + dy**2)
24
		self.dvx += v * (dx) / hyp
25
		self.dvy += v * (dy) / hyp
26
27
	def process_tick(self):
28
		self.vx += self.dvx
29
		self.vy += self.dvy
30
		self.dvx, self.dvy = 0, 0
31
		self.x += self.vx * self.tick
32
		self.y += self.vy * self.tick
33
		
34
	def distance_squared(self, p):
35
		return abs((self.x - p.x)**2 + (self.y - p.y)**2)

+ 68 - 0
main.py

@ -0,0 +1,68 @@
1
from RigidMass import RigidMass
2
from PIL import Image
3
from PIL import ImageFont
4
from PIL import ImageDraw 
5
import random
6
import math
7
8
9
10
11
outImg = Image.new("RGB", [1000, 1000])
12
def storePixels(name, things):
13
	global outImg
14
	#outImg = Image.new("RGB", [1000, 1000])
15
	w, h = 1000, 1000
16
	for thing in things:
17
		if thing.x > 0 and thing.y > 0 and thing.x < 1000 and thing.y < 1000:
18
			outImg.putpixel((int(thing.x), int(thing.y)), 16581370*255)
19
	#outImg.save(name)
20
21
#sun = RigidMass(  500000000000000, 0, -5, 500, 500, 0.05)
22
#earth = RigidMass(500000000000000, 0, 5, 250, 500, 0.05)
23
24
tick_time = 0.05
25
26
num_stars = 1000
27
stars = []
28
for i in range(num_stars):
29
	#give each star some angular velocity
30
	#did some cool algebra on paper
31
	x = random.randint(375, 625)
32
	y = random.randint(375, 625)
33
	dx = x - 500
34
	dy = y - 500
35
36
	if abs(dy) <= abs(dx):
37
		vy = 3 * random.random()
38
		vx = -vy * dy / dx
39
	else:
40
		vx = 3 * random.random()
41
		vy  = -vx * dx / dy
42
43
	#this makes the circle rotate together
44
	#without it, one half rotates in the opposite direction
45
	#the two halves are separated by the y=x line
46
	#I have no idea why that happens, but this fixes it.
47
	if y < x:
48
		vx = -vx
49
		vy = -vy
50
	stars.append(RigidMass(50000000000, vx, vy, x, y, tick_time))
51
52
for i in range(0, 50000):
53
	for a in range(0, num_stars):
54
		for b in range(0, num_stars):
55
			if not a == b and abs(stars[a].x - stars[b].x) > 0.5 and abs(stars[a].y - stars[b].y) > 0.5:
56
				stars[a].gravity(stars[b])
57
58
	for a in range(0, num_stars):
59
		stars[a].process_tick()
60
		
61
	storePixels(str(i) + ".png", stars)
62
	if i % 10 == 0:
63
		draw = ImageDraw.Draw(outImg)
64
		draw.text((0, 0),"t=" + str(i * tick_time) + "s", (255,255,255))
65
		outImg.save("img/" + str(i // 10) + ".png")
66
		outImg = Image.new("RGB", [1000, 1000])
67
68
outImg.save("final.png")