Skip to content

Instantly share code, notes, and snippets.

@mcdlee
Created June 10, 2022 00:53
Show Gist options
  • Save mcdlee/9e25ac7714493502bad24be70548564f to your computer and use it in GitHub Desktop.
Save mcdlee/9e25ac7714493502bad24be70548564f to your computer and use it in GitHub Desktop.
def lines2intersectionTuple(L1, L2):
# intersection of xcos(theta)+ysin(theta)=D, input as (theta, D) tuple
# the unit of theta is degree
theta1 = np.deg2rad(L1[0])
theta2 = np.deg2rad(L2[0])
D1 = L1[1]
D2 = L2[1]
y = np.round((D1/np.cos(theta1) - D2/np.cos(theta2))/(np.tan(theta1)-np.tan(theta2)))
x = np.round((D1/np.cos(theta1) - y*np.tan(theta1)))
return(x,y)
def points2line(P1, P2): #P1及P2為x,y座標,產生 xcos(theta)+ysin(theta)=D,theta 單位是 degree (範圍 0-180)
x1 = P1[0]
y1 = P1[1]
x2 = P2[0]
y2 = P2[1]
if x1 == x2:
theta = 0
elif y1 == y2:
theta = 90
else:
theta = np.rad2deg(np.arctan((x1-x2)/(y2-y1)))
if theta <0:
theta = theta +180
D = x1*np.cos(np.deg2rad(theta)) + y1*np.sin(np.deg2rad(theta))
return theta, D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment