moving object along circle
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Nov 20 22:46:39 EST 2012
On Tue, 20 Nov 2012 19:35:00 -0800, sparx10 wrote:
> I'm trying to move an object along a circle (orbit), and I did come up
> with this:
>
> radius = 100
> from math import sqrt
> for x in range(-radius,radius):
> y = sqrt(radius**2-x**2)
> print(x, y)
>
> however it moves faster at the beginning and end of the range (y value
> changes faster than x value) because the x value is changing at a
> constant rate but the y value isn't. I can't think of a way to get
> something to move smoothly around in a circle though..
Instead of using rectangular (x, y) coordinates directly, use polar
coordinates (r, θ) where r (radius) is the constant radius of your
circle, and θ (theta) smoothly varies between 0 and 360°.
http://www.teacherschoice.com.au/maths_library/coordinates/polar_-_rectangular_conversion.htm
import math
radius = 100
for angle in range(0, 361):
theta = math.radians(angle)
x = radius*math.cos(theta)
y = radius*math.sin(theta)
print(x, y)
--
Steven
More information about the Python-list
mailing list