Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python setworldcoordinates清除屏幕上的任何海龟图形_Python_Turtle Graphics - Fatal编程技术网

Python setworldcoordinates清除屏幕上的任何海龟图形

Python setworldcoordinates清除屏幕上的任何海龟图形,python,turtle-graphics,Python,Turtle Graphics,我要做的是创建一只乌龟做一个正弦函数,但是实际的乌龟看起来好像它仍然在原地,因为每次乌龟移动时,世界坐标都会随之轻微移动。想象一下一条蛇在电视上滑行,但它的头仍在屏幕中央,相机也跟着它移动。每当我移动乌龟后试图设置世界坐标时,乌龟画的所有东西都会消失。我意识到这可能不是应对挑战的最佳解决方案,但我想不出比这更好的解决方案。问题 正如您所知,海龟默认从坐标(0,0)绘制,而您的世界默认呈现在矩形(-200,-150,200,150)中,大小为400 x 300 您正在从默认的主位置(0+偏移量,0

我要做的是创建一只乌龟做一个正弦函数,但是实际的乌龟看起来好像它仍然在原地,因为每次乌龟移动时,世界坐标都会随之轻微移动。想象一下一条蛇在电视上滑行,但它的头仍在屏幕中央,相机也跟着它移动。每当我移动乌龟后试图设置世界坐标时,乌龟画的所有东西都会消失。我意识到这可能不是应对挑战的最佳解决方案,但我想不出比这更好的解决方案。

问题 正如您所知,海龟默认从坐标
(0,0)
绘制,而您的世界默认呈现在矩形
(-200,-150,200,150)
中,大小为
400 x 300

您正在从默认的主位置
(0+偏移量,0)
绘制,并且您的屏幕呈现为
(0+偏移,-3180+偏移,3)

因此,最后一个点总是在左边框的中间绘制,而之前绘制的所有点都被保留在后面(双关语)。这就是为什么你看不到(漂亮的)图表

解决方案 您希望最后一点(圆)始终绘制在中心

你想要的是这样的东西:

import turtle
import math
turtleSize=1
turtle.speed(1)

turtleA=turtle.Turtle()
turtleA.shape("circle")
turtleA.shapesize(turtleSize)

screenSizeX=180
screenSizeY=3
llx=0
turtle.setworldcoordinates(llx,-screenSizeY,screenSizeX,screenSizeY)

for i in range(180):
    turtleA.goto(i,(2*math.sin(0.1*i)))
    screenSizeX=screenSizeX+1
    llx=llx+1
    turtle.setworldcoordinates(llx,-screenSizeY,screenSizeX,screenSizeY)
其中,
点位置X
点位置Y
是最后绘制点的坐标

让我给你一些东西作为基础:

turtle.setworldcoordinates(
    -screenWidth / 2 + point_positionX,
    -screenHeight / 2 + point_positionY,
    screenWidth / 2 + point_positionX,
    screenHeight / 2 + point_positionY
)
你只需要计算y偏移量。祝你好运

每当我移动后尝试设置世界坐标时 乌龟,乌龟画的一切都消失了

第一次调用
setworldcoordinates()
可能会破坏当前的海龟状态,因此我始终建议您在程序中尽早调用它。例如:

'''
Graph Simulation (Limited to sine waves with amplitude 0.5 and frequency pi/180)
'''

import turtle       # For drawing graph
import math         # For calculating positions

# Initialise turtle
turtleSize = 1
turtle.speed(1)

# Initialise the sketcher
graphSketcher = turtle.Turtle()
graphSketcher.shape("circle")
graphSketcher.shapesize(turtleSize)

# Some constants
SCREEN_WIDTH = 180
SCREEN_HEIGHT = 3
AMPLITUDE = 0.5
WAVES_IN_SCREEN = 10
ANGULAR_FREQUENCY = math.pi/180

# Set the correct scale
turtle.setworldcoordinates(-SCREEN_WIDTH/2, -SCREEN_HEIGHT/2, SCREEN_WIDTH/2, SCREEN_HEIGHT/2)

for time in range(181):
    # Draw the graph
    graphSketcher.goto(
        time,
        AMPLITUDE * math.sin(2 * ANGULAR_FREQUENCY * time * WAVES_IN_SCREEN)
    )
    # move forward in time and space
    # Try commenting this to see the difference!
    turtle.setworldcoordinates(
        -SCREEN_WIDTH / 2 + time,
        -SCREEN_HEIGHT / 2,
        SCREEN_WIDTH / 2 + time,
        SCREEN_HEIGHT / 2
    )
调用
setworldcoordinates()
将坐标系向上向下翻转也会移除图章,并重置海龟的大小和方向。但是,如果取消对
turtle.mode('world')
调用的注释,它将阻止这种情况发生

后续调用
setworldcoordinates()
并不像@jaidepshekhar对您的问题的出色(+1)回答那样具有破坏性。我使用了Jaideep的示例代码,得出了我认为与您描述的更接近的结果:

import turtle

turtle.setup(300, 300)
# turtle.mode('world')
turtle.shape('turtle')
turtle.shapesize(5)  # make a large cursor
turtle.setheading(90)  # turtle facing upward
turtle.stamp()  # leave a lasting impression

turtle.setworldcoordinates(-150, 150, 150, -150)  # flip coordinates upsidedown

turtle.exitonclick()
请注意,交换
setposition()
setworldcoordinates()
调用的顺序将产生不良结果,尽管没有明显的理由不这样做。当振幅改变符号时,这个重做在光标上有一个轻微的抖动——它不是理想的静止状态

import turtle
import math

WIDTH = 300
HEIGHT = 1.0
AMPLITUDE = 0.25
WAVES_IN_SCREEN = 10
ANGULAR_FREQUENCY = math.pi / WIDTH

turtle.setworldcoordinates(-WIDTH/2, -HEIGHT/2, WIDTH/2, HEIGHT/2)
turtle.tracer(False)

for time in range(720):
    extent = AMPLITUDE * math.sin(2 * ANGULAR_FREQUENCY * time * WAVES_IN_SCREEN)
    turtle.setposition(time, extent)
    turtle.setworldcoordinates(-WIDTH/2 + time, extent - HEIGHT/2, WIDTH/2 + time, extent + HEIGHT/2)
    turtle.update()

turtle.tracer(True)
turtle.exitonclick()