[Tutor] Drawing inside the turtle canvas

DL Neil PyTutor at DancesWithMice.info
Mon Apr 27 18:18:25 EDT 2020


On 28/04/20 6:33 AM, Stefan Ram wrote:
>    The following program will move the turtle
>    /out of the visible area/:
> 
> from turtle import *
> forward( window_width()/ 2 )

Yes, assuming (as the snippet does) that the turtle is in the vertical 
center of the window, or further to the right/east.


>    I would like it to move as far as possible while it is
>    still completly visible.
> 
>    . Apparently, one needs to obtain the current width of the
>    window border and subtract it from the width.

This depends upon how you have defined the window and/or "canvas"* Do 
you understand the difference between these two terms? Are you using 
them precisely?

It may be helpful to distinguish the drawing-surface, within the 
window-widget, rather than re-using a term like "window" which has 
specific meaning in other contexts.


>    How is this done?


The turtle is a 2-D object inside a 2-D window.

You know the (central?top-left*) position of the turtle.

You know how to measure it, and thus find the size of the drawing-surface.

(which can thus be re-expressed as the space between two Coordinates: ( 
0, 0 ) and ( drawing_width, drawing_height ).

IIRC* the size of the turtle is under programmatic control. So, you now 
need to find its size.

Next, ignore the turtle's appearance, and consider it a rectangle. 
(x-pixels wide, y-pixels high).

Now, with reference to what the turtle reports as its (single-pixel) 
position, we can 'map' it onto the drawing-surface's 2-D Cartesian space 
by relating its size to its "position", and expressing that as a 
rectangle - as per the description (above), use two Coordinates 
representing its top-left and bottom-right pixels! Thus the turtle 
occupies a portion of space, a rectangle (of pixels) from ( x1, y1 ) to 
( x2, y2 )!


Now, we can use algebra and consider each of the four sides in-turn 
(two-sides per dimension!).

Do not worry about the turtle's current position (we assume it is 
on-screen)! Calculate its next position (after direction and speed/rate) 
- without regard to the drawing-surface/its edges. (However, do not 
display the turtle in its new position until we've checked it!)

The problem re-stated:
If the turtle continues to move at its current rate, in its current 
direction, would the edge of its space start to run 'off' the edge of 
the drawing-surface?

Remember there are four edges (two each, for two dimensions - um, yes, 
er, ok!) So we don't talk about THE edge of the drawing-surface (as I 
did, above) but FOUR separate considerations for four edges:

1 consider the turtle's left-edge in relation to the drawing-surface's 
left-border: assert x1 > 0 (this zero is the x-origin)

2 consider the turtle's right-edge in relation to the drawing-surface's 
right-border: assert x2 < drawing_width

3 ...top...

4 ...bottom...

If one (or two) of the above conditions are false, modify the turtle's 
trajectory, as appropriate (and calculate such a modification to its 
next position prior to display).


* apologies, I have only used Python's turtle briefly, and in the 
context of helping a bunch of kids play with the language. Accordingly, 
some of the finer details may have slipped my memory since - assuming I 
ever really knew them! The low-level computer graphics calculations have 
been applicable, across libraries, translated into various languages, 
and down through the decades, since character (only) terminals. Silver 
Surfers rule! Today, they are applicable to tkinter, HTML5 Canvas, 
JavaScript, etc, etc.

NB if you are learning Python, you may find the Python-Tutor list helpful.
-- 
Regards =dn


More information about the Tutor mailing list