题解 | 两直线交点
两直线交点
https://www.nowcoder.com/practice/b14130882e704fd6aa57f8762ae17bac
import sys
class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
class Line:
def __init__(self, point_a:Point, point_b:Point):
self.point_A = point_a
self.point_B = point_b
class Coefficient:
def __init__(self,A:float,B:float,C:float) -> None:
self.A = A
self.B = B
self.C = C
def ca_line(point_A:Point, point_B:Point):
A = 0.0
B = 1.0
C = 0.0
if point_A.x == point_B.x and point_A.y == point_B.y:
return False
if point_A.x == point_B.x:
A = 1.0
B = 0.0
C = -point_A.x
return Coefficient(A,B,C)
if point_A.y == point_B.y:
A = 0.0
B = 1.0
C = -point_A.y
return Coefficient(A,B,C)
# A*x + B*y + C = 0
m = (point_A.y - point_B.y) / (point_A.x - point_B.x)
A = -m
B = 1
C = m*point_A.x -point_A.y
return Coefficient(A,B,C)
def find_meeting_point(coef_1:Coefficient,coef_2:Coefficient):
# TODO: 在这里输入你的代码,求直线 line_A 与 line_B 的交点
DD = coef_1.A*coef_2.B - coef_1.B*coef_2.A
if DD == 0:
return (-1,-1)
x = (coef_1.B*coef_2.C - coef_2.B*coef_1.C) / DD
y = (coef_2.A*coef_1.C - coef_1.A*coef_2.C) / DD
return (x,y)
if __name__ == "__main__":
n = 1
A_x = 0
A_y = 0
B_x = 0
B_y = 0
C_x = 0
C_y = 0
D_x = 0
D_y = 0
for line in sys.stdin:
a = line.split()
if n == 1:
A_x,A_y,B_x,B_y = int(a[0]),int(a[1]),int(a[2]),int(a[3])
n += 1
continue
if n == 2:
C_x,C_y,D_x,D_y = int(a[0]),int(a[1]),int(a[2]),int(a[3])
n += 1
continue
point_A = Point(A_x,A_y)
point_B = Point(B_x,B_y)
point_C = Point(C_x,C_y)
point_D = Point(D_x,D_y)
coef1 = ca_line(point_A,point_B)
coef2 = ca_line(point_C,point_D)
res = find_meeting_point(coef1,coef2)
print(res[0],end=' ')
print(res[1])