u/muntooR_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν}Oct 17 '18edited Oct 17 '18
Your Positions can be greatly simplified:
angle_step = 2 * math.pi / frame_count
angles = (angle_step * i for i in range(frame_count))
coords = [(
center[0] + radius * math.cos(x),
center[1] + radius * math.sin(x))
for x in angles]
self.final_coords = coords[phase:] + coords[:phase]
...but it's really strange that you made it a class. It looks more like a function to me!
Actually, there's a lot of redundancy, unnecessary computation, and somewhat confusing organization. Here's a first attempt at fixing it:
import cairo
import math
class CelestialBody:
def __init__(self, radius, orbit_radius, orbit_center, fill):
self.radius = radius
self.orbit_radius = orbit_radius
self.orbit_center = orbit_center
self.phase = 0.0
self.fill = fill
self.update()
def update(self):
self.center = (
self.orbit_center[0] + self.orbit_radius * math.cos(self.phase),
self.orbit_center[1] + self.orbit_radius * math.sin(self.phase))
def draw(self, context):
context.set_source_rgb(1, 1, 1)
context.arc(*self.center, self.radius, 0, 2 * math.pi)
if self.fill:
context.fill()
else:
context.stroke()
def clear_screen(context):
context.set_source_rgb(0, 0, 0)
context.rectangle(0, 0, 1000, 1000)
context.fill()
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1000, 1000)
context = cairo.Context(surface)
FRAME_COUNT = 100
bodies = {
'blackhole': CelestialBody(125, 0, (500, 500), False),
'planet': CelestialBody(25, 300, (500, 500), True),
'moon': CelestialBody(5, 50, (750, 500), False) }
for i in range(FRAME_COUNT):
clear_screen(context)
bodies['planet'].phase = i * 2 * math.pi / FRAME_COUNT
bodies['moon'].phase = i * 4 * math.pi / FRAME_COUNT
for key, body in bodies.items():
if key == 'moon':
body.orbit_center = bodies['planet'].center
body.update()
for _, body in bodies.items():
body.draw(context)
context.stroke()
surface.write_to_png(f'out/frame{i}.png')
This is a good start. We've eliminated Positionsentirely and converted it into the update function. Also, we're now using a data structure called bodies to keep track of our celestial objects rather than explicitly holding named references (variables).
But it has some problems still. Inside our main loop, we still have to do some case-specific handling:
bodies = { ... }
bodies['planet'].phase = i * 2 * math.pi / FRAME_COUNT
bodies['moon'].phase = i * 4 * math.pi / FRAME_COUNT
for key, body in bodies.items():
if key == 'moon':
body.orbit_center = bodies['planet'].center
Ugh! It'd be much nicer if we could encapsulate this functionality. Perhaps one idea is to bind our celestial bodies together:
class CelestialBody:
def __init__(self, radius, orbit_radius, orbit_center, fill):
self.parent_body = None
# ...
def bind_body(self, body):
self.parent_body = body
def update(self):
if self.parent_body is not None:
self.orbit_center = self.parent_body.center
# ...
35
u/OBstaxs Oct 16 '18
Source ?