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)
我试图做的是创建一个制作正弦函数的 turtle ,但是实际的 turtle 看起来好像它保持在原地,因为每次 turtle 移动时,世界坐标都会随着它轻微移动。想象一条蛇在电视上滑行,但它的头仍然在屏幕中央,而摄像机只是跟着它移动。每当我在移动 turtle 后尝试设置世界坐标时, turtle 绘制的所有内容都会消失。我意识到这可能不是应对挑战的最佳解决方案,但我想不出更好的解决方案。
最佳答案
问题
如您所知, turtle 默认从坐标 (0,0)
绘制。 ,并且您的世界默认呈现在矩形 (-200, -150, 200, 150)
中大小为 400 x 300
.
您正在从默认的家庭位置绘图 (0 + offset,0)
,并且您的屏幕呈现为(0 + offset, -3, 180 + offset, 3)
.
因此,最后一个点总是绘制在左边框的中间,所有之前绘制的点都被留下(双关语)。这就是为什么您看不到(漂亮)图表的原因。
解决方案
您希望始终在中心绘制最后一个点(圆)。
你想要的是这样的:
turtle.setworldcoordinates(
-screenWidth / 2 + point_positionX,
-screenHeight / 2 + point_positionY,
screenWidth / 2 + point_positionX,
screenHeight / 2 + point_positionY
)
哪里
point_positionX
和 point_positionY
是最后绘制点的坐标。让我给你一些东西来建立:
'''
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
)
你只需要计算 y 偏移量。祝你好运!
关于python - setworldcoordinates 清除任何 turtle 绘图的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692255/