@mtf/

line equations

Python

No description

fork
loading
Files
  • main.py
main.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
101
102
103
104
105
106
107
108
109
110
111
112
from fractions import Fraction

#def point(a, b):
#    return a, b

def point(a, b):
  def f():
    return a, b
  return f

def distance(a,b):
    #x1, y1 = a
    #x2, y2 = b
    x1, y1 = a()
    x2, y2 = b()
    return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

def slope(a, b):
    #x1, y1 = a
    #x2, y2 = b
    x1, y1 = a()
    x2, y2 = b()
    return (y2 - y1) / (x2 - x1)

def rational(a, b):
    x1, y1 = a()
    x2, y2 = b()
    return y2 - y1, x2 - x1

def yintercept(a, m):
    #x, y = a
    x, y = a()
    return y - m * x

def xintercept(m, b):
    return -b / m

P = point(3, 5)
Q = point(8, 12)

d = distance(P, Q)
m = slope(P, Q)
b = yintercept(P, m)
r = xintercept(m, b)

#print (f"A: {A}\nB: {B}\nd: {round(d, 4)}\nm: {round(m, 4)}\nb: {round(b, 4)}\nr: {round(r, 4)}")
print (f"P: {P()}\nQ: {Q()}\nd: {round(d, 4)}\nm: {round(m, 4)}\nb: {round(b, 4)}\nr: {round(r, 4)}")
'''
dy, dx = rational(P, Q)
print (dy, dx)
print (dy / dx)
'''
def line(a, b):
  x1, y1 = a()
  x2, y2 = b()  
  dx = (x2 - x1)
  dy = (y2 - y1)
  m = dy / dx
  b = y1 - m * x1
  return dy, -dx, -int(dx * b)  # ????

print ('='*25)
P = point(3, 5)
Q = point(8, 12)
a1, b1, c1 = line(P, Q)
if a1 < 0:
  a1, b1, c1 = -a1, -b1, -c1
#b1 = -b1
print (f"{a1}x + {b1}y + {c1}")

R = point(5, 13)
S = point(1, 8)
a2, b2, c2 = line(R, S)
if a2 < 0:
  a2, b2, c2 = -a2, -b2, -c2
#b2 = -b2
print (f"{a2}x + {b2}y + {c2}")
#print (f"{a1 - a2}x + {b1 - b2}y + {c1 - c2}") # missing 0 term

l1 = a1 * b2
m1 = b1 * b2
n1 = c1 * b2

print (f"{l1}x + {m1}y + {n1}")

l2 = a2 * b1
m2 = b2 * b1
n2 = c2 * b1

print (f"{l2}x + {m2}y + {n2}")

print (f"{l1 - l2}x + {m1 - m2}y + {n1 - n2}")
x = - (n1 - n2) / (l1 - l2)
print (f"x = {x}")
print (f"x = {Fraction(-(n1 - n2), (l1 - l2))}")

l1 = a1 * a2
m1 = b1 * a2
n1 = c1 * a2

print (f"{l1}x + {m1}y + {n1}")

l2 = a2 * a1
m2 = b2 * a1
n2 = c2 * a1

print (f"{l2}x + {m2}y + {n2}")

print (f"{l1 - l2}x + {m1 - m2}y + {n1 - n2}")
y = - (n1 - n2) / (m1 - m2)
print (f"y = {y}")
print (f"y = {Fraction(-(n1 - n2), (m1 - m2))}")