python极客项目编程pdf微盘下载_《Python极客项目编程 》——2.4 完整代码

本节书摘来自异步社区《Python极客项目编程 》一书中的第2章,第2.4节,作者 [美] Mahesh Venkitachalam,王海鹏 译,更多章节内容可以访问云栖社区“异步社区”公众号查看。

2.4 完整代码

下面是完整的万花尺程序。也可以从https://github.com/electronut/pp/blob/master/ spirograph/spiro.py 下载该项目的代码。

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 that draws a Spirograph

class Spiro:

# constructor

def __init__(self, xc, yc, col, R, r, l):

# create the turtle object

self.t = turtle.Turtle()

# set the cursor shape

self.t.shape('turtle')

# set the step in degrees

self.step = 5

# set the drawing complete flag

self.drawingComplete = False

# set the parameters

self.setparams(xc, yc, col, R, r, l)

# initialize the drawing

self.restart()

# set the parameters

def setparams(self, xc, yc, col, R, r, l):

# the Spirograph parameters

self.xc = xc

self.yc = yc

self.R = int(R)

self.r = int(r)

self.l = l

self.col = col

# reduce r/R to its smallest form by dividing with the GCD

gcdVal = gcd(self.r, self.R)

self.nRot = self.r//gcdVal

# get ratio of radii

self.k = r/float(R)

# set the color

self.t.color(*col)

# store the current angle

self.a = 0

# restart the drawing

def restart(self):

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值