IT编程 > 脚本编程 > Python

Python编写万花尺图案实例

13人参与2021-01-03

小的时候大家应该都玩过万花尺,将笔尖放置万花尺内不停的转动,便可以画出一幅精致的线稿图,现在我们不用万花尺,我们通过python也能绘制出万花尺图案。

(一)代码

#!/usr/bin/python# -*- coding: utf-8 - *-
import mathclass pts:
def __ init __(self):
self.x = 0
self.y = 0
points = []
def linetodemo():
from tkinter import *
screenx = 400
screeny = 400
canvas = canvas(width = screenx,height = screeny,bg = ‘white')

aspectratio = 0.85
maxpts = 15
h = screeny
w = screenx
xcenter = w / 2
ycenter = h / 2
radius = (h - 30) / (aspectratio * 2) - 20
step = 360 / maxpts
angle = 0.0
for i in range(maxpts):
  rads = angle * math.pi / 180.0
  p = pts()
  p.x = xcenter + int(math.cos(rads) * radius)
  p.y = ycenter - int(math.sin(rads) * radius * aspectratio)
  angle += step
  points.append(p)
canvas.create_oval(xcenter - radius,ycenter - radius,
          xcenter + radius,ycenter + radius)
for i in range(maxpts):
  for j in range(i,maxpts):
    canvas.create_line(points[i].x,points[i].y,points[j].x,points[j].y)

canvas.pack()
mainloop()if __name__ == '__main__':
linetodemo()

(二)实现结果

我们还可以通过修改不同的参数来实现不同的图案转化,大家可以多试试,做出多种不同的效果图。

到此这篇关于python编写万花尺图案实例的文章就介绍到这了,更多相关python之万花尺图案内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

您对本文有任何疑问!!点此进行留言回复

推荐阅读

猜你喜欢

Python编写万花尺图案实例

01-03

Python 实现一个简单的web服务器

01-03

关于Python错误重试方法总结

01-03

详解python中的异常和文件读写

01-03

python绘制雷达图实例讲解

01-03

学会迭代器设计模式,帮你大幅提升python性能

01-03

拓展阅读

大家都在看

pycharm 2020 Error: failed to send plot to http://127.0.0.1:63342

07-30

Numpy用于数据存储和读取

12-06

Pycharm2020最新激活码|永久激活(附最新激活码和插件)

01-08

Python导入父文件夹中模块并读取当前文件夹内的资源

11-19

Python编写一个优美的下载器

06-17

安装ROS后初始化时出现The ‘rosdep==0.19.0‘ distribution was not found and is required by the application错误

07-30

十条建议帮你提高Python编程效率

05-31

django的ORM模型的实现原理

03-16

热门评论