我得到了这个等式,以便让圆圈进入轨道。假设它应该永远运行,我创建了一个无限循环。 x = cx + r*cos(t)
和y = cy + r*sin(t)
我做错了吗?
from graphics import *
import math
def main():
win=GraphWin("Cirlce",600,600)
x=250
y=70
c=Circle(Point(x,y),18)
c.draw(win)
v=True
while v==True:
c.undraw()
x = x + c.getRadius()*math.cos(2)
y = y + c.getRadius()*math.sin(2)
c=Circle(Point(x,y),18)
c.draw(win)
main()
答案 0 :(得分:0)
问题在于:
x = x + c.getRadius()*math.cos(2)
y = y + c.getRadius()*math.sin(2)
你正在直线前进。并且,由于您的代码运行速度非常快,因此可能会很快超出范围。正确的版本是:
x0, y0 = 0, 0 # Coordinates of the centre
r = 2 # Radius
t = 0
dt = 0.01 # Or anything that looks smooth enough.
while True: # No need for an extra variable here
c.undraw() # I don't know this graphics library
# I will assume what you did is correct
x = x0 + r * math.cos(t)
y = y0 + r * math.sin(t)
c=Circle(Point(x,y),18)
c.draw(win)
t += dt
time.sleep(0.01)
在循环结束时,我将它发送到睡眠状态一段时间,以便以有限的速度运行。某些图形库包含rate
功能,允许您以每秒固定的帧数运行它,与机器中的循环速度无关。
答案 1 :(得分:0)
c.undraw()#我不知道这个图形库,我会假设什么 你做的是正确的
当GraphicsObjects c.undraw()
时,在每次循环迭代中使用c = Circle(...)
,c.draw()
和move(dx, dy)
似乎都很浪费。然而,棘手的部分是运动是相对的,所以你必须计算你下一个位置的差异:
import math
import time
from graphics import *
WINDOW_WIDTH, WINDOW_HEIGHT = 600, 600
win = GraphWin("Circle", WINDOW_WIDTH, WINDOW_HEIGHT)
win.setCoords(-WINDOW_WIDTH / 2, -WINDOW_HEIGHT / 2, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)
ORBIT_RADIUS = 200
PLANET_RADIUS = 18
SOLAR_RADIUS = 48
x0, y0 = 0, 0 # Coordinates of the center
t = 0.0
dt = 0.01 # Or anything that looks smooth enough.
delay = 0.01
star = Circle(Point(x0, y0), SOLAR_RADIUS)
star.setFill("yellow")
star.draw(win)
orbit = Circle(Point(x0, y0), ORBIT_RADIUS)
orbit.setOutline("lightgray")
orbit.draw(win)
planet = Circle(Point(x0 + ORBIT_RADIUS * math.cos(t), y0 + ORBIT_RADIUS * math.sin(t)), PLANET_RADIUS)
planet.setFill("blue")
planet.draw(win)
while True:
x, y = x0 + ORBIT_RADIUS * math.cos(t), y0 + ORBIT_RADIUS * math.sin(t)
center = planet.getCenter()
planet.move(x - center.getX(), y - center.getY())
t = (t + dt) % (2 * math.pi)
if win.checkMouse() is not None:
break
time.sleep(delay)
win.close()
我添加了一位明星,因为很难理解某些东西在没有看到被绕轨道运行的轨道上运行:
您可以点击窗口彻底退出。