`
rokuan
  • 浏览: 19660 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

用pil模拟一下小时候的一个玩具

阅读更多
以前玩过个玩具,类似于齿轮,一个小齿轮在一个大齿轮里面转,把笔插入那个小齿轮的小孔,然后一圈一圈转,就有了一个神奇的图案。用python写了下,也不知对不对,图是出来了,分析了一下规律,应该就是一个公转自转问题。下面上代码:
#! /usr/bin/env python
#coding=utf-8
import 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[0]+math.cos(thita_big)*bigr, bigc[1]+math.sin(thita_big)*bigr)
        point = (smallc[0]+math.cos(thita_small+thita)*smallr, smallc[1]+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 = 100
magic(center,radius,10,2)
del draw

im.save("c:/t.jpg")


下面是效果图:

换一下参数:




  • 大小: 8.7 KB
  • 大小: 28.9 KB
  • 大小: 65 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics