python用turtle设置画布_Python turtle.setup()将画布截断为屏幕大小-如何避免?

为了测量一根杆的转速,我需要做一个刻度盘,刻度盘上有大量交替排列的暗/透明段。旋转的表盘会打断光传感器上的光,然后我只需要测量光脉冲的频率。Python turtle图形似乎是绘制此刻度盘的好主意。

我需要画这个图像非常大,以避免楼梯的影响,对各部分的边缘-我需要平滑的边缘。但如果我做turtle.setup(x,y)的x或y大于屏幕,画布将被截断以适合屏幕。如何避免?

我的代码包含在最后。这里有一个截短画布的截图,x=y=1420

编辑:只需在结束时清除-getscreen()/getcanvas()即可捕获此截断的画布图像并将其按原样(截断)保存到EPS文件中。这就是困扰我的原因。我需要在一个高分辨率的图像文件中捕获整个圆。

我在Ubuntu 13.04上使用python-2.7.4

这是代码:#!/usr/bin/python

# set this to 1 to troubleshoot

debug = 0

import turtle

import math

# image file with the result

fname="dial.eps"

# number of lines

n = 100

# external radius

r2 = 700

# length of each line

l = round(r2 / 10)

r1 = r2 - l

# pen thickness

# tuned for 50% fill factor at the inner end of each line

# (dark stripe and transparent stripe have equal width there)

thick = 2 * math.pi * r1 / float(2 * n)

print "thickness =", thick

# setup screen size to contain the whole circle, plus a little extra

border = 20 + thick

turtle.setup(2 * r2 + border, 2 * r2 + border)

dot = turtle.Turtle()

dot.speed(0)

dot.hideturtle()

# draw crosshairs in the center

dot.setpos(l, 0)

dot.setpos(-l, 0)

dot.home()

dot.setpos(0, l)

dot.setpos(0, -l)

dot.penup()

# thickness of lines

dot.pensize(thick)

for step in range(0, n):

a = 360.0 * step / float(n)

arad = math.radians(a)

x1 = r1 * math.cos(arad)

y1 = r1 * math.sin(arad)

x2 = r2 * math.cos(arad)

y2 = r2 * math.sin(arad)

if debug == 1:

print "a =", a, "\t x1 =", x1, "\t y1 =", y1, "\t x2 =", x2, "\t y2 =", y2

dot.penup()

dot.setpos(x1, y1)

dot.pendown()

dot.setpos(x2, y2)

ts = turtle.getscreen()

ts.getcanvas().postscript(file=fname)

print "Saved image to: ", fname

print "All done. Click image to exit."

turtle.exitonclick()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值