Python turtle.setWorldCoordinations函数的作用是什么?

Python turtle.setWorldCoordinations函数的作用是什么?,python,turtle-graphics,Python,Turtle Graphics,Python初学者的书倾向于向中间页倾斜,几乎没有任何解释。因此,我很难弄清楚turtle.setWorldCoordinations函数的作用,因为我无法通过阅读毫无帮助的文档来实现这一点,并且无法根据经验推断出文档中的一些想法。有人能指出这个函数在turtle graphics中的作用吗?因为turtle.py是Python附带的,并且是用Python编写的,所以您可以查看源代码来了解这个函数的作用,我发现我在turtle.py中做了很多工作,见下文 我相信setworldcoordinat

Python初学者的书倾向于向中间页倾斜,几乎没有任何解释。因此,我很难弄清楚turtle.setWorldCoordinations函数的作用,因为我无法通过阅读毫无帮助的文档来实现这一点,并且无法根据经验推断出文档中的一些想法。有人能指出这个函数在turtle graphics中的作用吗?

因为turtle.py是Python附带的,并且是用Python编写的,所以您可以查看源代码来了解这个函数的作用,我发现我在turtle.py中做了很多工作,见下文

我相信setworldcoordinates允许您选择一个更方便您解决问题的坐标系。例如,假设您不希望0,0位于屏幕中央。您可以使用setworldcoordinates将其移动到一个角点,而不是合并偏移,如果这样对您更合适的话。您还可以设置坐标系,该坐标系在水平方向与垂直方向上具有不同的比例因子

例如,请参见我在其中定义的例程,该例程使用setworldcoordinates缩放绘制的任何对象,而无需将任何缩放因子合并到自己的绘图代码中

或者,可以将角点位于0,0,1,0,0,1和1,1的坐标系设置为完全在单位正方形内工作

棘手的一点是,它将坐标系映射到现有的窗口形状上——因此,您必须根据窗口调整坐标或重塑窗口以匹配您的坐标系。否则,您可能会发现自己的纵横比不理想

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()
我的书给出了一个奇怪的例子:setWorldCoordinations-10,0.5, 你能告诉我这个手术到底是做什么的吗

这些奇怪的SetWorldCoordination例子比比皆是,但解释很少,例如见@TessellatingHeckler的幻灯片。它们只是表明你们可以用坐标做极端的事情。但是为了回答你接下来的问题,如果我们有一个100乘100的窗口,这就是这个特殊的调用对坐标系的作用:


请看星星如何在中扭曲的图片。我还是一个真正的编程新手。我理解代码之前的部分,但我的书给出了一个奇怪的例子:setworldcoordinates-10,0.5,1,2,你能告诉我这个操作到底是做什么的吗?@lind,我添加了一张图片来显示调用setworldcoordinates-10,0.5,1,2会影响海龟屏幕的坐标系。很抱歉,我太执着了,这一定会让人沮丧,但指导坐标输入和输出的数学关系是什么;因为我不能完全阅读上面的代码:-自我模式是从哪里来的。我的意思是,它既没有定义也没有导入。它似乎不是内置的。我从海龟身上进口了所有东西,但仍然没有任何迹象。请你澄清一下,你能至少告诉我self.mode是从哪里来的或者它是做什么的吗?