-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo2d.py
236 lines (183 loc) · 6.86 KB
/
geo2d.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: pierre
#
# Created: 26/05/2018
# Copyright: (c) pierre 2018
# Licence: <your licence>
# -------------------------------------------------------------------------------
from math import *
from multipledispatch import dispatch
from numbers import Real
from typing import List, Tuple, Union
class Vecteur:
@dispatch(Real, Real, str)
def __init__(self, x: Real, y: Real, nom: str):
self.x = x
self.y = y
self.nom = nom
@dispatch(Real, Real)
def __init__(self, x: Real, y: Real):
self.__init__(x, y, "")
@dispatch((list, tuple))
def __init__(self, couple: List[Real]):
self.__init__(couple[0], couple[1], "")
@dispatch((list, tuple), str)
def __init__(self, couple: Union[List[Real], Tuple[Real]], nom: str):
self.__init__(couple[0], couple[1], nom)
@dispatch(object)
def __init__(self, vecteur: 'Vecteur'):
self.__init__(vecteur.x, vecteur.y)
@dispatch(object, object)
def __init__(self, p1: 'Point', p2: 'Point'):
x = p2.x - p1.x
y = p2.y - p1.y
self.__init__(x, y, "")
@dispatch(object, object, str)
def __init__(self, p1: 'Point', p2: 'Point', nom: str):
x = p2.x - p1.x
y = p2.y - p1.y
self.__init__(x, y, nom)
def __str__(self) -> str:
if self.nom == "":
return "({},{})".format(self.x, self.y)
else:
return "{}=({},{})".format(self.nom, self.x, self.y)
def produit_scalaire(self, vecteur: 'Vecteur') -> Real:
""" Retourne le produit scalaire """
return self.x * vecteur.x + self.y * vecteur.y
def colineaire(self, vecteur: 'Vecteur') -> bool:
""" Retourne True si les deux vecteurs sont colinéaires """
return self.x * vecteur.y == self.y * vecteur.x
def ortho(self, vecteur: 'Vecteur') -> bool:
""" Retourne True si les deux vecteurs sont orthogonaux """
return self.produit_scalaire(vecteur) == 0
def norme(self) -> float:
return sqrt(self.x ** 2 + self.y ** 2)
def __mul__(self, vecteur: 'Vecteur') -> Real:
return self.produit_scalaire(vecteur)
def angle(self, vecteur: 'Vecteur') -> float:
pass
class Point:
@dispatch(Real, Real, str)
def __init__(self, x: Real, y: Real, nom: str):
self.x = x
self.y = y
self.nom = nom
@dispatch(Real, Real)
def __init__(self, x: Real, y: Real):
self.__init__(x, y, "")
@dispatch((list, tuple))
def __init__(self, couple: List[Real]):
self.__init__(couple[0], couple[1], "")
@dispatch(object)
def __init__(self, point: 'Point'):
self.__init__(point.x, point.y, "")
def __str__(self):
if self.nom == "":
return "({},{})".format(self.x, self.y)
else:
return "{}=({},{})".format(self.nom, self.x, self.y)
def dx(self, point: 'Point'):
return point.x - self.x
def dy(self, point: 'Point'):
return point.y - self.y
def distance(self, point: 'Point'):
""" Retourne la distance entre deux points """
return sqrt(self.dx(point) ** 2 + self.dy(point) ** 2)
def translation(self, vecteur: Vecteur):
""" Retourne un point issue de la translation par un vecteur """
return Point(self.x + vecteur.x, self.y + vecteur.y)
def alignes(self, point1: 'Point', point2: 'Point'):
point0 = Point(self)
v1 = Vecteur(point0, point1)
v2 = Vecteur(point1, point2)
return v1.colineaire(v2)
class Droite:
@dispatch(Real, Real)
def __init__(self, a: Real, b: Real):
self.a = a
self.b = b
@dispatch(Point, Point)
def __init__(self, p1: Point, p2: Point):
self.a = (p2.y - p1.y) / (p2.x - p1.x)
self.b = p1.y - self.a * p1.x
@dispatch(Vecteur, Point)
def __init__(self, v: Vecteur, p: Point):
self.a = v.y / v.x
self.b = p.y - self.a * p.x
def __str__(self) -> str:
return f"y = {self.a}x {self.b:+}"
def intersection(self, droite: 'Droite') -> Union[Point, None]:
"""retourne le point d'intersection entre deux droites sécantes"""
if self.a != droite.a:
x: Real = (self.b - droite.b) / (droite.a - self.a)
y: Real = self.a * x + self.b
return Point(x, y)
return None
def parallele(self, p: Point) -> 'Droite':
"""retourne la droite parallele à self passant par p"""
return Droite(self.a, p.y - self.a * p.x)
def perpendiculaire(self, p: Point) -> 'Droite':
"""retourne la droite perpendiculaire à self passant par p"""
return Droite(-1 / self.a, p.y + p.x / self.a)
def appartient(self, p: Point, delta: float = 0.001) -> bool:
"""retourne true si p appartient à self à +-delta près"""
return abs(p.y - self.a * p.x - self.b) <= delta
def get_vecteur(self) -> Vecteur:
return Vecteur(1, self.a)
def hauteur(self, p: Point) -> ['Segment', None]:
"""retourne la hauteur à une doite sous la forme d'un segment si p n'appartient pas à self
et None dans le cas contraire"""
if self.appartient(p) is False:
d: Droite = self.perpendiculaire(p)
pi: Point = self.intersection(d)
return Segment(pi, p)
return None
def get_y(self, x: Real) -> Real:
return self.a * x + self.b
def get_x(self, y: Real) -> Real:
return (y - self.b) / self.a
class Segment(Droite):
def __init__(self, p1: Point, p2: Point):
super().__init__(p1, p2)
self.p1 = p1
self.p2 = p2
def intersection(self, seg: 'Segment') -> Union[Point, None]:
pi = super().intersection(seg)
if pi is not None and (self.p1.x <= pi.x <= self.p2.x or self.p1.x >= pi.x >= self.p2.x):
return pi
return None
def longueur(self):
return self.p1.distance(self.p2)
def get_xmax(self):
return max(self.p1.x, self.p2.x)
def get_ymax(self):
return max(self.p1.y, self.p2.y)
def get_xmin(self):
return min(self.p1.x, self.p2.x)
def get_ymin(self):
return min(self.p1.y, self.p2.y)
def __str__(self):
return f"{super().__str__()} {self.p1} {self.p2}"
def main():
print("debut")
a = Point(0, 0, "A")
b = Point(4, 1, "B")
c = Point(-1, 4, "C")
ca = Vecteur(c, a, "CA")
cb = Vecteur(c, b, "CB")
print(ca, cb)
print("CA.CB =", ca * cb)
print("cos(teta) = ", (ca * cb) / (ca.norme() * cb.norme()))
print(cb.norme())
s1 = Segment(Point(0, 0), Point(4, 0))
s2 = Segment(Point(-1, 3), Point(3, 0))
print(s1.intersection(s2))
d = Droite(a, b)
print(d.perpendiculaire(c))
print(d.hauteur(c).longueur())
if __name__ == '__main__':
main()