what does the turtle.setworldcoordinates function do?

Question:

Beginner’s Python book tend to grow more steep towards the middle pages and hardly explain anything at all. Therefore, I’m having a hard time figuiring out what the turtle.setworldcoordinates function does after being disappointed by my inability to do so by reading the documentation which was very unhelpful and not being able to empirically deduce anything with some ideas of the documentation. Could anyone please point out what this function does in turtle graphics?

Asked By: lind

||

Answers:

Since turtle.py comes with Python, and is written in Python, you can look at the source to see what the function does (which I find I do a lot with turtle.py) See below.

I believe setworldcoordinates() allows you to select a coordinate system that’s more convenient for your problem. For example, suppose you don’t want (0,0) in the center of the screen. Instead of incorporating an offset, you can use setworldcoordinates() to move it to a corner if that works better for you. You can also set up coordinate systems that have a different scaling factor in the horizontal vs. the verical.

For example see my answer to this question where I define a routine that uses setworldcoordinates() to scale anything you draw without you having to incorporate any scaling factor into your own drawing code.

Or you can set a coordinate system with corners at (0,0), (1,0), (0,1) and (1,1) to work completely within the unit square.

The tricky bit is it maps your coordinate system onto the existing window shape — so you either have to adjust your coordinates to the window or reshape the window to match your coordinate system. Otherwise you might find yourself with an undesirable aspect ratio.

def setworldcoordinates(self, llx, lly, urx, ury):
    """Set up a user defined coordinate-system.

    Arguments:
    llx -- a number, x-coordinate of lower left corner of canvas
    lly -- a number, y-coordinate of lower left corner of canvas
    urx -- a number, x-coordinate of upper right corner of canvas
    ury -- a number, y-coordinate of upper right corner of canvas

    Set up user coodinat-system and switch to mode 'world' if necessary.
    This performs a screen.reset. If mode 'world' is already active,
    all drawings are redrawn according to the new coordinates.

    But ATTENTION: in user-defined coordinatesystems angles may appear
    distorted. (see Screen.mode())

    Example (for a TurtleScreen instance named screen):
    >>> screen.setworldcoordinates(-10,-0.5,50,1.5)
    >>> for _ in range(36):
    ...     left(10)
    ...     forward(0.5)
    """
    if self.mode() != "world":
        self.mode("world")
    xspan = float(urx - llx)
    yspan = float(ury - lly)
    wx, wy = self._window_size()
    self.screensize(wx-20, wy-20)
    oldxscale, oldyscale = self.xscale, self.yscale
    self.xscale = self.canvwidth / xspan
    self.yscale = self.canvheight / yspan
    srx1 = llx * self.xscale
    sry1 = -ury * self.yscale
    srx2 = self.canvwidth + srx1
    sry2 = self.canvheight + sry1
    self._setscrollregion(srx1, sry1, srx2, sry2)
    self._rescale(self.xscale/oldxscale, self.yscale/oldyscale)
    self.update()

my book gives a weird example like this: setworldcoordinates(-10, 0.5,
1, 2), could you tell me what this operation exactly does?

These weird examples of setworldcoordinates() abound but explain little, see @TessellatingHeckler’s slide for example. They just show that you can do extreme things with the coordinates. But to answer your followup question, if we have a 100 by 100 window, this is what that particular call would do to our coordinate system:

enter image description here

Answered By: cdlane