-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_.py
35 lines (22 loc) · 836 Bytes
/
line_.py
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
from decimal import Decimal, getcontext
from hyperplane import Hyperplane
from vector import Vector
getcontext().prec = 30
class Line(Hyperplane):
def __init__(self, normal_vector=None, constant_term=None):
super(Line, self).__init__(normal_vector, constant_term, dimension=2)
def intersection_with(self, l):
try:
(a, b) = self.normal_vector.coordinates
(c, d) = l.normal_vector.coordinates
k1 = self.constant_term
k2 = l.constant_term
x = (d * k1) - (b * k2)
y = (a * k2) - (c * k1)
denominator = (a * d) - (b * c)
return Vector([x, y]).divided_by_scalar(denominator)
except ZeroDivisionError:
if self == l:
return self
else:
return None