我们如何借助行进距离和运动方向获得坐标?

问题描述 投票:0回答:1

假设我有一个数组

sensor_data=[10,0,5,1,10,1,20,1,20,1,15]

现在在此:

0表示机器人/无人机向右转,

1表示机器人/无人机左转。

剩余的数字是行进的距离。

因此,根据上述阵列,机器人/无人机首先行进10厘米的距离。然后它右转。向右转后,机器人/无人机行驶5厘米,然后向左行驶。左转后行驶10厘米,依此类推。因此,最初,机器人/无人机在(0,0)。然后它直线移动,即沿y方向。因此,坐标将是(0,10)。然后,在右转后,经过5 cm后,坐标将为(-5,10)。按照这种模式,其余坐标为:(-5,20),(15,20)和(15,0)。可以编写什么代码,以便可以从上述给定数组生成这些坐标。请帮忙!

python arrays list coordinates coordinate-systems
1个回答
0
投票
import numpy as np from numpy import cos,sin,pi import matplotlib.pyplot as plt # Convert data into floats, for sensor_data = tuple(map(lambda x: float(x),[10,0,5,1,10,1,20,1,20,1,15])) # Start at 0,0 in a 2D plane and start out in x-Direction Starting_Position = np.array((0.,0.)) Starting_Direction = np.array((1.,0.)) def Rotation_Matrix(direction): '''Can be expanded for any angle of rotation in a 2D plane. Google rotation matrix in 2D space.''' a = {'left':pi/2,'right':-pi/2}[direction] matrix = np.array(((round(cos(a),7),round(-sin(a),7)), (round(sin(a),7),round(cos(a),7)))) return matrix def DronePosition(Number_input,Current_Position,Current_Direction): if Number_input == 1.: New_Direction = Current_Direction.dot(Rotation_Matrix('left')) New_Position = Current_Position elif Number_input == 0.: New_Direction = Current_Direction.dot(Rotation_Matrix('right')) New_Position = Current_Position else: New_Position = Current_Position + Current_Direction*Number_input New_Direction = Current_Direction return New_Position,New_Direction Drone_Path = np.zeros(shape=(len(sensor_data),2)) for step in range(len(sensor_data)): Drone_Path[step,0] = Starting_Position[0] Drone_Path[step,1] = Starting_Position[1] Starting_Position, Starting_Direction = DronePosition(sensor_data[step],Starting_Position,Starting_Direction) fig, ax = plt.subplots(figsize=(6,6)) ax.plot(Drone_Path[:,0],Drone_Path[:,1]) plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.