快活林资源网 Design By www.csstdc.com

小的时候大家应该都玩过万花尺,将笔尖放置万花尺内不停的转动,便可以画出一幅精致的线稿图,现在我们不用万花尺,我们通过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编写万花尺图案实例

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

快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com