-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinter.py
More file actions
36 lines (33 loc) · 798 Bytes
/
inter.py
File metadata and controls
36 lines (33 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def printPoint(self):
print(self.x, self.y)
def get_intersection (A, B, C, D): #start end start end
ax = A.x
ay = A.y
bx = B.x
by = B.y
cx = C.x
cy = C.y
dx = D.x
dy = D.y
d = (ax-bx)*(cy-dy)-(ay-by)*(cx-dx)
if d == 0:
return None #they are parallel
a = ax*by-ay*bx
b = cx*dy-cy*dx
x1 = (a*(cx-dx))
x2 = (b*(ax-bx))
x = x1/d - x2/d
y = (a*(cy-dy) - b*(ay-by))/d
if (x <= max(ax, bx) and x >= min(ax, bx)) and (x <= max(cx, dx) and x >= min(cx, dx)): #between start and end of both lines
print(x)
print(y)
return x,y
A = Point(1,4)
B = Point(5,8)
C = Point(4,2)
D = Point(4,8)
get_intersection(A,B,C,D)