python : turtle 画单车两轮

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):
    global X,Y
    tt.seth(0)
    tt.pen(fillcolor="blue", pencolor="black", pensize=1 )
    start_pos = tt.pos() # start_pos
    polygon(tt, n, L)
    
    R = L/2/math.sin(math.pi/n) # 半径  
    r = R*math.cos(math.pi/n)   # 边心距 
    # 求中心点坐标(x,y)    
    x = L/2 + start_pos[0]
    y = -r  + start_pos[1]  
    tt.goto(start_pos)  # 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=Y)
    tt.pendown()
    tt.circle(y,360)
    tt.penup()
    # 画轮罩
    tt.goto(x,abs(Y))
    tt.pen(fillcolor="blue", pencolor="yellow", pensize=Y)
    tt.pendown()
    tt.circle(-(R+2*Y),90) # 正负号是画线方向
    tt.penup()
    tt.goto(x,abs(Y))
    tt.seth(180) # 转向180度
    tt.pendown()
    tt.circle(R+2*Y,90) # 正负号是画线方向
    tt.penup()
    print 'heading:',tt.heading()

# 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

猜你喜欢

转载自blog.csdn.net/belldeep/article/details/80042514