-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.py
30 lines (24 loc) · 967 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
import math
from point import Point
class Line(object):
def __init__(self, p1_in, p2_in):
self.P1 = p1_in
self.P2 = p2_in
self.Length = 0.0
self.line_length()
self.UnitVector = Point(0, 0)
self.unit_vector()
self.PerpendicularUnitVector = Point(0, 0)
self.perpendicular_unit_vector()
def line_length(self):
dx = self.P1.x - self.P2.x
dy = self.P1.y - self.P2.y
self.Length = math.sqrt(dx * dx + dy * dy)
def unit_vector(self):
x = (self.P2.x - self.P1.x) / self.Length
y = (self.P2.y - self.P1.y) / self.Length
self.UnitVector = Point(x, y)
def perpendicular_unit_vector(self):
x = self.UnitVector.x * math.cos(math.pi / 2) - self.UnitVector.y * math.sin(math.pi / 2)
y = self.UnitVector.x * math.sin(math.pi / 2) + self.UnitVector.y * math.cos(math.pi / 2)
self.PerpendicularUnitVector = Point(x, y)