Python Turtle.setup() 将画布截断为屏幕大小 - 如何避免这种情况?

Flo*_*rei 5 python turtle-graphics

为了测量杆的旋转速度,我需要制作一个表盘,其中有大量交替排列的深色/透明部分,排列成圆圈。旋转转盘会中断光电传感器上的光,然后我只需要测量光脉冲的频率。Python 海龟图形似乎是绘制这个表盘的好主意。

我需要将此图像绘制得非常大,以避免分段边缘出现阶梯效应 - 我需要平滑的边缘。但是,如果我执行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()
Run Code Online (Sandbox Code Playgroud)

scr*_*uss 3

    \n
  • 您不能将海龟画布设置为大于屏幕
  • \n
  • 保存的EPS文件与getcanvas().postscript分辨率无关;您可以以任何尺寸打印它,并且它仍然会以打印机的原始分辨率打印。
  • \n
\n\n

我修改了您的代码以读取屏幕尺寸并相应地更改圆半径:

\n\n
#!/usr/bin/python\n\n# set this to 1 to troubleshoot\n\ndebug = 0\n\nimport turtle\nimport math\nts = turtle.getscreen()\nmax_size = 0\nif ts.window_width > ts.window_height:\n    max_size = ts.window_height()\nelse:\n    max_size = ts.window_width()\n\n# image file with the result\n\nfname = \'dial.eps\'\n\n# number of lines\n\nn = 100\n\n# external radius\n# r2 = 700\n\nr2 = 0.8 * max_size / 2\n\n# length of each line - changed from \'l\', which looks too much like 1\n\nline_length = round(r2 / 10)\n\nr1 = r2 - line_length\n\n# pen thickness\n# tuned for 50% fill factor at the inner end of each line\n# (dark stripe and transparent stripe have equal width there)\n\nthick = 2 * math.pi * r1 / float(2 * n)\nprint \'thickness =\', thick\n\n# setup screen size to contain the whole circle, plus a little extra\n\nborder = 20 + thick\n\n# turtle.setup(2 * r2 + border, 2 * r2 + border)\n\ndot = turtle.Turtle()\ndot.speed(0)\ndot.hideturtle()\n\n# draw crosshairs in the center\n\ndot.setpos(line_length, 0)\ndot.setpos(-line_length, 0)\ndot.home()\ndot.setpos(0, line_length)\ndot.setpos(0, -line_length)\ndot.penup()\n\n# thickness of lines\n\ndot.pensize(thick)\n\nfor step in range(0, n):\n    a = 360.0 * step / float(n)\n    arad = math.radians(a)\n    x1 = r1 * math.cos(arad)\n    y1 = r1 * math.sin(arad)\n    x2 = r2 * math.cos(arad)\n    y2 = r2 * math.sin(arad)\n    if debug == 1:\n        print \'a =\', a, \'\\t x1 =\', x1, \'\\t y1 =\', y1, \'\\t x2 =\', x2, \\\n            \'\\t y2 =\', y2\n    dot.penup()\n    dot.setpos(x1, y1)\n    dot.pendown()\n    dot.setpos(x2, y2)\n\nts.getcanvas().postscript(file=fname)\n\nprint \'Saved image to: \', fname\nprint \'All done. Click image to exit.\'\n\nturtle.exitonclick()\n
Run Code Online (Sandbox Code Playgroud)\n\n

放大到 500%,没有锯齿:

\n\n

EPS输出扩大

\n\n

哦,请永远不要用作l变量名;看起来太像了1糟糕的程序员,没有饼干\xc2\xa0\xe2\x80\xa6\xe2\x98\xba

\n