Skip to content
Snippets Groups Projects
Commit d6e94d23 authored by Stephen D's avatar Stephen D
Browse files

First commit

parents
Branches master
No related tags found
No related merge requests found
import math
G = 6.67408E-11
class RigidMass:
def __init__(self, mass, vx, vy, x, y, tick):
self.mass = mass
self.vx = vx
self.vy = vy
self.x = x
self.y = y
self.dvx, self.dvy = 0, 0
self.tick = tick #tick length, in seconds
def gravity(self, p): #p is other particle
v = -G * p.mass / self.distance_squared(p) * self.tick
#print(self.distance_squared(p))
#self.v = v #debugging
dx = self.x - p.x
dy = self.y - p.y
hyp = math.sqrt(dx**2 + dy**2)
self.dvx += v * (dx) / hyp
self.dvy += v * (dy) / hyp
def process_tick(self):
self.vx += self.dvx
self.vy += self.dvy
self.dvx, self.dvy = 0, 0
self.x += self.vx * self.tick
self.y += self.vy * self.tick
def distance_squared(self, p):
return abs((self.x - p.x)**2 + (self.y - p.y)**2)
main.py 0 → 100644
from RigidMass import RigidMass
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import random
import math
outImg = Image.new("RGB", [1000, 1000])
def storePixels(name, things):
global outImg
#outImg = Image.new("RGB", [1000, 1000])
w, h = 1000, 1000
for thing in things:
if thing.x > 0 and thing.y > 0 and thing.x < 1000 and thing.y < 1000:
outImg.putpixel((int(thing.x), int(thing.y)), 16581370*255)
#outImg.save(name)
#sun = RigidMass( 500000000000000, 0, -5, 500, 500, 0.05)
#earth = RigidMass(500000000000000, 0, 5, 250, 500, 0.05)
tick_time = 0.05
num_stars = 1000
stars = []
for i in range(num_stars):
#give each star some angular velocity
#did some cool algebra on paper
x = random.randint(375, 625)
y = random.randint(375, 625)
dx = x - 500
dy = y - 500
if abs(dy) <= abs(dx):
vy = 3 * random.random()
vx = -vy * dy / dx
else:
vx = 3 * random.random()
vy = -vx * dx / dy
#this makes the circle rotate together
#without it, one half rotates in the opposite direction
#the two halves are separated by the y=x line
#I have no idea why that happens, but this fixes it.
if y < x:
vx = -vx
vy = -vy
stars.append(RigidMass(50000000000, vx, vy, x, y, tick_time))
for i in range(0, 50000):
for a in range(0, num_stars):
for b in range(0, num_stars):
if not a == b and abs(stars[a].x - stars[b].x) > 0.5 and abs(stars[a].y - stars[b].y) > 0.5:
stars[a].gravity(stars[b])
for a in range(0, num_stars):
stars[a].process_tick()
storePixels(str(i) + ".png", stars)
if i % 10 == 0:
draw = ImageDraw.Draw(outImg)
draw.text((0, 0),"t=" + str(i * tick_time) + "s", (255,255,255))
outImg.save("img/" + str(i // 10) + ".png")
outImg = Image.new("RGB", [1000, 1000])
outImg.save("final.png")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment