how to find angle between two vectors
xxxxxxxxxx
To find the angle between two vectors:
1. Find the dot product of the two vectors.
2. Divide this by the magnitude of the first vector.
3. Divide this by the magnitude of the second vector.
4. Take the inverse cosine of this value to obtain the angle.
xxxxxxxxxx
function angleTo(v: Vector) {
return Math.atan2(v.y - this.y, v.x - this.x)
}
xxxxxxxxxx
import math
def get_angle(vector1, vector2):
dot_product = sum(a * b for a, b in zip(vector1, vector2))
magnitude1 = math.sqrt(sum(a**2 for a in vector1))
magnitude2 = math.sqrt(sum(b**2 for b in vector2))
angle = math.degrees(math.acos(dot_product / (magnitude1 * magnitude2)))
return angle
# Example usage
vector1 = [4, 3]
vector2 = [-2, 1]
angle = get_angle(vector1, vector2)
print(angle) # Output: 135.0 degrees