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):
    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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值