Skip to content

Instantly share code, notes, and snippets.

@kerenskybr
Last active November 22, 2022 19:53
Show Gist options
  • Save kerenskybr/78bbe250d8096d3d1f1953b7806c89a2 to your computer and use it in GitHub Desktop.
Save kerenskybr/78bbe250d8096d3d1f1953b7806c89a2 to your computer and use it in GitHub Desktop.
Function to find intersection between two lines
def intersection(horizontal, vertical):
# https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
# Horizontal x y x y
# Vertical x y x y
# x1, y1, x2, y2, x3, y3, x4, y4
# 0 1 2 3 0 1 2 3
result = []
x1, x2, x3, x4, y1, y2, y3, y4 = int(horizontal[0]), int(horizontal[2]), int(vertical[0]), int(vertical[2]), int(horizontal[1]), int(horizontal[3]), int(vertical[1]), int(vertical[3])
xl1 = x1 - x2
xl2 = x3 - x4
yl1 = y1 - y2
yl2 = y3 - y4
c = xl1 * yl2 - yl1 * xl2
# Finding the intersect
a = x1 * y2 - y1 * x2
b = x3 * y4 - y3 * x4
x = (a * xl2 - b * xl1) / c
y = (a * yl2 - b * yl1) / c
result.append(x)
result.append(y)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment