• 用turtle模块创建图形
  • 使用参数方程
  • 利用数学方程生成曲线
  • 用线段画曲线
  • 用定时器来生成图形动画
  • 将图形保存为图像文件

利用海龟画图绘制圆

import math
import turtle#Python的turtle模块来创建图案#draw the circle using turtle
def drawCircleTurtle(x,y,r):#move to the dtart of circleturtle.up()#调用up告诉python提笔,即让笔离开虚拟的纸turtle.setpos(x+r,y)#绘图前先定义海龟的位置turtle.down()#调用down,开始绘图#draw the circlefor i in range(0,365,5):#步长为5,变量i为角度参数a = math.radians(i)#将角度转换为弧度turtle.setpos(x+r*math.cos(a),y+r*math.sin(a))#绘制圆的参数方程drawCircleTurtle(100,100,50)#调用圆形绘图函数
turtle.mainloop()#调用mainloop(),保持tkinter窗口打开

万花尺

  • Spiro函数创造新turtle对象
  • getParams()方法:初始化Spiro对象
  • restart()方法:重置Spiro对象的绘制参数
  • draw()方法:用连续的线段绘制曲线
  • update()方法:利用线段绘制曲线创建动画
  • SpiroAnimator类:同时绘制随机螺线
  • genRandomParams()方法:生成随机参数
  • restart()方法:重新启动程序
  • update()方法:由定时器调用,以动画的形式更新所有的Spiro对象
  • toggleTurtles()方法:显示或隐藏光标
  • saceDrawing()方法:绘制保存PNG图像文件
import sys, random, argparse
import numpy as np
import math
import turtle
import random
from PIL import Image
from datetime import datetime
from fractions import gcd#a class tat draws a Spirough
class Spiro:#constructordef __init__(self,xc,yc,col,R,r,l):#creat the turtle objectself.t = turtle.Turtle()#创建一个新的turtle对象#set the cursor shapeself.t.shape('turtle')#在光标的位置设置海龟#set the step in degreesself.step = 5#将参数绘图角度的增量设置为5#setr the drawing cpmplete flagself.drawingComplete = False#设置提个标志,在动画中使用它,它会产生一组螺线#set the parametersself.setparams(xc,yc,col,R,r,l)#调用设置函数#initialize the drawingself.restart()#调用设置函数# set the parametersdef setparams(self, xc, yc, col, R, r, l) :# the Spirograph parametersself.xc = xc  # 保存曲线中心坐标self.yc = ycself.R = int(R)  # 将每个圆的半径转换为整数,并保留这些数值self.r = int(r)self.l = lself.col = col# reduce r/R to its smallest form by dividing with the GCOgcdVal = gcd(self.r, self.R)  # Python模块feacyion内置的gcd()方法来计算半径的GCDself.nRot = self.r // gcdVal  # 确定曲线的周期性# get ratio of radiiself.K = r / float(R)# set the colorself.t.color(*col)# store the current angleself.a = 0  # 保存当前角度,用来创建动画"""restart()方法重置Spiro对象绘制参数,让他准备好重画"""# restart the drawingdef restart(self) :# set the flagself.drawingComplete = False  # 布置布尔标志drawingComplete ,确定绘图是否完成# show the turtleself.t.showturtle()  # 显示海龟图标# go to the first pointself.t.up()  # 提笔R, K, l = self.R, self.K, self.l  # 设置局部变量a = 0.0x = R * ((l - K) * math.cos(a) + l * K * math.cos((l - K) * a / K))  # 计算角度a设为0时,x和y坐标y = R*((l - K) * math.sin(a) - l * K * math.sin((l - K) * a / K))self.t.setpos(self.xc + x, self.yc + y)  # 移动到下一个位置而不画线self.t.down()  # 落笔# draw the whole thingdef draw(self) :# draw the rest of the pointsR, K, l = self.R, self.K, self.lfor i in range(0, 360 * self.nRot + 1, self.step) :  # 遍历i的所有范围,以度表示,a = math.radians(i)x = R * ((l - K) * math.cos(a) + l * K * math.cos((l - K) * a / K))  # 计算参数i的每个值对应的x,y的坐标y = R((l - K) * math.sin(a) - l * K * math.sin((l - K) * a / K))self.t.setpos(self.xc + x, self.yc + y)# drwing is now done sp hide the turtle cursorself.t.hideturtle()# update by on stepedef update(self) :# skip the rest of dteps if doneif self.drawingComplete :  # update()方法检查drawingComplete标志是否设置return  # 若果没有设置,则继续代码其余部分# increment the angleself.a += self.step  # update()增加当前的角度# draw a stepR, K, l = self.R, self.K, self.l# set the anglea = math.radians(self.a)  # 计算当前的角度对应的(x,y)坐标,并将海龟的位置蝶弄到那里,过程画线x = R * ((l - K) * math.cos(a) + l * K * math.cos((l - K) * a / K))y = R*((l - K) * math.sin(a) - l * K * math.sin((l - K) * a / K))self.t.setpos(self.xc + x, self.yc + y)# if drawing is complete,set the flagif self.a >= 360 * self.nRot :  # 检查角度是否达到这条特定曲线定制的完整范围self.drawingComplete = True  # 达到范围设置drawingComplete,即完成绘图# drawing is now done so hide the turtle cursorself.t.hideturtle()  # 调用函数隐藏绘图光标#clear everythingdef clear(self):self.t.clear()#a classfor animal Spirographs
class SpiroAnimator:#construtordef __init__(self,N):#set the timer value in millisecondsself.deltaT = 10#以毫秒为单位的时间间隔,用于定时器#get the window dimensionsself.width = turtle.window_width()#海龟窗口的尺寸self.height = turtle.window_height()#creat the Spiro objectsself.spiros = []#创建空数组,其中将填入spiro对象for i in range(N):#generate random parametersrparams = self.genRandomParams()#调用辅助方法genRandomParams()#set tge spiro parametersspiro = Spiro(*rparams)#创建新的spiro对象,并将其添加到Spiro对象列表,rparams为元组,但是构造函数需要参数列表,*rparams将元组转换为参数列表self.spiros.append(spiro)#call timerturtle.ontimer(self.update,self.deltaT)#  turtle.ontimer()方法每隔deltaT毫秒调用update()# restart spieo drawingdef restart(self) :for spiro in self.spiros :  # 变量所有的spiro对象,清除以前绘制的每条螺线# clearspiro.clear()# generate random parametersrparams = self.genRandomParams()# set the spiro parametersspiro.setparams(*rparams)  # 分配新的螺线参数# restart drawingspiro.restart()  # 重启程序# generate random parametersdef genRandomParams(self) :  # randint()方法:返回指定范围内的随机数width, height = self.width, self.heightR = random.randint(50, min(width, height) // 2)  # R设置为50至窗口短边一半的随机整数r = random.randint(10, 9 * R // 10)  # r设置为R的10%-90%之间l = random.uniform(0.1, 0.9)  # ubiform()方法 l为0.1-0.9之间的随机小数xc = random.randint(-width // 2, width // 2)  # 屏幕内随机选择x,y坐标,选择屏幕上的一个随机点作为螺线的中心yc = random.randint(-height // 2, height // 2)col = (random. random(),  # 随机设置红,绿,蓝色的成份random. random(),random. random())return (xc, yc, col, R, r, l)  # 计算的参数作为一个元祖返回"""update()方法:由定时器调用,以动画的形式更新所有的Spiro对象"""def update(self) :  # 使用计算器nComplete来记录已画的Spiro对象的数目# update all spirosnComplete = 0  # 初始化for spiro in self.spiros :  # 遍历Spiro对象的列表# updatespiro.update()  # 更新# count completed spirosif spiro.drawingComplete :  # 如果一个Spiro完成,计算器加1nComplete += 1# restart if all spiros are completeif nComplete == len(self.spiros) :  # 检查计算器,看所有对象是否已经完成self.restart()# call the timerturtle.ontimer(self.update, self.deltaT)  # 调用计时器的方法,在deltaT毫秒后再次调用update()方法# toggle turtle cursor on and offdef toggleTurtles(self) :for spiro in self.spiros :if spiro.t.isvisible() :spiro.t.hideturtle()else :spiro.t.showturtle()
def saveDrawing():#hide the turtle cursorturtle.hideturtle()#隐藏海龟图标#generate unique filenamesdateStr = (datetime.now()).strftime("%d%b%Y-%H%M%S")#利用时间和时期生成图像文件的唯一名称,日-月-年-时-分-秒fileName ='spiro-'+dateStrprint('saving drawing to %s.eps/png' % fileName)#get the tkinter canvascanvas = turtle.getcanvas()#save the drawing as a postscipt imagecanvas.postscript(file = fileName + '.eps')#利用canvas对象,将图像保存为嵌入式EPS文件格式#use the Pillow module to convert the postscript image file to pngimg = Image.open(fileName + '.eps')#打开EPS文件,将其保存为png格式img.save(fileName + '.png','png')#show the turtle cursorturtle.showturtle()#取消隐藏的海龟图标#main() function
def main():#use sys.argv if neededprint('generate spirograph...')#creat parserdescStr = """This program draws Spirographs using the Turtle module.When run with no arguments,this program draws random Spirogrphs.Terminology:R:radius of outer circler:radius of inner circlel:ratio of hole distance to r""""""解析命令行和参数初始化"""parser = argparse.ArgumentParser(description = descStr)#创建参数解析对象#argoarse为python的解析包#add expected argumentsparser.add_argument('--sparams',nargs=3,dest='sparams',required=False,#向解析器添加--sparams可选参数help = "THe three arguments in sparams:R,r,l,")# parse argsargs = parser.parse_args()#调用函数进行解析#set the width od the drawing windoe to 80 percent of the screen widthturtle.setup(width=0.8)#将绘图窗口的宽度设置为80%的屏幕宽度#set the cursoe shape to turtleturtle.shape('turtle')#设置光标的图像为海龟#set the title to Spirographs!turtle.title('Spirographs!')#设置窗口标题#add the key handle to save our drawingsturtle.onkey(saveDrawing,"s")#按s键保存图像#start listeningturtle.listen()#让窗口监听用户事件#hide the main turtle cursorturtle.hideturtle()#隐藏海龟图标#check for any arguments sent ti --sparams and draw the Spirographif args.sparams:#检查是否有参数传给--sparms,如果有,就从字符串中提取,并用‘列表解析’将它们转换为浮点数params = [float(x) for x in args.sparams]#draw the Spirograph with the given parameterscol = (0.0,0.0,0.0)spiro = Spiro(0,0,col,*params)spiro.draw()else:#若果命令行没有采纳数,就进入随机模式#create the animator objectspiroAnim = SpiroAnimator(4)#创建4幅图像#add a kry handler to toggle the turtle cursorturtle.onkey(spiroAnim.toggleTurtles,"t")#按键t切换海龟光标#add a key handler to restart the animationturtle.onkey(spiroAnim.restart,"space")#处理空格键盘space#start the turtle main loopturtle.mainloop()#调用mianloop():tikinter窗口打开#call main
if __name__ == '__main__':main()

为什么是两个图像?而不是四个图像?
如何向sparams赋值

Python项目——万花尺相关推荐

  1. 引入yml依赖包_手把手教你发布 Python 项目开源包

    编译:机器之心,作者:Gabriel Lerner.Nathan Toubiana 好不容易码了个 python 项目,是不是很兴奋?那么怎么把这个项目发出去让大家看到呢?本文作者写了一份在 GitH ...

  2. python项目了解_神级程序员都是这样来开源 Python 项目!今天算是涨知识了!

    工具和概念 项目布局 当准备一个项目时,正确合理的布局(目录结构)是十分重要的.一个合理的布局意味着想参与开发者不必花时间来寻找某些代码的位置; 凭直觉就可以找到文件的位置.因为我们在处理一个项目,就 ...

  3. Python训练营2021:构建8个真实世界的Python项目

    时长:19h 27m |视频:. MP4,1280×720 30 fps |音频:AAC,44.1 kHz,2ch |大小:9.54 GB 语言:英语+中英文字幕 机译 从Python Web开发的初 ...

  4. Github 年度最受欢迎的 TOP30 Python 项目,超值

    作者 | 俊欣 来源 | 关于数据分析与可视化 今天小编整理归纳了2021年Github上面最受欢迎的30个Python项目,帮助大家在打磨技术与提升自我上面更进一步. 通过代码来获取 Github官 ...

  5. 70+Python项目,面向初学者、中级和经验丰富的开发人员

    建立动手项目将帮助您获得实用的编码技能.一步一步,你将把你的理论知识运用和建立一个令人印象深刻的投资组合.如果您是一名经验丰富的Python开发人员,您可能已经听说并搜索了以下问题:"对于初 ...

  6. 有趣的python项目_推荐个超好玩的Python项目

    原标题:推荐个超好玩的Python项目 来自:Python之禅(微信号:VTtalk) GitHub上有个很有意思的项目,这个项目就是将一张图片转换成一个网页,例如这张蒙娜丽莎的微笑转换成网页之后的效 ...

  7. 完整的python项目流程

    最近看了python项目的打包,有点小感触.感觉自己还不是一个真正的pythoner.没有在开源项目网站留下什么,而又到处宣扬python的简洁与强大,有点小讽刺.于是乎,我想理清自己的思路,不怕迷茫 ...

  8. python项目中requirements的巧用(一键导入所有安装包)

    一个Python 项目中可能安装很多安装包, 再次创建虚拟环境是需要重新安装的话很麻烦也费时间, 或者项目部署的时候避免重装, 可以将现有项目的所有安装包记录在requirements.txt 文件, ...

  9. python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程

    python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程 参考文章: (1)python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程 ...

最新文章

  1. iOS 多级下拉菜单
  2. Linux 服务器配置信息查询方法,国产化申威服务器配置信息查看演示
  3. Java中方法重载和方法重写的区别
  4. php获取一个文件名的函数,PHP 文件系统函数之获取文件名及文件名后缀-php文件...
  5. [mybatis]typeHandlers日期类型的处理
  6. 中国国民休闲状况调查(2020)
  7. Bootstrap表格样式
  8. iphone7测试软件,iPhone7如何测试网速 ping命令测试网速方法介绍
  9. 【李宏毅机器学习】Classification_1 分类(p10) 学习笔记
  10. blood vessel 图像分割_单图像九宫格形式发送朋友圈
  11. IOS拍照控件同时显示拍照与录像
  12. nginx清缓存,网站刷新不过来时用
  13. 《新浪微博用户兴趣建模系统架构》阅读笔记
  14. java什么是工作空间_[Java教程]Java开发工具(Eclipse工作空间的基本配置)
  15. hog特征的matlab实现
  16. 命令行解析工具gflags
  17. Python 自动刷新网页
  18. 夜神模拟器android版本修改器,夜神模拟器怎么用 夜神安卓模拟器使用方法大大全...
  19. kudu底层存储引擎的数据组织方式
  20. 计算机组成原理(动态随机存储器)

热门文章

  1. 基于python的爬虫系统金融数据_基于Python的互联网金融数据采集
  2. 怎么使用php提交百度收录_百度站长工具链接提交主动推送(实时)PHP推送教程
  3. 键入1-5输出对应的礼拜天
  4. NSIS打包的EXE添加公司、描述和版本号的方法
  5. python使用国内源安装包
  6. 2014年武汉的IT行情好像不太好
  7. 常用网站木马文件后门检测工具
  8. 值得反思:强人都是如何“炼”成的?
  9. unity 开发游戏 认识_认识明天鼓舞人心的Unity开发人员
  10. 这一次,Google 誓要扎根中国了!