Skip to content

Instantly share code, notes, and snippets.

@baotang2118
Created February 6, 2023 11:13
Show Gist options
  • Save baotang2118/2d3bec20c6486ccfd833174e5f5962ef to your computer and use it in GitHub Desktop.
Save baotang2118/2d3bec20c6486ccfd833174e5f5962ef to your computer and use it in GitHub Desktop.
Find the intersection from given points/lines and relative position with another line
"""
Given a plane Oxy and 2 points, find the line
Y = aX + b
a = (y2 - y1) / (x2 - x1)
b = y1 - a*x1
Given a plane Oxy and 2 lines, find the intersection
Y = aX + b
X = (b2 - b1) / (a1 - a2)
X = (Y - b)/a
Y = a1*X + b1
"""
import random
import time
import matplotlib.pyplot as plt
def find_line_formula(x, y):
a = (y[1] - y[0]) / (x[1] - x[0])
b = y[0] - a * x[0]
return a, b
def find_intersection(line_1_a, line_1_b, line_2_a, line_2_b):
x = (line_2_b - line_1_b) / (line_1_a - line_2_a)
y = line_1_a * x + line_1_b
return x, y
def main_test():
x1, y1 = [random.randint(1, 10), random.randint(1, 10)], [
random.randint(1, 10),
random.randint(1, 10),
]
x2, y2 = [random.randint(1, 10), random.randint(1, 10)], [
random.randint(1, 10),
random.randint(1, 10),
]
# x1, y1 = [1, 3], [8, 8]
# x2, y2 = [6, 6], [7, 10]
intersect_point_x, intersect_point_y = None, None
line_1_a, line_1_b, line_2_a, line_2_b = None, None, None, None
print("line 1", x1, y1)
print("line 2", x2, y2)
if x1[0] == x1[1] and y1[0] == y1[1]:
print("Point overlap")
return
elif x1[0] == x1[1]:
intersect_point_x = x1[0]
elif y1[0] == y1[1]:
intersect_point_y = y1[0]
else:
line_1_a, line_1_b = find_line_formula(x1, y1)
print("line 1 formula", line_1_a, line_1_b)
if x2[0] == x2[1] and y2[0] == y2[1]:
print("Point overlap")
return
elif x2[0] == x2[1]:
intersect_point_x = x2[0]
elif y2[0] == y2[1]:
intersect_point_y = y2[0]
else:
line_2_a, line_2_b = find_line_formula(x2, y2)
print("line 2 formula", line_2_a, line_2_b)
if line_1_a != line_2_a:
if line_1_a == None and line_2_a != None:
if intersect_point_x == None and intersect_point_y != None:
intersect_point_x = (intersect_point_y - line_2_b) / line_2_a
elif intersect_point_x != None and intersect_point_y == None:
intersect_point_y = line_2_a * intersect_point_x + line_2_b
elif line_1_a != None and line_2_a == None:
if intersect_point_x == None and intersect_point_y != None:
intersect_point_x = (intersect_point_y - line_1_b) / line_1_a
elif intersect_point_x != None and intersect_point_y == None:
intersect_point_y = line_1_a * intersect_point_x + line_1_b
if line_1_a is not None and line_2_a is not None:
intersect_point_x, intersect_point_y = find_intersection(
line_1_a, line_1_b, line_2_a, line_2_b
)
print("intersect point", intersect_point_x, intersect_point_y)
if intersect_point_x != None and intersect_point_y != None:
print("intersect point", intersect_point_x, intersect_point_y)
else:
print("No intersect point")
return
"""
Assume that a new line and Ox create an angle smaller than 90 degree,
check if the intersection point is above or below this line.
"""
x3, y3 = [1, 5], [2, 6]
line_3_a, line_3_b = find_line_formula(x3, y3)
if intersect_point_y > line_3_a*intersect_point_x + line_3_b:
print("intersect point is above a new line")
elif intersect_point_y < line_3_a*intersect_point_x + line_3_b:
print("intersect point is below a new line")
else:
print("intersect point is in a new line")
plt.plot(x1, y1, x2, y2, [intersect_point_x], [intersect_point_y], x3, y3, marker="o")
plt.show()
for i in range(100):
main_test()
time.sleep(0.5)
print(i)
# main_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment