Dot product and Cross product

1 minute read

Published:

  1. Cross product



Cross product have a lot of application in engineering purpose, such as check line intersections and check point in polygon
Cross product should be included 3-axis, so we need to expand 2-axis point to 3-axis point



If res > 0, it means AC located in AB anticlockwise direction
If res = 0, it means AC is parallel with AB
If res < 0, it means AC located in AB clockwise direction

  1. Cross Product Python implementation
import numpy as np
a = np.array([0,1])
b = np.array([-1,1])
res = (a[0])*(b[1]) - (a[1])*(b[0])
print(res)
print(np.cross(a,b))


  1. Dot product

Dot product can measure the angle between two vector



If res > 0, it means the angle is 0<θ<90
If res = 0, it means the angle is θ=90, which means two vector is perpendicular
If res < 0, it means the angle is 90<θ<180

  1. Dot Product Python implementation
import numpy as np
a = np.array([4,6])
b = np.array([-3,7])
res = (a[0])*(b[0]) + (a[1])*(b[1])
print(res)
print(np.dot(a,b))


  1. Rotation Matrix Python implementation
import numpy as np
import math
p = np.array([1,0])
rotation_angle = 90 # refers to angle between X axis and the line (clockwise is negative) (anticlockwise is positive)
new_x = p[0]*math.cos(math.radians(rotation_angle)) - p[1]*math.sin(math.radians(rotation_angle))
new_y = p[0]*math.sin(math.radians(rotation_angle)) + p[1]*math.cos(math.radians(rotation_angle))
new_p = np.array([new_x,new_y])
print(new_p)