""" By Kirby Urner, 4D Solutions, Portland, Oregon (copyleft) 2006 Last revised by the original author: 14 Nov 2006 (added Moon class) Complex numbers provide machinery for rotating in the XY plane, where the Y axis is "imaginary", the X axis "real". When two complex numbers multiply, their magnitudes are multiplied (think of vectors) while their angles get added. By keeping the magnitudes = 1, we focus on the angles. A scale factor applied afterwards can change the radius of the orbit. Or we may use the fact that e**(theta*complex(0,1)) takes us around in a circle. This 2nd technique shows up in a Planet class, easiest for having multiple balls, each with its own radius, color, increment, size of orbit. Since visual is expecting three floating points (x,y,z) for position, we use c.real and c.imag to break c into two floating point numbers for plotting purposes. """ import cmath import math from visual import * def orbit1(): """ multiply clock hand by a one degree incrementer """ m = complex(1,0) # vector pointing to 3 o'clock angle = math.radians(1.0) # normalized to unit radius (clock diameter = 2) onedegree = complex(cos(angle), sin(angle)) theball = sphere(pos = (m.real, m.imag), radius = 0.1, color = color.red ) while True: m = m * onedegree # multiplying causes rotation theball.pos = (m.real, m.imag) rate(100) def orbit2(): """ use e**(theta * i) to get coordinates of rotating ball cmath.exp handles a complex number """ onedegree = math.radians(1.0) i = complex(0,1) angle = 0 theball = sphere(pos = (1,0,0), radius = 0.1, color = color.green ) while True: angle += onedegree coords = cmath.exp(angle * i) theball.pos = (coords.real, coords.imag) rate(50) class Planet (object): # subclassing object gives a 'new style' class """ Orbit around (0,0,0) by increment (degrees) per each move invocation """ def __init__(self, distance=1.0, radius=0.1, increment=1, color = color.red): self.pos = (1.0, 0.0, 0.0) self.distance = distance self.radius = radius self.color = color self.theball = sphere(pos = self.pos, radius = self.radius, color = self.color) self.increment = math.radians(increment) self.angle = 0 def move(self): self.angle = self.angle + self.increment coords = cmath.exp(self.angle * complex(0,1)) self.coords = (coords.real * self.distance, coords.imag * self.distance) self.theball.pos = (self.coords[0], self.coords[1]) class Moon(Planet): """ Orbit around planet (1st argument) by increment (degrees) per each move invocation """ def __init__(self, planet, distance=1.0, radius=0.1, increment=1, color = color.red): super(Moon,self).__init__(distance, radius, increment, color) self.planet = planet def move(self): self.angle = self.angle + self.increment coords = cmath.exp(self.angle * complex(0,1)) self.coords = (coords.real * self.distance + self.planet.coords[0], coords.imag * self.distance + self.planet.coords[1]) self.theball.pos = (self.coords[0], self.coords[1]) def solarsystem(): sun = Planet(distance = 0, radius = 0.8, color = color.yellow) p1 = Planet(distance=6.0, radius=0.3, increment=0.5, color = color.green) p2 = Planet(distance=4.0, radius=0.1, increment=1.0, color = color.red) m1 = Moon(p1, distance = 1.0, radius = 0.1, increment = 3.0, color = color.cyan) while True: p1.move() p2.move() m1.move() rate(50)