-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspirograph.py
More file actions
53 lines (41 loc) · 1.47 KB
/
spirograph.py
File metadata and controls
53 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
###############################################################################
# This program makes a computerized version of a spirograph
###############################################################################
# imports
from turtle import *
from math import cos, sin, radians
def get_input():
# the 3 apostrophes is a special type of comment called a doc string
''' Function to get user input '''
R = int(input('Radius of larger circle: '))
r = int(input('Radius of smaller circle: '))
d = int(input('Distance from center of small circle: '))
spins = int(input('Number of rotations: '))
return R, r, d, spins
def get_point(angle, R, r, d):
''' Calculate the x and y values for the current point '''
angle = radians(angle)
x = (R - r) * cos(angle) + d * cos((R - r) / r * angle)
y = (R - r) * sin(angle) - d * sin((R - r) / r * angle)
return x, y
def main():
# set up turtle
setup(800, 800)
speed(0)
bgcolor('black')
colormode(255)
# spirograph parameters
R, r, d, rotations = get_input()
# go to starting point
penup()
start_x, start_y = get_point(0, R, r, d)
goto(start_x, start_y)
pendown()
# run spirograph
for angle in range(360):
pencolor(angle * 255 // 360, 0, 255 - angle * 255 // 360)
x, y = get_point(angle * rotations, R, r, d)
goto(x, y)
if __name__ == '__main__':
main()
done()