您可以沿着北/南或东/西轴移动,因为您的旋转限制在90度。
您可以观察到任何运动都有一个北/南组件和一个东/西组件。
如果你的动作一致,那么你离下一步只有90度转弯:
1. turn east or west
2. move east or west
3. turn north or south
4. move north or south
5. You should be at your target
6. turn east or west
7. move east or west
8. turn north or south
9. move north or south
10. you should be at your (next) target...等
如果我们假设您的机器人朝北开始,那么您的环路应先转向东/西,然后移动,然后转向北/南。
这是一个开始。这可能是您的全局数据和主要代码。
Robot_pos = coordinates[0]
Robot_facing = NORTH
for next_pos in coordinates[1:]:
move_robot(next_pos)如果我们假设x是东/西,y是北/南,那么你对move_robot有类似的东西:
def move_robot(new_pos):
"""
Move robot from ``Robot_pos`` to ``new_pos`` given. The robot
is assumed to be facing either north or south upon entry, so
east/west movement is done first.
"""
delta_x = ...
turn_robot(EAST or WEST)
forward( some amount )
# similarly for Y你必须在turn_robot()代码中有点聪明才能优化转向,无论你是开始面向正方向还是负方向。但它应始终是一个90度的旋转。