问题
When we instantiate a turtle object, we can draw a circle. I wonder about the radius parameter of the circle()
method.
import turtle
myTurtle = turtle.Turtle()
myTurtle.circle(50)
What is the unit of measurement of this parameter?
Does the radius equal to 50 pixels or 50 inches?
回答1:
the documentation for turtle.setup indicates that size parameters, if expressed as integers, are pixels, if expressed as floats, are fractions of the screen.
回答2:
As you can see from the first few lines from the documentation, the forward method uses the unit pixel and since there is no other unit used in the documentation, you can conclude the all methods use pixel.
Edit: After looking at the source code form turtle, I'm on 100% sure that it is using pixel as unit, since it is adding the distance to the position directly.
回答3:
It depends on whether we're measuring the image on the screen, or a printed PostScript image obtained through the underlying tkinter canvas.
My Dell display has a pixel pitch of 0.282mm so I expect to see 90 dots per inch. If turtle draws a circle with a radius of 45 pixels, what I measure on my screen is a circle with a 1" diameter.
However, if I print that image, and turn off any printing scaling, I won't get a 1" printed circle, but something larger. To achieve a properly printed circle, turtle needs to draw with a radius of 36, as the underlying measure for PostScript conversion seems to be based on points (@ 72 per inch):
from turtle import Turtle, Screen
screen = Screen()
screen.setup(400, 600)
tortoise = Turtle()
tortoise.circle(36)
canvas = screen.getcanvas()
canvas.postscript(file="circle.ps")
screen.exitonclick()
Although this circle measures around 3/4" on my screen, the PostScript output on the printer is 1". (Make sure to turn off any automatic scaling features and print @ 100%)
The actual size of the image on your monitor depends on its pixel pitch. Although tkinter can work in fixed size points for both display and print, Python turtle only works in variable size pixels for display. Once your determine your monitor's pixel pitch, you can use your own scaling factor, or:
turtle.setworldcoordinates(llx, lly, urx, ury)
to configure your own scaling:
from turtle import Turtle, Screen
PIXEL_PITCH = 0.282 # mm
MM_PER_INCH = 25.4
DOTS_PER_INCH = int(1 / (PIXEL_PITCH / MM_PER_INCH))
screen = Screen()
screen.setup(4 * DOTS_PER_INCH, 5 * DOTS_PER_INCH)
screen.setworldcoordinates(-2, -2.5, 2, 2.5) # convert to inches
tortoise = Turtle()
tortoise.circle(0.5) # 1/2" radius, one inch diameter on my screen
screen.exitonclick()
Which should be reasonably accurate on the screen but not print correctly without extra work to switch units during PostScript conversion or use the correct scaling factor in your print dialog.
回答4:
Usage of the circle()
function:
circle(radius, extent=None, steps=None)
See more here: https://docs.python.org/2/library/turtle.html#turtle.circle
So yes, it is the radius as pixels, the first parameter of the function.
来源:https://stackoverflow.com/questions/49595669/python-turtle-unit-of-measurement