draw_bicycle.py
# -*- coding: cp936 -*-
# 已知单车的车轮直径约长60cm, 两车轮的圆心距约为100cm, 轮胎径长为4cm或5cm
# 用turtle 画出单车的两个轮子, 要求图形几何相似
# draw_bicycle.py 18 20
import os, sys
import turtle
import math
vlist =[]
def p_line(t, n, length, angle):
"""Draws n line segments."""
tt.pen(fillcolor="blue", pencolor="#fe6023", pensize=5 )
for i in range(n):
t.fd(length)
vlist.append(t.pos())
t.right(angle)
def polygon(t, n, length):
"""Draws a polygon with n sides."""
angle = 360.0/n
p_line(t, n, length, angle)
def draw(tt, n, L):
X,Y = tt.pos() # start_pos
polygon(tt, n, L)
tt.pen(fillcolor="blue", pencolor="#fe6023", pensize=1 )
R = L/2/math.sin(math.pi/n) # 半径
r = R*math.cos(math.pi/n) # 边心距
# 求中心点坐标(x,y)
x = L/2 +X
y = -r +Y
tt.goto(X,Y) # start_pos
for pos in vlist:
tt.goto(x,y)
tt.pendown()
tt.goto(pos)
tt.penup()
print(x,y)
# 画圆
tt.goto(x,0)
tt.pen(fillcolor="blue", pencolor="black", pensize=abs(Y))
tt.pendown()
tt.circle(y,360)
tt.penup()
tt.pen(fillcolor="blue", pencolor="#fe6023", pensize=1 )
# main
if len(sys.argv) ==3:
n = int(sys.argv[1]) # n 边形
L = float(sys.argv[2]) # 边长
else:
print('usage: draw_bicycle.py int Length')
sys.exit(4)
if n < 3:
print('Error: n < 3 ')
sys.exit(4)
if n > 36:
print('Error: n > 36 ')
sys.exit(4)
window= turtle.Screen() #creat a screen
window.bgcolor("white")
tt = turtle.Turtle()
tt.pencolor("black")
tt.fillcolor("violet")
tt.width(1)
tt.speed(10)
tt.pu()
S = L/10 # 几何放大系数
X = 50*S
Y = 4*S # 轮胎径长为4cm
print('X=',X,', Y=',Y)
tt.goto(X,-Y)
tt.pd()
draw(tt,n,L)
tt.pu()
tt.goto(-X,-Y)
tt.pd()
vlist =[]
draw(tt,n,L)
tt.pu()
window.exitonclick()
cmd
draw_bicycle.py 18 20