【发布时间】:2014-03-28 21:50:34
【问题描述】:
我从在线课程中完成了一个 pset,我们创建了随机机器人,它们在网格清洁瓷砖周围随机移动。 我想创建一个机器人来依次清理每个图块,并且在我将速度设置为 1.0 时实现了这一目标。
但是,当我将速度提高 1 时,机器人会移动两个位置而不是一个位置,从而增加与每次增加直接相关的移动。
这是计算新位置的类:
class Pos(object):
"""
A Position represents a location in a two-dimensional room.
"""
def __init__(self, x, y):
"""
Initializes a position with coordinates (x, y).
"""
self.x = x
self.y = y
def getX(self):
return self.x
def getY(self):
return self.y
def getNewPos(self, angle, speed):
"""
Computes and returns the new Position after a single clock-tick has
passed, with this object as the current position, and with the
specified angle and speed.
Does NOT test whether the returned position fits inside the room.
angle: number representing angle in degrees, 0 <= angle < 360
speed: positive float representing speed
Returns: a Po sobject representing the new position.
"""
old_x, old_y = self.getX(), self.getY()
angle = float(angle)
# Compute the change in position
delta_y = speed * math.cos(math.radians(angle))
delta_x = speed * math.sin(math.radians(angle))
# Add that to the existing position
new_x = old_x + delta_x
new_y = old_y + delta_y
return Position(new_x, new_y)
def __str__(self):
return "(%0.2f, %0.2f)" % (self.x, self.y)
速度和机器人移动的量有什么关系,我认为增加速度会增加他移动的速度,但仍然认为已经越过了每个位置,但显然这是不正确的。
有人可以解释一下计算是如何工作的吗,我很长时间没有使用 sin、cos 等,可以改变它来实现我需要的东西还是我需要一些非常不同的东西?
【问题讨论】: