| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import re |
| 25 |
from random import uniform |
| 26 |
from math import pi, atan2, cos, sin, hypot, floor, ceil |
| 27 |
|
| 28 |
from numpy import inf |
| 29 |
|
| 30 |
from .objet import Objet, Objet_avec_equation, Argument, ArgumentNonModifiable, \ |
| 31 |
Ref, contexte, RE_NOM_DE_POINT, G |
| 32 |
from .angles import Secteur_angulaire |
| 33 |
from .points import Point, Point_generique, Centre, Point_translation, Milieu |
| 34 |
from .routines import nice_display, distance, formatage, \ |
| 35 |
vect, produit_scalaire, norme, distance_segment, \ |
| 36 |
arrondir_1_25_5, sign, distincts |
| 37 |
from .vecteurs import Vecteur_unitaire, Vecteur, Vecteur_libre, Vecteur_generique |
| 38 |
from .labels import Label_droite, Label_demidroite, Label_segment |
| 39 |
from .transformations import Translation |
| 40 |
|
| 41 |
from .. import param |
| 42 |
from ..pylib import eval_restricted, fullrange |
| 43 |
|
| 44 |
from sympy import Rational |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
class Ligne_generique(Objet_avec_equation): |
| 55 |
"""Une ligne générique. |
| 56 |
|
| 57 |
Usage interne : la classe mère pour les droites, segments, demi-droites.""" |
| 58 |
|
| 59 |
_affichage_depend_de_la_fenetre = True |
| 60 |
_marqueurs = "()" |
| 61 |
|
| 62 |
point1 = __point1 = Argument("Point_generique") |
| 63 |
point2 = __point2 = Argument("Point_generique") |
| 64 |
|
| 65 |
def __init__(self, point1, point2, **styles): |
| 66 |
self.__point1 = point1 = Ref(point1) |
| 67 |
self.__point2 = point2 = Ref(point2) |
| 68 |
Objet.__init__(self, **styles) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
def _longueur(self): |
| 73 |
return norme(self.__point2.x - self.__point1.x, self.__point2.y - self.__point1.y) |
| 74 |
|
| 75 |
|
| 76 |
def _espace_vital(self): |
| 77 |
x1, y1 = self.__point1.coordonnees |
| 78 |
x2, y2 = self.__point2.coordonnees |
| 79 |
return (min(x1, x2), max(x1, x2), min(y1, y2), max(y1, y2)) |
| 80 |
|
| 81 |
|
| 82 |
def _get_equation(self): |
| 83 |
"Retourne un triplet (a, b, c), tel que ax + by + c = 0 soit une équation de droite de la ligne." |
| 84 |
xA, yA = self.__point1.coordonnees |
| 85 |
xB, yB = self.__point2.coordonnees |
| 86 |
a, b, c = yA - yB, xB - xA, xA*yB - yA*xB |
| 87 |
while -1 < a < 1 and -1 < b < 1: |
| 88 |
a *= 10 |
| 89 |
b *= 10 |
| 90 |
c *= 10 |
| 91 |
return (a, b, c) |
| 92 |
|
| 93 |
@property |
| 94 |
def equation_reduite(self): |
| 95 |
"""Retourne (a, b) si la droite a une équation de la forme y=ax+b ; et (c,) si la droite a une équation de la forme x=c. |
| 96 |
|
| 97 |
Ceci permet de comparer facilement deux droites, ce qui n'est pas le cas avec la propriété .équation (puisqu'une droite a une infinité d'équations) : si d1._equation_reduite() ~ d2._equation_reduite, d1 et d2 sont (à peu près) confondues. |
| 98 |
""" |
| 99 |
eq = self.equation |
| 100 |
if eq is None: |
| 101 |
return |
| 102 |
a, b, c = eq |
| 103 |
if abs(b) > contexte['tolerance']: |
| 104 |
return (-a/b, -c/b) |
| 105 |
else: |
| 106 |
return (-c/a, ) |
| 107 |
|
| 108 |
def _parallele(self, ligne): |
| 109 |
"Indique si la ligne est parallèle à une autre ligne." |
| 110 |
if not isinstance(ligne, Ligne_generique): |
| 111 |
raise TypeError("L'objet doit etre une ligne.") |
| 112 |
a, b, c = self.equation |
| 113 |
a0, b0, c0 = ligne.equation |
| 114 |
return abs(a*b0 - b*a0) < contexte['tolerance'] |
| 115 |
|
| 116 |
def _perpendiculaire(self, ligne): |
| 117 |
"Indique si la ligne est perpendiculaire à une autre ligne." |
| 118 |
if not isinstance(ligne, Ligne_generique): |
| 119 |
raise TypeError("L'objet doit etre une ligne.") |
| 120 |
a, b, c = self.equation |
| 121 |
a0, b0, c0 = ligne.equation |
| 122 |
return abs(a*a0 + b*b0) < contexte['tolerance'] |
| 123 |
|
| 124 |
def __iter__(self): |
| 125 |
return iter((self.__point1, self.__point2)) |
| 126 |
|
| 127 |
|
| 128 |
def _creer_nom_latex(self): |
| 129 |
"""Crée le nom formaté en LaTeX. Ex: M1 -> $M_1$.""" |
| 130 |
Objet._creer_nom_latex(self) |
| 131 |
nom = self.nom_latex[1:-1] |
| 132 |
if re.match("(" + RE_NOM_DE_POINT + "){2}", nom): |
| 133 |
self.nom_latex = "$" + self._marqueurs[0] + nom + self._marqueurs[1] + "$" |
| 134 |
else: |
| 135 |
nom = self.latex_police_cursive(nom) |
| 136 |
self.nom_latex = "$" + nom + "$" |
| 137 |
|
| 138 |
def xy(self, x = None, y = None): |
| 139 |
"""Retourne les coordonnées du point de la droite d'abscisse ou d'ordonnée donnée. |
| 140 |
|
| 141 |
x ou y doivent être définis, mais bien sûr pas les deux. |
| 142 |
|
| 143 |
Nota: |
| 144 |
Si x est donné pour une droite verticale, un point d'abscisse infinie |
| 145 |
est retourné. À l'inverse, si y est donné pour une droite horizontale, |
| 146 |
un point d'ordonnée infinie est retourné. |
| 147 |
""" |
| 148 |
x1, y1 = self.__point1.coordonnees |
| 149 |
x2, y2 = self.__point2.coordonnees |
| 150 |
|
| 151 |
if y is None and x is not None: |
| 152 |
a = self.pente |
| 153 |
return (x, a*(x - x1) + y1) |
| 154 |
elif x is None and y is not None: |
| 155 |
a = self.copente |
| 156 |
return (a*(y - y1) + x1, y) |
| 157 |
raise TypeError('x ou y doivent etre definis.') |
| 158 |
|
| 159 |
@property |
| 160 |
def pente(self): |
| 161 |
x1, y1 = self.__point1.coordonnees |
| 162 |
x2, y2 = self.__point2.coordonnees |
| 163 |
return (y2 - y1)/(x2 - x1) if (x2 - x1) else inf |
| 164 |
|
| 165 |
@property |
| 166 |
def copente(self): |
| 167 |
x1, y1 = self.__point1.coordonnees |
| 168 |
x2, y2 = self.__point2.coordonnees |
| 169 |
return (x2 - x1)/(y2 - y1) if (y2 - y1) else inf |
| 170 |
|
| 171 |
def _points_extremes(self): |
| 172 |
"""Donne les points d'intersection de la droite avec les bords de la fenêtre. |
| 173 |
|
| 174 |
Retourne une liste de deux points au maximum. |
| 175 |
""" |
| 176 |
s**, |
| 177 |
é. |
| 178 |
xmin, xmax, ymin, ymax = self.feuille.fenetre_reellement_affichee() |
| 179 |
eps = contexte['tolerance'] |
| 180 |
dx = eps*(xmax - xmin) |
| 181 |
dy = eps*(ymax - ymin) |
| 182 |
. |
| 183 |
points = [] |
| 184 |
Mxmin = self.xy(x=xmin) |
| 185 |
if ymin - dy <= Mxmin[1] <= ymax + dy: |
| 186 |
points.append(Mxmin) |
| 187 |
Mxmax = self.xy(x=xmax) |
| 188 |
if ymin - dy <= Mxmax[1] <= ymax + dy: |
| 189 |
points.append(Mxmax) |
| 190 |
Mymin = self.xy(y=ymin) |
| 191 |
if xmin - dx <= Mymin[0] <= xmax + dx: |
| 192 |
points.append(Mymin) |
| 193 |
Mymax = self.xy(y=ymax) |
| 194 |
if xmin - dx <= Mymax[0] <= xmax + dx: |
| 195 |
points.append(Mymax) |
| 196 |
s. |
| 197 |
te |
| 198 |
r |
| 199 |
|
| 200 |
|
| 201 |
if len(points) == 4: |
| 202 |
return points[:2] . |
| 203 |
elif len(points) == 3: |
| 204 |
if points[0] == Mxmin and points[1] == Mxmax: |
| 205 |
return points[:2] . |
| 206 |
return points[1:] |
| 207 |
else: |
| 208 |
assert len(points) <= 2 |
| 209 |
return points |
| 210 |
|
| 211 |
def angle_affichage(self): |
| 212 |
"Angle, à l'écran, de la ligne par rapport l'horizontale, en radians." |
| 213 |
x1, y1 = self.__point1.coordonnees |
| 214 |
x2, y2 = self.__point2.coordonnees |
| 215 |
dx, dy = self.feuille.dcoo2pix(x2 - x1, y2 - y1) |
| 216 |
return atan2(-dy, dx) |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
class Segment(Ligne_generique): |
| 221 |
"""Un segment. |
| 222 |
|
| 223 |
Un segment défini par deux points""" |
| 224 |
|
| 225 |
_affichage_depend_de_la_fenetre = True . |
| 226 |
_style_defaut = param.segments |
| 227 |
_prefixe_nom = "s" |
| 228 |
_marqueurs = "[]" |
| 229 |
|
| 230 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 231 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 232 |
|
| 233 |
def __init__(self, point1 = None, point2 = None, **styles): |
| 234 |
self.__point1 = point1 = Ref(point1) |
| 235 |
self.__point2 = point2 = Ref(point2) |
| 236 |
Ligne_generique.__init__(self, point1 = point1, point2 = point2, **styles) |
| 237 |
self.etiquette = Label_segment(self) |
| 238 |
|
| 239 |
|
| 240 |
def _creer_figure(self): |
| 241 |
if not self._representation: |
| 242 |
self._representation = [self.rendu.ligne(), self.rendu.codage()] |
| 243 |
x1, y1 = self.__point1.coordonnees |
| 244 |
x2, y2 = self.__point2.coordonnees |
| 245 |
couleur = self.style("couleur") |
| 246 |
epaisseur = self.style("epaisseur") |
| 247 |
niveau = self.style("niveau") |
| 248 |
style = self.style("style") |
| 249 |
|
| 250 |
for elt_graphique in self._representation: |
| 251 |
elt_graphique.set_visible(True) |
| 252 |
|
| 253 |
plot, codage = self._representation |
| 254 |
plot.set_data(((x1, x2), (y1, y2))) |
| 255 |
plot.set(color=couleur, linestyle=style, linewidth=epaisseur) |
| 256 |
plot.zorder = niveau |
| 257 |
|
| 258 |
ur |
| 259 |
if not self.style("codage"): |
| 260 |
codage.set_visible(False) |
| 261 |
else: |
| 262 |
codage.set(visible=True, style=self.style('codage'), |
| 263 |
position=(.5*(x1 + x2), .5*(y1 + y2)), |
| 264 |
direction=(x2 - x1, y2 - y1), |
| 265 |
taille=param.codage["taille"], angle=param.codage["angle"], |
| 266 |
color=couleur, linewidth=epaisseur, |
| 267 |
zorder=niveau + 0.01, |
| 268 |
) |
| 269 |
|
| 270 |
|
| 271 |
def image_par(self, transformation): |
| 272 |
return Segment(self.__point1.image_par(transformation), self.__point2.image_par(transformation)) |
| 273 |
|
| 274 |
|
| 275 |
def _distance_inf(self, x, y, d): |
| 276 |
return distance_segment((x, y), self._pixel(self.__point1), self._pixel(self.__point2), d) |
| 277 |
|
| 278 |
|
| 279 |
def _contains(self, M): |
| 280 |
|
| 281 |
|
| 282 |
A = self.__point1 |
| 283 |
B = self.__point2 |
| 284 |
xu, yu = vect(A, B) |
| 285 |
xv, yv = vect(A, M) |
| 286 |
if abs(xu*yv - xv*yu) > contexte['tolerance']: |
| 287 |
return False |
| 288 |
if abs(xu) > abs(yu): |
| 289 |
k = xv/xu |
| 290 |
elif yu: |
| 291 |
k = yv/yu |
| 292 |
else: |
| 293 |
return M.confondu(A) |
| 294 |
return 0 <= k <= 1 |
| 295 |
|
| 296 |
@property |
| 297 |
def longueur(self): |
| 298 |
"""Longueur du segment. |
| 299 |
|
| 300 |
Alias de _longueur, disponible pour indiquer que l'objet a vraiment une longueur |
| 301 |
au sens mathématique du terme (pas comme une droite !)""" |
| 302 |
return self._longueur() |
| 303 |
|
| 304 |
@property |
| 305 |
def extremites(self): |
| 306 |
return self.__point1, self.__point2 |
| 307 |
|
| 308 |
@property |
| 309 |
def info(self): |
| 310 |
return self.nom_complet + ' de longueur ' + nice_display(self.longueur) |
| 311 |
|
| 312 |
@staticmethod |
| 313 |
def _convertir(objet): |
| 314 |
if hasattr(objet, "__iter__"): |
| 315 |
return Segment(*objet) |
| 316 |
raise TypeError("'" + str(type(objet)) + "' object is not iterable") |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
class Demidroite(Ligne_generique): |
| 321 |
"""Une demi-droite. |
| 322 |
|
| 323 |
Une demi-droite définie par son origine et un deuxième point""" |
| 324 |
|
| 325 |
_style_defaut = param.droites |
| 326 |
_prefixe_nom = "d" |
| 327 |
_marqueurs = "[)" |
| 328 |
|
| 329 |
origine = __origine = Argument("Point_generique", defaut = Point) |
| 330 |
point = __point = Argument("Point_generique", defaut = Point) |
| 331 |
|
| 332 |
def __init__(self, origine = None, point = None, **styles): |
| 333 |
self.__origine = origine = Ref(origine) |
| 334 |
self.__point = point = Ref(point) |
| 335 |
Ligne_generique.__init__(self, point1 = origine, point2 = point, **styles) |
| 336 |
self.etiquette = Label_demidroite(self) |
| 337 |
|
| 338 |
def image_par(self, transformation): |
| 339 |
return Demidroite(self.__point1.image_par(transformation), self.__point2.image_par(transformation)) |
| 340 |
|
| 341 |
def _conditions_existence(self): |
| 342 |
return distincts(self.__origine, self.__point) |
| 343 |
!!) |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
def _creer_figure(self): |
| 350 |
if not self._representation: |
| 351 |
self._representation = [self.rendu.ligne()] |
| 352 |
|
| 353 |
x, y = self.__origine.coordonnees |
| 354 |
x0, y0 = self.__point.coordonnees |
| 355 |
points = self._points_extremes() |
| 356 |
if len(points) < 2: |
| 357 |
) |
| 358 |
return |
| 359 |
(x1, y1), (x2, y2) = points |
| 360 |
plot = self._representation[0] |
| 361 |
|
| 362 |
if produit_scalaire((x1 - x, y1 - y), (x0 - x, y0 - y)) > produit_scalaire((x2 - x, y2 - y), (x0 - x, y0 - y)): |
| 363 |
plot.set_data((x1, x), (y1, y)) |
| 364 |
else: |
| 365 |
plot.set_data((x, x2), (y, y2)) |
| 366 |
plot.set(color = self.style("couleur"), linestyle = self.style("style"), |
| 367 |
linewidth = self.style("epaisseur"), zorder = self.style("niveau")) |
| 368 |
|
| 369 |
|
| 370 |
def _distance_inf(self, x, y, d): |
| 371 |
|
| 372 |
xA, yA = self._pixel(self.__origine) |
| 373 |
xB, yB = self._pixel(self.__point) |
| 374 |
x1 = min(xA, xB); x2 = max(xA, xB) |
| 375 |
y1 = min(yA, yB); y2 = max(yA, yB) |
| 376 |
if (xA == x1 and x > x1 - d or xA == x2 and x < x2 + d) \ |
| 377 |
and (yA == y1 and y > y1 - d or yA == y2 and y < y2 + d): |
| 378 |
return ((yA - yB)*(x - xA)+(xB - xA)*(y - yA))**2/((xB - xA)**2+(yB - yA)**2) < d**2 |
| 379 |
else: |
| 380 |
return False |
| 381 |
|
| 382 |
|
| 383 |
def _contains(self, M): |
| 384 |
|
| 385 |
|
| 386 |
A = self.__origine |
| 387 |
B = self.__point |
| 388 |
xu, yu = vect(A, B) |
| 389 |
xv, yv = vect(A, M) |
| 390 |
if abs(xu*yv - xv*yu) > contexte['tolerance']: |
| 391 |
return False |
| 392 |
if abs(xu) > abs(yu): e |
| 393 |
k = xv/xu |
| 394 |
elif yu: e |
| 395 |
k = yv/yu |
| 396 |
else: |
| 397 |
return M.confondu(A) |
| 398 |
return 0 <= k |
| 399 |
|
| 400 |
|
| 401 |
@property |
| 402 |
def equation_formatee(self): |
| 403 |
"Equation sous forme lisible par l'utilisateur." |
| 404 |
eq = self.equation |
| 405 |
if eq is None: |
| 406 |
return "L'objet n'est pas défini." |
| 407 |
e |
| 408 |
a, b, c = (nice_display(coeff) for coeff in eq) |
| 409 |
eps = contexte['tolerance'] |
| 410 |
if abs(a) < abs(b): e |
| 411 |
if (self.__point.ordonnee - self.__origine.ordonnee) > eps: |
| 412 |
ajout = "y > " + nice_display(self.__origine.ordonnee) |
| 413 |
elif (self.__point.ordonnee - self.__origine.ordonnee) < eps: |
| 414 |
ajout = "y < " + nice_display(self.__origine.ordonnee) |
| 415 |
else: |
| 416 |
return "Précision insuffisante." |
| 417 |
else: e |
| 418 |
if (self.__point.abscisse - self.__origine.abscisse) > eps: |
| 419 |
ajout = "x > " + nice_display(self.__origine.abscisse) |
| 420 |
elif (self.__point.abscisse - self.__origine.abscisse) < eps: |
| 421 |
ajout = "x < " + nice_display(self.__origine.abscisse) |
| 422 |
else: |
| 423 |
return "Précision insuffisante." |
| 424 |
return formatage("%s x + %s y + %s = 0 et %s" %(a, b, c, ajout)) |
| 425 |
|
| 426 |
|
| 427 |
@staticmethod |
| 428 |
def _convertir(objet): |
| 429 |
if hasattr(objet, "__iter__"): |
| 430 |
return Demidroite(*objet) |
| 431 |
raise TypeError("'" + str(type(objet)) + "' object is not iterable") |
| 432 |
|
| 433 |
|
| 434 |
class Droite_generique(Ligne_generique): |
| 435 |
"""Une droite générique. |
| 436 |
|
| 437 |
Usage interne : la classe mère pour toutes les droites.""" |
| 438 |
|
| 439 |
_style_defaut = param.droites |
| 440 |
_prefixe_nom = "d" |
| 441 |
|
| 442 |
parallele = Ligne_generique._parallele |
| 443 |
perpendiculaire = Ligne_generique._perpendiculaire |
| 444 |
|
| 445 |
point1 = __point1 = Argument("Point_generique") |
| 446 |
point2 = __point2 = Argument("Point_generique") |
| 447 |
|
| 448 |
def __init__(self, point1, point2, **styles): |
| 449 |
self.__point1 = point1 = Ref(point1) |
| 450 |
self.__point2 = point2 = Ref(point2) |
| 451 |
Ligne_generique.__init__(self, point1 = point1, point2 = point2, **styles) |
| 452 |
self.etiquette = Label_droite(self) |
| 453 |
|
| 454 |
def image_par(self, transformation): |
| 455 |
return Droite(self.__point1.image_par(transformation), self.__point2.image_par(transformation)) |
| 456 |
|
| 457 |
def _conditions_existence(self): |
| 458 |
return distincts(self.__point1, self.__point2) |
| 459 |
|
| 460 |
|
| 461 |
def _creer_figure(self): |
| 462 |
if not self._representation: |
| 463 |
self._representation = [self.rendu.ligne()] |
| 464 |
points = self._points_extremes() |
| 465 |
plot = self._representation[0] |
| 466 |
if len(points) < 2: |
| 467 |
) |
| 468 |
plot._visible = False |
| 469 |
return |
| 470 |
(x1, y1), (x2, y2) = points |
| 471 |
plot._visible = True |
| 472 |
plot.set_data((x1, x2), (y1, y2)) |
| 473 |
plot.set(color = self.style("couleur"), linestyle = self.style("style"), |
| 474 |
linewidth = self.style("epaisseur"), zorder = self.style("niveau")) |
| 475 |
|
| 476 |
|
| 477 |
def _distance_inf(self, x, y, d): |
| 478 |
|
| 479 |
xA, yA = self._pixel(self.__point1) |
| 480 |
xB, yB = self._pixel(self.__point2) |
| 481 |
return ((yA-yB)*(x-xA)+(xB-xA)*(y-yA))**2/((xB-xA)**2+(yB-yA)**2) < d**2 |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
def _contains(self, M): |
| 486 |
A = self.__point1 |
| 487 |
B = self.__point2 |
| 488 |
xu, yu = vect(A, B) |
| 489 |
xv, yv = vect(A, M) |
| 490 |
return abs(xu*yv - xv*yu) < contexte['tolerance'] |
| 491 |
|
| 492 |
@property |
| 493 |
def equation_formatee(self): |
| 494 |
"Equation sous forme lisible par l'utilisateur." |
| 495 |
eq = self.equation |
| 496 |
if eq is None: |
| 497 |
return "L'objet n'est pas défini." |
| 498 |
. |
| 499 |
a, b, c = (nice_display(coeff) for coeff in eq) |
| 500 |
return formatage("%s x + %s y + %s = 0" %(a, b, c)) |
| 501 |
|
| 502 |
@property |
| 503 |
def info(self): |
| 504 |
return self.nom_complet + " d'équation " + self.equation_formatee |
| 505 |
|
| 506 |
def confondu(self, y): |
| 507 |
if self.existe and isinstance(y, Droite_generique) and y.existe: |
| 508 |
eq1 = self.equation_reduite |
| 509 |
eq2 = y.equation_reduite |
| 510 |
if len(eq1) == len(eq2) == 1: |
| 511 |
return abs(eq1[0] - eq2[0]) < contexte['tolerance'] |
| 512 |
elif len(eq1) == len(eq2) == 2: |
| 513 |
return abs(eq1[0] - eq2[0]) < contexte['tolerance'] and abs(eq1[1] - eq2[1]) < contexte['tolerance'] |
| 514 |
return False |
| 515 |
|
| 516 |
egal = egale = confondu |
| 517 |
|
| 518 |
@staticmethod |
| 519 |
def _convertir(objet): |
| 520 |
if hasattr(objet, "__iter__"): |
| 521 |
return Droite(*objet) |
| 522 |
raise TypeError("'" + str(type(objet)) + "' object is not iterable") |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
class Droite(Droite_generique): |
| 527 |
"""Une droite. |
| 528 |
|
| 529 |
Une droite définie par deux points""" |
| 530 |
|
| 531 |
|
| 532 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 533 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 534 |
|
| 535 |
def __new__(cls, *args, **kw): |
| 536 |
if len(args) == 1 and isinstance(args[0], str): |
| 537 |
newclass = Droite_equation |
| 538 |
elif len(args) == 2 and isinstance(args[1], Vecteur_generique): |
| 539 |
newclass = Droite_vectorielle |
| 540 |
else: |
| 541 |
return object.__new__(cls) |
| 542 |
droite = newclass.__new__(newclass, *args, **kw) |
| 543 |
droite.__init__(*args, **kw) |
| 544 |
return droite |
| 545 |
|
| 546 |
def __init__(self, point1 = None, point2 = None, **styles): |
| 547 |
self.__point1 = point1 = Ref(point1) |
| 548 |
self.__point2 = point2 = Ref(point2) |
| 549 |
Droite_generique.__init__(self, point1 = point1, point2 = point2, **styles) |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
class Point_droite(Point_generique): |
| 554 |
"""Un des deux points servant à construire une droite d'équation donnée. |
| 555 |
|
| 556 |
Usage interne. |
| 557 |
Ceci sert pour les droites qui ne sont pas définies à l'aide de points, mais directement à l'aide d'une équation. |
| 558 |
Comme l'implémentation actuelle des droites exige que la droite soit définie par 2 points, |
| 559 |
on génère deux points de la droite à partir de son équation.""" |
| 560 |
|
| 561 |
droite = __droite = Argument("Droite_generique") |
| 562 |
premier = __premier = ArgumentNonModifiable("bool") |
| 563 |
|
| 564 |
def __init__(self, droite, premier, **styles): |
| 565 |
self.__droite = droite = Ref(droite) |
| 566 |
self.__premier = premier |
| 567 |
Point_generique.__init__(self, **styles) |
| 568 |
|
| 569 |
def _get_coordonnees(self): |
| 570 |
a, b, c = self.__droite.equation |
| 571 |
if b: |
| 572 |
if self.__premier: |
| 573 |
return 0, -c/b |
| 574 |
else: |
| 575 |
return 1, -a/b - c/b |
| 576 |
else: |
| 577 |
if self.__premier: |
| 578 |
return -c/a, 0 |
| 579 |
else: |
| 580 |
return -c/a, 1 |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
class Droite_vectorielle(Droite_generique): |
| 585 |
"""Une droite dirigée par un vecteur. |
| 586 |
|
| 587 |
Une droite définie par un point et un vecteur directeur.""" |
| 588 |
|
| 589 |
point = __point = Argument("Point_generique", defaut=Point) |
| 590 |
vecteur = __vecteur = Argument("Vecteur_generique", defaut=Vecteur_libre) |
| 591 |
|
| 592 |
def __init__(self, point = None, vecteur = None, **styles): |
| 593 |
self.__point = point = Ref(point) |
| 594 |
self.__vecteur = vecteur = Ref(vecteur) |
| 595 |
Objet.__init__(self) isé |
| 596 |
Droite_generique.__init__(self, Point_droite(self, True), Point_droite(self, False), **styles) |
| 597 |
|
| 598 |
def _get_equation(self): |
| 599 |
a, b = self.__vecteur.coordonnees |
| 600 |
x, y = self.__point.coordonnees |
| 601 |
return -b, a, b*x - a*y |
| 602 |
|
| 603 |
def _conditions_existence(self): |
| 604 |
return self.__vecteur.x**2 + self.__vecteur.x**2 > contexte['tolerance']**2 |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
class Parallele(Droite_generique): |
| 610 |
"""Une parallèle. |
| 611 |
|
| 612 |
La parallèle à une droite passant par un point.""" |
| 613 |
|
| 614 |
droite = __droite = Argument("Droite_generique") |
| 615 |
point = __point = Argument("Point_generique", defaut = Point) |
| 616 |
|
| 617 |
def __init__(self, droite, point = None, **styles): |
| 618 |
self.__droite = droite = Ref(droite) |
| 619 |
self.__point = point = Ref(point) |
| 620 |
Objet.__init__(self) isé |
| 621 |
Droite_generique.__init__(self, Point_droite(self, True), Point_droite(self, False), **styles) |
| 622 |
|
| 623 |
def _get_equation(self): |
| 624 |
a, b, c = self.__droite.equation |
| 625 |
x, y = self.__point.coordonnees |
| 626 |
return a, b, -a*x-b*y |
| 627 |
|
| 628 |
def _conditions_existence(self): |
| 629 |
return True |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
IR |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
on |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
class Perpendiculaire(Droite_generique): |
| 655 |
"""Une perpendiculaire. |
| 656 |
|
| 657 |
Une droite perpendiculaire à une autre passant par un point.""" |
| 658 |
|
| 659 |
droite = __droite = Argument("Ligne_generique") |
| 660 |
point = __point = Argument("Point_generique", defaut = Point) |
| 661 |
|
| 662 |
def __init__(self, droite, point = None, **styles): |
| 663 |
self.__droite = droite = Ref(droite) |
| 664 |
self.__point = point = Ref(point) |
| 665 |
Objet.__init__(self) isé |
| 666 |
Droite_generique.__init__(self, Point_droite(self, True), Point_droite(self, False), **styles) |
| 667 |
|
| 668 |
def _get_equation(self): |
| 669 |
a, b, c = self.__droite.equation |
| 670 |
x, y = self.__point.coordonnees |
| 671 |
return -b, a, b*x - a*y |
| 672 |
|
| 673 |
def _conditions_existence(self): |
| 674 |
return True |
| 675 |
|
| 676 |
|
| 677 |
class Mediatrice(Perpendiculaire): |
| 678 |
"""Une médiatrice. |
| 679 |
|
| 680 |
La médiatrice d'un segment (ou d'un bipoint, ...) |
| 681 |
|
| 682 |
>>> from wxgeometrie.geolib import Point, Mediatrice, Segment |
| 683 |
>>> A=Point(1,2); B=Point(3,4) |
| 684 |
>>> s=Segment(A,B) |
| 685 |
>>> Mediatrice(A, B).confondu(Mediatrice(s)) |
| 686 |
True |
| 687 |
""" |
| 688 |
|
| 689 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 690 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 691 |
|
| 692 |
def __init__(self, point1 = None, point2 = None, **styles): |
| 693 |
if isinstance(point1, Segment): |
| 694 |
point2 = point1._Segment__point2 |
| 695 |
point1 = point1._Segment__point1 |
| 696 |
self.__point1 = point1 = Ref(point1) |
| 697 |
self.__point2 = point2 = Ref(point2) |
| 698 |
Perpendiculaire.__init__(self, Droite(point1, point2), Milieu(point1, point2), **styles) |
| 699 |
|
| 700 |
|
| 701 |
def _conditions_existence(self): |
| 702 |
return distincts(self.__point1, self.__point2) |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
class Droite_equation(Droite_generique): |
| 712 |
"""Une droite définie par une équation. |
| 713 |
|
| 714 |
Une droite d'équation donnée sous forme d'un triplet (a, b, c). (ax + by + c = 0)""" |
| 715 |
|
| 716 |
a = __a = Argument("Variable_generique") |
| 717 |
b = __b = Argument("Variable_generique") |
| 718 |
c = __c = Argument("Variable_generique") |
| 719 |
|
| 720 |
@staticmethod |
| 721 |
def __coeff(match_object): |
| 722 |
if match_object is None: |
| 723 |
return 0 |
| 724 |
chaine = match_object.group() |
| 725 |
if chaine == "-": |
| 726 |
return -1 |
| 727 |
elif chaine == "+" or not chaine: |
| 728 |
return 1 |
| 729 |
elif chaine[-1] == "*": |
| 730 |
chaine = chaine[:-1] |
| 731 |
return eval_restricted(chaine) |
| 732 |
|
| 733 |
@classmethod |
| 734 |
def __extraire_coeffs(cls, chaine): |
| 735 |
a = cls.__coeff(re.search("[-+]*[^-+xy]*(?=x)", chaine)) |
| 736 |
b = cls.__coeff(re.search("[-+]*[^-+xy]*(?=y)", chaine)) |
| 737 |
c = cls.__coeff(re.search("[-+]*[^-+xy]+(?=$|[-+])", chaine)) |
| 738 |
return a, b, c |
| 739 |
|
| 740 |
def __init__(self, a = 1, b = -1, c = 0, **styles): |
| 741 |
if isinstance(a, str): |
| 742 |
membre_gauche, membre_droite = a.replace(" ", "").split("=") |
| 743 |
a, b, c = self.__extraire_coeffs(membre_gauche) |
| 744 |
a_, b_, c_ = self.__extraire_coeffs(membre_droite) |
| 745 |
a -= a_ |
| 746 |
b -= b_ |
| 747 |
c -= c_ |
| 748 |
self.__a = a = Ref(a) |
| 749 |
self.__b = b = Ref(b) |
| 750 |
self.__c = c = Ref(c) |
| 751 |
Objet.__init__(self) isé |
| 752 |
Droite_generique.__init__(self, point1 = Point_droite(self, True), |
| 753 |
point2 = Point_droite(self, False), **styles) |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
def _conditions_existence(self): |
| 762 |
return not self.__a == self.__b == 0 |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
def _set_equation(self, a = None, b = -1, c = 0): |
| 772 |
if a is not None: |
| 773 |
|
| 774 |
self.__a = a |
| 775 |
self.__b = b |
| 776 |
self.__c = c |
| 777 |
|
| 778 |
def _get_equation(self): |
| 779 |
return self.__a, self.__b, self.__c |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
class Bissectrice(Droite_vectorielle): |
| 801 |
"""Une bissectrice. |
| 802 |
|
| 803 |
La bissectrice d'un angle défini par 3 points. |
| 804 |
On peut, au choix, entrer un angle ou 3 points comme argument.""" |
| 805 |
|
| 806 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 807 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 808 |
point3 = __point3 = Argument("Point_generique", defaut = Point) |
| 809 |
|
| 810 |
def __init__(self, point1 = None, point2 = None, point3 = None, **styles): |
| 811 |
if isinstance(point1, Secteur_angulaire): |
| 812 |
point2 = point1._Secteur_angulaire__point |
| 813 |
point1 = Point_translation(point2, Translation(point1._Secteur_angulaire__vecteur1)) |
| 814 |
point3 = Point_translation(point2, Translation(point1._Secteur_angulaire__vecteur2)) |
| 815 |
self.__point1 = point1 = Ref(point1) |
| 816 |
self.__point2 = point2 = Ref(point2) |
| 817 |
self.__point3 = point3 = Ref(point3) |
| 818 |
|
| 819 |
v = Vecteur_unitaire(Vecteur(point2, point1)) + Vecteur_unitaire(Vecteur(point2, point3)) |
| 820 |
Droite_vectorielle.__init__(self, point2, v, **styles) |
| 821 |
|
| 822 |
def _conditions_existence(self): |
| 823 |
A = self.__point1 |
| 824 |
O = self.__point2 |
| 825 |
B = self.__point3 |
| 826 |
return distincts(O, A) and distincts(O, B) |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
class Point_tangence(Point_generique): |
| 831 |
"""Un point de tangence. |
| 832 |
|
| 833 |
Le point de tangence d'un cercle et d'une droite tangente au cercle passant par un point donné. |
| 834 |
Usage interne.""" |
| 835 |
|
| 836 |
cercle = __cercle = Argument("Cercle_generique") |
| 837 |
point = __point = Argument("Point_generique") |
| 838 |
|
| 839 |
def __init__(self, cercle, point, angle_positif = None, **styles): |
| 840 |
self.__cercle = cercle = Ref(cercle) |
| 841 |
self.__point = point = Ref(point) |
| 842 |
self.__angle_positif = angle_positif = Ref(angle_positif) |
| 843 |
self.__intersection = G.Intersection_cercles(cercle, G.Cercle_diametre(Centre(cercle), point), angle_positif) |
| 844 |
Point_generique.__init__(self, **styles) |
| 845 |
|
| 846 |
def _get_coordonnees(self): |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
if self.__point in self.__cercle: |
| 851 |
return self.__point.coordonnees |
| 852 |
return self.__intersection.coordonnees |
| 853 |
|
| 854 |
def _conditions_existence(self): |
| 855 |
return self.__intersection.existe or self.__point in self.__cercle |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
class Tangente(Perpendiculaire): ? |
| 860 |
"""Une tangente. |
| 861 |
|
| 862 |
Une des deux tangentes à un cercle passant par un point extérieur au cercle. |
| 863 |
Le dernier paramètre (True/False) sert à distinguer les deux tangentes. |
| 864 |
(Voir la classe Intersection_cercles pour plus d'infos).""" |
| 865 |
|
| 866 |
cercle = __cercle = Argument("Cercle_generique", defaut='Cercle') |
| 867 |
point = __point = Argument("Point_generique", defaut = Point) |
| 868 |
angle_positif = __angle_positif = Argument("bool", defaut = True) |
| 869 |
|
| 870 |
def __init__(self, cercle = None, point = None, angle_positif = None, **styles): |
| 871 |
self.__cercle = cercle = Ref(cercle) |
| 872 |
self.__point = point = Ref(point) |
| 873 |
self.__angle_positif = angle_positif = Ref(angle_positif) |
| 874 |
self.point_tangence = self.__point_tangence = Point_tangence(cercle, point, angle_positif) |
| 875 |
Perpendiculaire.__init__(self, Droite(Centre(cercle), self.__point_tangence), self.__point_tangence, **styles) |
| 876 |
|
| 877 |
|
| 878 |
def _conditions_existence(self): |
| 879 |
return self.__point_tangence.existe |
| 880 |
|
| 881 |
|
| 882 |
def _set_feuille(self): |
| 883 |
t, |
| 884 |
. |
| 885 |
if "_Tangente__angle_positif" in self._valeurs_par_defaut: |
| 886 |
for objet in self.feuille.objets.lister(type = Tangente): |
| 887 |
if objet._Tangente__cercle is self.__cercle and objet._Tangente__point is self.__point: |
| 888 |
e |
| 889 |
self.__angle_positif = not objet._Tangente__angle_positif |
| 890 |
break |
| 891 |
if "_Tangente__point" in self._valeurs_par_defaut: |
| 892 |
if distance(self.__cercle.centre, self.__point) < self.__cercle.rayon: |
| 893 |
r = self.__cercle.rayon*uniform(1, 2) |
| 894 |
a = uniform(0, 2*pi) |
| 895 |
self.__point.coordonnees = self.__cercle.centre.x + r*cos(a), self.__cercle.centre.y + r*sin(a) |
| 896 |
Objet._set_feuille(self) |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
class Demiplan(Objet_avec_equation): |
| 906 |
"""Un demi-plan. |
| 907 |
|
| 908 |
Le demi-plan délimité par la droite d et contenant le point M.""" |
| 909 |
|
| 910 |
_affichage_depend_de_la_fenetre = True |
| 911 |
_marqueurs = "()" |
| 912 |
_style_defaut = param.polygones |
| 913 |
_prefixe_nom = "P" |
| 914 |
|
| 915 |
droite = __droite = Argument('Ligne_generique', defaut = Droite) |
| 916 |
point = __point = Argument('Point_generique', defaut = Point) |
| 917 |
droite_incluse = __droite_incluse = Argument(bool, defaut = True) |
| 918 |
|
| 919 |
def __init__(self, droite = None, point = None, droite_incluse = None, **styles): |
| 920 |
self.__droite = droite = Ref(droite) |
| 921 |
self.__point = point = Ref(point) |
| 922 |
self.__droite_incluse = droite_incluse = Ref(droite_incluse) |
| 923 |
Objet_avec_equation.__init__(self, **styles) |
| 924 |
|
| 925 |
|
| 926 |
def _get_equation(self): |
| 927 |
return self.__droite.equation |
| 928 |
|
| 929 |
|
| 930 |
@property |
| 931 |
def equation_formatee(self): |
| 932 |
"Equation sous forme lisible par l'utilisateur." |
| 933 |
eq = self.equation |
| 934 |
if eq is None: |
| 935 |
return "Le demi-plan n'est pas défini." |
| 936 |
test = self._signe() |
| 937 |
if test < 0: |
| 938 |
symbole = '<' |
| 939 |
elif test > 0: |
| 940 |
symbole = '>' |
| 941 |
else: |
| 942 |
return "Le demi-plan n'est pas défini." |
| 943 |
a, b, c = (nice_display(coeff) for coeff in eq) |
| 944 |
e |
| 945 |
if self.__droite_incluse: |
| 946 |
symbole += '=' |
| 947 |
return formatage("%s x + %s y + %s %s 0" %(a, b, c, symbole)) |
| 948 |
|
| 949 |
def _signe(self, xy = None): |
| 950 |
x, y = (xy if xy else self.__point.xy) |
| 951 |
a, b, c = self.equation |
| 952 |
return (1 if a*x + b*y + c >= 0 else -1) |
| 953 |
|
| 954 |
def _contains(self, M): |
| 955 |
signe = self._signe(M) |
| 956 |
return signe == self._signe() or (self.__droite_incluse and M in self.__droite) |
| 957 |
|
| 958 |
def _conditions_existence(self): |
| 959 |
return self.__point not in self.__droite |
| 960 |
|
| 961 |
|
| 962 |
def _creer_figure(self): |
| 963 |
if not self._representation: |
| 964 |
self._representation = [self.rendu.ligne(), self.rendu.polygone()] |
| 965 |
plot, fill = self._representation |
| 966 |
: |
| 967 |
points = self.__droite._points_extremes() |
| 968 |
couleur, niveau = self.style(('couleur', 'niveau')) |
| 969 |
xmin, xmax, ymin, ymax = self.feuille.fenetre_reellement_affichee() |
| 970 |
. |
| 971 |
coins = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)] |
| 972 |
sommets = [coin for coin in coins if (coin in self)] |
| 973 |
|
| 974 |
if len(points) == 2: |
| 975 |
2 |
| 976 |
(x1, y1), (x2, y2) = points |
| 977 |
plot.set_data((x1, x2), (y1, y2)) |
| 978 |
plot.set(color = couleur, linestyle = self.style("style"), |
| 979 |
linewidth = self.style("epaisseur"), zorder = niveau + 0.01) |
| 980 |
sommets.extend([(x1, y1), (x2, y2)]) |
| 981 |
x0, y0 = (x1 + x2)/2, (y1 + y2)/2 |
| 982 |
sommets.sort(key = lambda xy: atan2(xy[0] - x0, xy[1] - y0)) |
| 983 |
|
| 984 |
elif len(sommets) > 1: |
| 985 |
). |
| 986 |
|
| 987 |
assert len(sommets) == 4 |
| 988 |
|
| 989 |
else: |
| 990 |
fill.set(visible=False) |
| 991 |
return |
| 992 |
|
| 993 |
fill.set_xy(sommets + [sommets[0]]) |
| 994 |
fill.set(edgecolor=couleur, facecolor=couleur, alpha=self.style('alpha'), |
| 995 |
zorder=niveau, visible=True, hatch=self.style('hachures')) |
| 996 |
|
| 997 |
|
| 998 |
def _distance_inf(self, x, y, d): |
| 999 |
return self.feuille.pix2coo(x, y) in self |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
class Axe(Droite): |
| 1004 |
"""Un axe orienté. |
| 1005 |
|
| 1006 |
Un axe orienté servant au repérage. |
| 1007 |
Cette classe sert essentiellement à construire les axes (Ox) et (Oy). |
| 1008 |
|
| 1009 |
Styles spécifiques: |
| 1010 |
- `graduations` (bool): afficher ou non les graduations |
| 1011 |
- `pas` (int|None): |
| 1012 |
* entier `n` (non nul): `n` fois l'écart entre les deux points repères |
| 1013 |
de l'axe. |
| 1014 |
* `None` ou `0`: adapte automatiquement la taille |
| 1015 |
de la graduation à la fenêtre (défaut) |
| 1016 |
- `pas_num` (int): entier positif `n`: |
| 1017 |
* si `n` est nul, n'affiche aucun nombre sur l'axe ; |
| 1018 |
* sinon, affiche un nombre toutes les `n` graduations |
| 1019 |
(en partant de l'origine de l'axe). |
| 1020 |
En particulier, si `pas_num=1`, toutes les graduations |
| 1021 |
auront un nombre. |
| 1022 |
- `repeter` (bool): répéter ou non la numérotation sur les axes. |
| 1023 |
- `hauteur` (int): hauteur d'une graduation (en pixels). |
| 1024 |
- `placement_num` (-1|1): position de la numérotation par rapport à l'axe. |
| 1025 |
""" |
| 1026 |
|
| 1027 |
_style_defaut = param.axes |
| 1028 |
_prefixe_nom = "a" |
| 1029 |
point1 = __point1 = Argument("Point_generique", defaut=Point) |
| 1030 |
point2 = __point2 = Argument("Point_generique", defaut=Point) |
| 1031 |
|
| 1032 |
def __init__(self, point1=None, point2=None, **styles): |
| 1033 |
self.__point1 = point1 = Ref(point1) |
| 1034 |
self.__point2 = point2 = Ref(point2) |
| 1035 |
Droite.__init__(self, point1, point2, **styles) |
| 1036 |
|
| 1037 |
|
| 1038 |
def _creer_figure(self): |
| 1039 |
if not self._representation: |
| 1040 |
self._representation = [self.rendu.fleche(), self.rendu.lignes()] |
| 1041 |
else: |
| 1042 |
|
| 1043 |
s. |
| 1044 |
self._representation = self._representation[:2] |
| 1045 |
|
| 1046 |
e |
| 1047 |
|
| 1048 |
fleche = self._representation[0] |
| 1049 |
linewidth = self.style('epaisseur') |
| 1050 |
color = self.style('couleur') |
| 1051 |
zorder = self.style("niveau") |
| 1052 |
taille = self.style("taille") |
| 1053 |
|
| 1054 |
e |
| 1055 |
points = self._points_extremes() |
| 1056 |
if len(points) < 2: |
| 1057 |
) |
| 1058 |
return |
| 1059 |
(x1, y1), (x2, y2) = points |
| 1060 |
|
| 1061 |
O = self.__point1 |
| 1062 |
I = self.__point2 |
| 1063 |
xO, yO = O.xy |
| 1064 |
xI, yI = I.xy |
| 1065 |
|
| 1066 |
é |
| 1067 |
if abs(xI - xO) > contexte['tolerance']: |
| 1068 |
if (x2 - x1)*(xI - xO) < 0: |
| 1069 |
x2, x1, y2, y1 = x1, x2, y1, y2 |
| 1070 |
else: |
| 1071 |
if (y2 - y1)*(yI - yO) < 0: |
| 1072 |
x2, x1, y2, y1 = x1, x2, y1, y2 |
| 1073 |
fleche.set(xy0=(x1, y1), xy1=(x2, y2), linewidth=linewidth, |
| 1074 |
angle=self.style("angle"), taille=taille, |
| 1075 |
double=self.style("double_fleche"), |
| 1076 |
color=color, linestyle=self.style("style"), |
| 1077 |
zorder=zorder, |
| 1078 |
) |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
lignes = self._representation[1] |
| 1083 |
|
| 1084 |
if not self.style("graduations"): |
| 1085 |
lignes.set(visible=False) |
| 1086 |
return |
| 1087 |
|
| 1088 |
pas = self.style("pas") |
| 1089 |
if not pas: |
| 1090 |
s |
| 1091 |
pxO, pyO = self.__point1._pixel() |
| 1092 |
pxI, pyI = self.__point2._pixel() |
| 1093 |
norme = hypot(pxI - pxO, pyI - pyO) |
| 1094 |
nt |
| 1095 |
s. |
| 1096 |
t |
| 1097 |
eux |
| 1098 |
|
| 1099 |
, |
| 1100 |
|
| 1101 |
if norme < contexte['tolerance']: |
| 1102 |
|
| 1103 |
return |
| 1104 |
pas = arrondir_1_25_5(contexte['graduation']/norme) |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
xu, yu = vect(O, I) |
| 1109 |
xu *= pas |
| 1110 |
yu *= pas |
| 1111 |
|
| 1112 |
e |
| 1113 |
. |
| 1114 |
e. |
| 1115 |
xmin, xmax = sorted((x1, x2)) |
| 1116 |
ymin, ymax = sorted((y1, y2)) |
| 1117 |
|
| 1118 |
s |
| 1119 |
ro). |
| 1120 |
if abs(yu) < abs(xu): |
| 1121 |
e |
| 1122 |
assert xu |
| 1123 |
. |
| 1124 |
n = (xmin - xO)/xu |
| 1125 |
min. |
| 1126 |
n = (ceil(n) if xu > 0 else floor(n)) |
| 1127 |
|
| 1128 |
x = xO + n*xu |
| 1129 |
y = yO + n*yu |
| 1130 |
assert x > xmin |
| 1131 |
|
| 1132 |
). |
| 1133 |
sens = sign(xu) |
| 1134 |
|
| 1135 |
else: |
| 1136 |
e |
| 1137 |
assert yu |
| 1138 |
|
| 1139 |
n = (ymin - yO)/yu |
| 1140 |
e y. |
| 1141 |
n = (ceil(n) if yu > 0 else floor(n)) |
| 1142 |
x = xO + n*xu |
| 1143 |
y = yO + n*yu |
| 1144 |
assert y > ymin |
| 1145 |
sens = sign(yu) |
| 1146 |
|
| 1147 |
|
| 1148 |
ns. |
| 1149 |
! |
| 1150 |
faut |
| 1151 |
|
| 1152 |
`. |
| 1153 |
pxu, pyu = self.feuille.dcoo2pix(xu, yu) |
| 1154 |
pnorm = hypot(pxu, pyu) |
| 1155 |
k = .5*self.style('hauteur')/pnorm |
| 1156 |
: |
| 1157 |
pxv, pyv = k*pyu, -k*pxu |
| 1158 |
s. |
| 1159 |
xv, yv = self.feuille.dpix2coo(pxv, pyv) |
| 1160 |
|
| 1161 |
segments = [] |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
! |
| 1166 |
while xmin <= x <= xmax and ymin <= y <= ymax: |
| 1167 |
if hypot(*self.feuille.dcoo2pix(x2 - x, y2 - y)) > 1.5*taille: |
| 1168 |
he |
| 1169 |
segments.append([(x - xv, y - yv), (x + xv, y + yv)]) |
| 1170 |
x += sens*xu |
| 1171 |
y += sens*yu |
| 1172 |
|
| 1173 |
lignes.set(segments=segments, visible=True, color=color, lw=linewidth, |
| 1174 |
zorder=zorder) |
| 1175 |
|
| 1176 |
s |
| 1177 |
|
| 1178 |
|
| 1179 |
pas_num = self.style('pas_num') |
| 1180 |
|
| 1181 |
if not pas_num: |
| 1182 |
return |
| 1183 |
|
| 1184 |
. |
| 1185 |
. |
| 1186 |
ple |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
n += sens*(n%pas_num) |
| 1192 |
x = xO + n*xu |
| 1193 |
y = yO + n*yu |
| 1194 |
|
| 1195 |
|
| 1196 |
pvnorm = hypot(pxv, pyv) |
| 1197 |
taille_txt = self.etiquette.style('taille') |
| 1198 |
couleur_txt = self.etiquette.style('couleur') |
| 1199 |
ion, |
| 1200 |
. |
| 1201 |
` |
| 1202 |
|
| 1203 |
coeff = self.style('placement_num')*(pvnorm + 2 + .5*taille_txt)/pvnorm |
| 1204 |
xw, yw = self.feuille.dpix2coo(coeff*pxv, coeff*pyv) |
| 1205 |
|
| 1206 |
eps = contexte['tolerance'] |
| 1207 |
indice = 0 |
| 1208 |
indice_O = None |
| 1209 |
valeurs = [] |
| 1210 |
while xmin <= x <= xmax and ymin <= y <= ymax: |
| 1211 |
if hypot(*self.feuille.dcoo2pix(x2 - x, y2 - y)) > 1.5*taille: |
| 1212 |
valeurs.append((x, y, n)) |
| 1213 |
if abs(x - xO) < eps: |
| 1214 |
indice_O = indice |
| 1215 |
indice += 1 |
| 1216 |
n += sens*pas_num |
| 1217 |
x += sens*pas_num*xu |
| 1218 |
y += sens*pas_num*yu |
| 1219 |
|
| 1220 |
if not self.style('repeter'): |
| 1221 |
le |
| 1222 |
0 |
| 1223 |
. |
| 1224 |
if indice_O is not None: |
| 1225 |
if indice_O == len(valeurs) - 1: |
| 1226 |
valeurs = valeurs[indice_O - 1:] |
| 1227 |
else: |
| 1228 |
valeurs = valeurs[indice_O:indice_O + 2] |
| 1229 |
else: |
| 1230 |
valeurs = valeurs[:2] |
| 1231 |
|
| 1232 |
|
| 1233 |
for x, y, n in valeurs: |
| 1234 |
s = nice_display(n*pas) |
| 1235 |
if s[0] == '-': |
| 1236 |
s = '$%s$' % s |
| 1237 |
txt = self.rendu.texte(x + xw, y + yw, s, |
| 1238 |
va='center', ha='center', size=taille_txt, |
| 1239 |
color = couleur_txt) |
| 1240 |
self._representation.append(txt) |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
class Repere(Objet): |
| 1249 |
"""Un repère du plan. |
| 1250 |
|
| 1251 |
Un repère du plan défini par deux axes de même origine.""" |
| 1252 |
|
| 1253 |
_style_defaut = param.axes |
| 1254 |
_prefixe_nom = "rep" |
| 1255 |
axe1 = __axe1 = Argument(Axe) |
| 1256 |
axe2 = __axe2 = Argument(Axe) |
| 1257 |
|
| 1258 |
def __init__(self, axe1=None, axe2=None, **styles): |
| 1259 |
self.__axe1 = axe1 = Ref(axe1) |
| 1260 |
self.__axe2 = axe2 = Ref(axe2) |
| 1261 |
Objet.__init__(self, **styles) |
| 1262 |
|
| 1263 |
def style(self, nom_style=None, **kw): |
| 1264 |
if kw: |
| 1265 |
self.__axe1.style(**kw) |
| 1266 |
self.__axe2.style(**kw) |
| 1267 |
return self.style(nom_style) |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
. |
| 1275 |
" |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
. |
| 1291 |
|
| 1292 |
on |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
. |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
class Tangente_glisseur_interpolation(Droite_equation): |
| 1327 |
"""Une tangente à une courbe de type interpolation polynomiale par morceau, et qui glisse. |
| 1328 |
|
| 1329 |
Le coefficient directeur est estimé par approximation numérique avec taux de variation |
| 1330 |
sur un pas self.__canvas__.pas(). |
| 1331 |
|
| 1332 |
:type courbe: Interpolation_polynomiale_par_morceaux |
| 1333 |
:param courbe: la courbe sur laquelle va se placer la tangente |
| 1334 |
:type P: Glisseur_courbe |
| 1335 |
:param P: Point de type glisseur sur la courbe. |
| 1336 |
|
| 1337 |
exemple:: |
| 1338 |
|
| 1339 |
>>> from wxgeometrie import Point, Interpolation_polynomiale_par_morceaux |
| 1340 |
>>> from wxgeometrie import Glisseur_courbe, Tangente_glisseur_interpolation |
| 1341 |
>>> A = Point(-1,-2) |
| 1342 |
>>> B = Point(2,1) |
| 1343 |
>>> C = Point(8,-3) |
| 1344 |
>>> d = Interpolation_polynomiale_par_morceaux(A,B,C, derivees=[-1,0.5,2]) |
| 1345 |
>>> P = Glisseur_courbe(d) |
| 1346 |
>>> d1 = Tangente_glisseur_interpolation(d, P) |
| 1347 |
|
| 1348 |
""" |
| 1349 |
|
| 1350 |
courbe = __courbe = Argument('Interpolation_polynomiale_par_morceaux') |
| 1351 |
glisseur = __glisseur = Argument('Glisseur_courbe') |
| 1352 |
|
| 1353 |
def __init__(self, courbe, glisseur=None): |
| 1354 |
self.__courbe = courbe = Ref(courbe) |
| 1355 |
self.__glisseur = P = Ref(glisseur) |
| 1356 |
Droite_equation.__init__(self) |
| 1357 |
|
| 1358 |
|
| 1359 |
def _get_equation(self): |
| 1360 |
x, y = self.glisseur.xy |
| 1361 |
ère). |
| 1362 |
v = self.courbe.fonction(x, 1) |
| 1363 |
self._set_equation(a = -v, b = 1, c = -y + v*x) |
| 1364 |
return Droite_equation._get_equation(self) |