找到两个向量和它们之间的角度
Find two vectors and angle between them
我有一辆车和一个代表行人的点,我找到了向量 a 和 b(见下图),然后我尝试计算它们之间的角度
我使用已知公式 cos(Angle) = magnitude(vector_a)*magnitude(vector_a))/dot_product(vector_a,vector_b) 但是当我计算余弦时它比 1 大得多所以不可能计算角度...我做错了什么?
def detect_pedestrian(car,pedestrian):
#Find a vector which connects pedestrian with a car
pedestrian_position = pedestrian.body.position
car_position = car.body.position
#Find vectors a and b
car_vector = Box2D.b2Body.__GetTransform(car.body).R.GetYAxis()
pedestrian_vector = pedestrian_position - car_position
#Calculate magnitudes of both vector
pedestrian_vector_magnitude = np.linalg.norm(pedestrian_vector)
car_vector_magnitude = np.linalg.norm(car_vector)
#Normalize both vectors
pedestrian_vector_normalized = pedestrian_vector/pedestrian_vector_magnitude
car_vector_normalized = car_vector/car_vector_magnitude
#Calculate angle
magnitudes = pedestrian_vector_magnitude * car_vector_magnitude
dot_product = b2Dot(pedestrian_vector_normalized, car_vector_normalized)
cosine = magnitudes/dot_product
arccos = np.arccos(cosine)
print(cosine)
非常感谢
您需要汽车的航向和行人相对于汽车的位置。如果你有行人和汽车在世界坐标中的位置(即绝对位置)和汽车的速度,代码很简单:
import numpy as np
def detect_pedestrian(car_vector, pedestrian_vector, car_velocity):
pedestrian_relative = pedestrian_vector - car_vector
pedestrian_length = np.linalg.norm(pedestrian_relative)
velocity_length = np.linalg.norm(car_velocity)
dot_product = np.dot(pedestrian_relative, car_velocity)
cosine = dot_product / (pedestrian_length * velocity_length)
return(np.arccos(cosine))
car = np.array([5, 2])
pedestrian = np.array([2, 3])
v = np.array([0, 2])
angle_rad = detect_pedestrian(car, pedestrian, v)
print(f"Pedestrian angle: {np.degrees(angle_rad)}°")
# Pedestrian angle: 71.56505117707799°
我有一辆车和一个代表行人的点,我找到了向量 a 和 b(见下图),然后我尝试计算它们之间的角度
我使用已知公式 cos(Angle) = magnitude(vector_a)*magnitude(vector_a))/dot_product(vector_a,vector_b) 但是当我计算余弦时它比 1 大得多所以不可能计算角度...我做错了什么?
def detect_pedestrian(car,pedestrian):
#Find a vector which connects pedestrian with a car
pedestrian_position = pedestrian.body.position
car_position = car.body.position
#Find vectors a and b
car_vector = Box2D.b2Body.__GetTransform(car.body).R.GetYAxis()
pedestrian_vector = pedestrian_position - car_position
#Calculate magnitudes of both vector
pedestrian_vector_magnitude = np.linalg.norm(pedestrian_vector)
car_vector_magnitude = np.linalg.norm(car_vector)
#Normalize both vectors
pedestrian_vector_normalized = pedestrian_vector/pedestrian_vector_magnitude
car_vector_normalized = car_vector/car_vector_magnitude
#Calculate angle
magnitudes = pedestrian_vector_magnitude * car_vector_magnitude
dot_product = b2Dot(pedestrian_vector_normalized, car_vector_normalized)
cosine = magnitudes/dot_product
arccos = np.arccos(cosine)
print(cosine)
非常感谢
您需要汽车的航向和行人相对于汽车的位置。如果你有行人和汽车在世界坐标中的位置(即绝对位置)和汽车的速度,代码很简单:
import numpy as np
def detect_pedestrian(car_vector, pedestrian_vector, car_velocity):
pedestrian_relative = pedestrian_vector - car_vector
pedestrian_length = np.linalg.norm(pedestrian_relative)
velocity_length = np.linalg.norm(car_velocity)
dot_product = np.dot(pedestrian_relative, car_velocity)
cosine = dot_product / (pedestrian_length * velocity_length)
return(np.arccos(cosine))
car = np.array([5, 2])
pedestrian = np.array([2, 3])
v = np.array([0, 2])
angle_rad = detect_pedestrian(car, pedestrian, v)
print(f"Pedestrian angle: {np.degrees(angle_rad)}°")
# Pedestrian angle: 71.56505117707799°