用pil模拟一下小时候的一个玩具
以前玩过个玩具,类似于齿轮,一个小齿轮在一个大齿轮里面转,把笔插入那个小齿轮的小孔,然后一圈一圈转,就有了一个神奇的图案。用python写了下,也不知对不对,图是出来了,分析了一下规律,应该就是一个公转自转问题。下面上代码:#! /usr/bin/env python#coding=utf-8import Image,ImageDraw,sys,math def magic(bigc, bigr, smallr, l): thita = 0 for thita_big in xrange(36000): thita_small = thita_big*(float(bigr)/smallr) #print(thita_small) smallc = (bigc+math.cos(thita_big)*bigr, bigc+math.sin(thita_big)*bigr) point = (smallc+math.cos(thita_small+thita)*smallr, smallc+math.sin(thita_small+thita)*smallr) sep(point,"#FF0000")def sep(point, fillVal): draw.point(point, fill=fillVal)im = Image.new("RGB",(500,500), "#FFFFFF")draw = ImageDraw.Draw(im);center = (250,250)radius = 100magic(center,radius,10,2)del drawim.save("c:/t.jpg")
下面是效果图:
http://dl.iteye.com/upload/attachment/469348/d8d2122b-5cf0-31a4-9764-1ed987c3d38b.jpg
换一下参数:
http://dl.iteye.com/upload/attachment/469354/5aec42e0-7bc5-3621-ab1b-69b9a0f15280.jpg
http://dl.iteye.com/upload/attachment/469350/65ae57ee-5157-3eec-813b-3396a1ea64ea.jpg
页:
[1]