python – 功课帮助?制作一个螺旋形
发布时间:2020-12-20 13:40:34 所属栏目:Python 来源:网络整理
导读:因此,在休息一周,我们的老师给了我们一个需要Spirograph的小项目,这是他之前帮我们写过的代码 from graphics import *from math import *def ar(a): return a*3.141592654/180def main(): x0 = 100 y0 = 100 startangle = 60 stepangle = 120 radius = 50 wi
因此,在休息一周,我们的老师给了我们一个需要Spirograph的小项目,这是他之前帮我们写过的代码
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() 然后,他希望我们开发连续绘制12个等边三角形的程序(每次将三角形旋转30度通过一个完整的360度圆).这可以通过“踩踏”STARTANGLE参数来实现.我的问题是我被困在这里从哪里开始,他的意思是“踩到什么?”我假设做了某种循环,是否有人可以给我一个正确的步骤? 解决方法
这是使用matplotlib的解决方案.一般程序是一样的.但是您必须修改它以使用您允许使用的库.
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在列表中的每个点之间绘制线条.我们将原点添加到inn中,因此我们得到三角形而不是围绕中心的多边形. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |