-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_2d_my.py
100 lines (90 loc) · 3.17 KB
/
line_2d_my.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import numpy as np
# from point_2d_my import Point
import matplotlib.pyplot as plt
# This class is operating a line function in 2 dimension with the line function as ay + bx + c = 0
# This class manipulate several functionality. Check the point whether is on this line or not. Find
# a orthogonal line that cross a point in space. check the interception between two liens.
class Line():
def __init__(self, a, b, c):
# line function type: ay + bx + c = 0
self.a = float(a)
self.b = float(b)
self.c = float(c)
# Parameter:
# A point object
# Return:
# True if the point is on this line
# False if the point is not on this line
def check_point_on_line(self, point_to_go):
if self.a == 0:
if -self.c == point_to_go.x:
return True
else:
return False
elif self.b == 0:
if -self.c == point_to_go.y:
return True
else:
return False
else:
y = - self.b/self.a*point_to_go.x - self.c/self.a
if y == point_to_go.y:
return True
else:
return False
# Parameter:
# A point object
# Return:
# A line object that is orthogonal to this line and cross the point
def orthogonal_line_cross_point(self, point_to_go):
# intercept = - self.c / self.a
if self.a == 0:
orthogonal_a = 1
orthogonal_b = 0
orthogonal_c = - point_to_go.y
elif self.b == 0:
orthogonal_a = 0
orthogonal_b = 1
orthogonal_c = - point_to_go.x
else:
# ay + bx + c = 0
# y = -bx/a - c/a
# y = ax/b + h
# h = y0 - ax0/b
# y = ax/b + y0 - ax0/b
# y - ax/b - y0 + ax0/b = 0
orthogonal_a = 1
orthogonal_b = - self.a / self.b
orthogonal_c = - point_to_go.y + (self.a * point_to_go.x / self.b)
orthogonal_line = Line(orthogonal_a, orthogonal_b, orthogonal_c)
return orthogonal_line
# Parameter:
# Another line object
# Return:
# A point object that store the value of the interception between the other line and this line
def interception_between_two_lines(self, other_line):
a1 = other_line.a
b1 = other_line.b
c1 = other_line.c
left = np.array([[self.b, self.a], [b1, a1]])
right = np.array([[- self.c], [- c1]])
solution = np.linalg.solve(left, right)
interception_point = [solution[0], solution[1]]
return interception_point
def show_line_function(self):
line_function = repr(self.a) + '*y + ' + repr(self.b) + '*x + ' + repr(self.c) + '= 0'
print(line_function)
def plot_line(self):
x = []
y = []
if self.a == 0:
x = [-self.c, -self.c]
y = [-10, 10]
elif self.b == 0:
x = [-10, 10]
y = [-self.c, -self.c]
else:
for n in list(range(-10, 10)):
x.append(n)
y.append(-self.b / self.a * n - self.c / self.a)
plt.plot(x, y, 'r-')