简体   繁体   中英

homework help? for making a spirograph

SO over break week our teacher gave us a little project to do requiring a Spirograph, here is the code he helped us write before

from graphics import *
from math import *

def ar(a):
    return a*3.141592654/180

def main():
    x0 = 100
    y0 = 100
    startangle = 60
    stepangle = 120
    radius = 50

    win = GraphWin()

    p1 = Point(x0 + radius * cos(ar(startangle)), y0 + radius * sin(ar(startangle)))

    for i in range(stepangle+startangle,360+stepangle+startangle,stepangle):
        p2 = Point(x0 + radius * cos(ar(i)), y0 + radius * sin(ar(i)))
        Line(p1,p2).draw(win)
        p1 = p2

    input("<ENTER> to quit...")
    win.close()

main()

he then wants us to develop the program that consecutively draws 12 equilateral triangles (rotating the triangle each time by 30 degrees through a full 360 circle). This can be achieved by “stepping” the STARTANGLE parameter. My question I am stuck on where to go from here, what does he mean by "stepping?" I assume making some sort of loop, is it possible someone can give me a push in the right step?

This is a solution using matplotlib. The general procedure would be the same. But you will have to modify it for using the libraries you're allowed to use.

from math import radians, sin, cos 
import matplotlib.pyplot as plt

startAngle = 0 
stepAngle = 30
origin = (0,0)

points = []
points.append(origin)
points.append((cos(radians(startAngle)), sin(radians(startAngle))))

for i in range(startAngle + stepAngle, 360 + stepAngle, stepAngle):
    x = cos(radians(i))
    y = sin(radians(i))
    points.append((x,y))
    points.append(origin)
    points.append((x,y))

x,y = zip(*points) #separate the tupples into x and y coordinates. 

plt.plot(x,y) #plots the points, drawing lines between each point
plt.show()

plt.plot draw lines between each point in the list. We're adding inn the origin points so we get triangles instead of just a polygon around the center. 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM