| 1 | n/a | #!/usr/bin/env python3 |
|---|
| 2 | n/a | """ turtle-example-suite: |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | tdemo_planets_and_moon.py |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | Gravitational system simulation using the |
|---|
| 7 | n/a | approximation method from Feynman-lectures, |
|---|
| 8 | n/a | p.9-8, using turtlegraphics. |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | Example: heavy central body, light planet, |
|---|
| 11 | n/a | very light moon! |
|---|
| 12 | n/a | Planet has a circular orbit, moon a stable |
|---|
| 13 | n/a | orbit around the planet. |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | You can hold the movement temporarily by |
|---|
| 16 | n/a | pressing the left mouse button with the |
|---|
| 17 | n/a | mouse over the scrollbar of the canvas. |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | """ |
|---|
| 20 | n/a | from turtle import Shape, Turtle, mainloop, Vec2D as Vec |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | G = 8 |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | class GravSys(object): |
|---|
| 25 | n/a | def __init__(self): |
|---|
| 26 | n/a | self.planets = [] |
|---|
| 27 | n/a | self.t = 0 |
|---|
| 28 | n/a | self.dt = 0.01 |
|---|
| 29 | n/a | def init(self): |
|---|
| 30 | n/a | for p in self.planets: |
|---|
| 31 | n/a | p.init() |
|---|
| 32 | n/a | def start(self): |
|---|
| 33 | n/a | for i in range(10000): |
|---|
| 34 | n/a | self.t += self.dt |
|---|
| 35 | n/a | for p in self.planets: |
|---|
| 36 | n/a | p.step() |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | class Star(Turtle): |
|---|
| 39 | n/a | def __init__(self, m, x, v, gravSys, shape): |
|---|
| 40 | n/a | Turtle.__init__(self, shape=shape) |
|---|
| 41 | n/a | self.penup() |
|---|
| 42 | n/a | self.m = m |
|---|
| 43 | n/a | self.setpos(x) |
|---|
| 44 | n/a | self.v = v |
|---|
| 45 | n/a | gravSys.planets.append(self) |
|---|
| 46 | n/a | self.gravSys = gravSys |
|---|
| 47 | n/a | self.resizemode("user") |
|---|
| 48 | n/a | self.pendown() |
|---|
| 49 | n/a | def init(self): |
|---|
| 50 | n/a | dt = self.gravSys.dt |
|---|
| 51 | n/a | self.a = self.acc() |
|---|
| 52 | n/a | self.v = self.v + 0.5*dt*self.a |
|---|
| 53 | n/a | def acc(self): |
|---|
| 54 | n/a | a = Vec(0,0) |
|---|
| 55 | n/a | for planet in self.gravSys.planets: |
|---|
| 56 | n/a | if planet != self: |
|---|
| 57 | n/a | v = planet.pos()-self.pos() |
|---|
| 58 | n/a | a += (G*planet.m/abs(v)**3)*v |
|---|
| 59 | n/a | return a |
|---|
| 60 | n/a | def step(self): |
|---|
| 61 | n/a | dt = self.gravSys.dt |
|---|
| 62 | n/a | self.setpos(self.pos() + dt*self.v) |
|---|
| 63 | n/a | if self.gravSys.planets.index(self) != 0: |
|---|
| 64 | n/a | self.setheading(self.towards(self.gravSys.planets[0])) |
|---|
| 65 | n/a | self.a = self.acc() |
|---|
| 66 | n/a | self.v = self.v + dt*self.a |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | ## create compound yellow/blue turtleshape for planets |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | def main(): |
|---|
| 71 | n/a | s = Turtle() |
|---|
| 72 | n/a | s.reset() |
|---|
| 73 | n/a | s.getscreen().tracer(0,0) |
|---|
| 74 | n/a | s.ht() |
|---|
| 75 | n/a | s.pu() |
|---|
| 76 | n/a | s.fd(6) |
|---|
| 77 | n/a | s.lt(90) |
|---|
| 78 | n/a | s.begin_poly() |
|---|
| 79 | n/a | s.circle(6, 180) |
|---|
| 80 | n/a | s.end_poly() |
|---|
| 81 | n/a | m1 = s.get_poly() |
|---|
| 82 | n/a | s.begin_poly() |
|---|
| 83 | n/a | s.circle(6,180) |
|---|
| 84 | n/a | s.end_poly() |
|---|
| 85 | n/a | m2 = s.get_poly() |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | planetshape = Shape("compound") |
|---|
| 88 | n/a | planetshape.addcomponent(m1,"orange") |
|---|
| 89 | n/a | planetshape.addcomponent(m2,"blue") |
|---|
| 90 | n/a | s.getscreen().register_shape("planet", planetshape) |
|---|
| 91 | n/a | s.getscreen().tracer(1,0) |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | ## setup gravitational system |
|---|
| 94 | n/a | gs = GravSys() |
|---|
| 95 | n/a | sun = Star(1000000, Vec(0,0), Vec(0,-2.5), gs, "circle") |
|---|
| 96 | n/a | sun.color("yellow") |
|---|
| 97 | n/a | sun.shapesize(1.8) |
|---|
| 98 | n/a | sun.pu() |
|---|
| 99 | n/a | earth = Star(12500, Vec(210,0), Vec(0,195), gs, "planet") |
|---|
| 100 | n/a | earth.pencolor("green") |
|---|
| 101 | n/a | earth.shapesize(0.8) |
|---|
| 102 | n/a | moon = Star(1, Vec(220,0), Vec(0,295), gs, "planet") |
|---|
| 103 | n/a | moon.pencolor("blue") |
|---|
| 104 | n/a | moon.shapesize(0.5) |
|---|
| 105 | n/a | gs.init() |
|---|
| 106 | n/a | gs.start() |
|---|
| 107 | n/a | return "Done!" |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | if __name__ == '__main__': |
|---|
| 110 | n/a | main() |
|---|
| 111 | n/a | mainloop() |
|---|