d0n.key
d0n.key

Reputation: 1483

Python: Turtle size in pixels

How do I get the dimensions of Python's turtle in pixels?

Upvotes: 4

Views: 14597

Answers (4)

SadSack963
SadSack963

Reputation: 2059

Most of the standard shapes fit inside a 21 x 21 square. The arrow is much smaller as is the triangle and classic, and the turtle feet do not rest on the bottom of the square and the head sticks out quite a long way.

Turtle shapes

Turtle Shapes - Python Code

Why 21 x 21? I guess because this allows the turtle to be centered on a pixel rather than centered between two pixels, which certainly makes the circle much more symetrical and also allows the pen to draw accurately.

Upvotes: 2

raianmr
raianmr

Reputation: 147

The way the size and behavior of Python's turtle work isn't obvious. I'm too lazy to go through all the implementation code but I think I can at least explain the situation.

The default size of the square turtle really is 21x21 in practice as someone pointed out. However, when you try to resize it using shapesize() you'll notice something weird. If you write something like,

turtle.shapesize(stretch_wid=2, stretch_len=0.5)

The resulting width should be 42 and length should be either 10 (floor) or 11 (ceiling) in theory. What we see in reality is that the new length is 11 but the new width is 41 instead.

The only way all this would make sense is if the actual size of the default square turtle was 20x20 but it only showed 21x21 on the screen. In my tests, it shows 81x81, 41x41, 11x11, 6x6 instead of 80x80, 40x40, 10x10, 5x5 respectively. This hypothesis also explains why giving a stretch factor of 0.95 results in a 20x20 square turtle: 20 times 0.95 is 19, but the final result shows 1 extra pixel in both the axes so ultimately you see a 20x20 square turtle on the screen.

The CPython implementation of the turtle module could be flawless, but I think we can all agree that this is bad design. Therefore, I highly advise that you use Pygame for projects involving animation and Tkinter for working with static or very low framerate graphics. The turtle module can be a real pain to work with even when you're looking for a challenge, it's best to find an alternative instead.

Upvotes: 2

cdlane
cdlane

Reputation: 41905

The turtle shapes are stored in a dictionary in the TurtleScreen class where the key for shape square, for example, contains:

Shape("polygon", ((10,-10), (10,10), (-10,10), (-10,-10)))

All of the cursor shapes appear to fit within this 20 x 20 square. What I do when I program with shapesize() is the following:

STAMP_SIZE = 20
...
turtle.shapesize(size / STAMP_SIZE)

Where size is how large (or small) I want the cursor to be in pixels. This doesn't have to be uniform, sometimes I make lines by doing:

turtle.shapesize(size / STAMP_SIZE, 1 / STAMP_SIZE)  # vertical line

Remember that the width and length arguments to shapesize() are relative to the turtle's orientation so width is in the direction the turtle is pointing and length is perpendicular to that.

If you really want to know the dimensions of the current turtle cursor (including after any transformations), you can use turtle.get_shapepoly(). But this gives you a polygon so you'll need to figure out height, width, area or what it is you need from the polygon verticies.

Upvotes: 5

PyNuts
PyNuts

Reputation: 73

I know exactly what you mean, shapesize does not equal the width in pixels and it had me buggered for a day or 2.

I ended up changing the turtle shape to a square and simply using print screen in windows to take a snap shot of the canvas with the square turtle in the middle, then took that print screen into Photoshop to zoom right up to the square until i could see the pixel grid, i found that the default size of the square turtle is 21x21 pixels, who knows why they made it 21 but i found if i take some solid value i want the turtle to be in pixels like 20 and divide that by the 21 i get 0.9523...

Rounding the value to 0.95 and putting that into the shapesize gave me a square turtle of exactly 20x20 pixels and made it much easier to work with, you can do this with any number you want the turtle to be in pixels but i have only tried it with the square turtle.

I hope that helps somewhat or gives you an idea of how to find the turtle size in pixels, ms-paint will do the same providing you goto the view tab and turn on grid then zoom in as far as you can.

Upvotes: 1

Related Questions