| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
from random import uniform |
| 25 |
from math import sin, cos, pi, hypot, sqrt |
| 26 |
from cmath import phase, rect |
| 27 |
|
| 28 |
from numpy import arange, cos as ncos, sin as nsin |
| 29 |
|
| 30 |
from sympy import pi as PI |
| 31 |
|
| 32 |
from .angles import Angle_vectoriel |
| 33 |
from .labels import Label_cercle, Label_arc_cercle |
| 34 |
from .lignes import Segment |
| 35 |
from .objet import Objet, Objet_avec_equation, Ref, Argument, issympy, contexte, \ |
| 36 |
FILL_STYLES, TYPES_REELS, G |
| 37 |
from .points import Point, Milieu, Centre, Point_generique, Point_equidistant, Glisseur_cercle |
| 38 |
from .routines import nice_display, angle_vectoriel, distance, formatage, trigshift, \ |
| 39 |
carre_distance_point_ellipse, vect |
| 40 |
from .variables import Mul, Rayon |
| 41 |
from .vecteurs import Vecteur |
| 42 |
from .. import param |
| 43 |
from ..pylib import fullrange |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
class Cercle_Arc_generique(Objet_avec_equation): |
| 56 |
"""La classe mère de tous les cercles et arcs de cercles.""" |
| 57 |
|
| 58 |
_affichage_depend_de_la_fenetre = True |
| 59 |
s |
| 60 |
|
| 61 |
centre = __centre = Argument("Point_generique") |
| 62 |
|
| 63 |
def __init__(self, centre, **styles): |
| 64 |
self.__centre = centre = Ref(centre) |
| 65 |
Objet.__init__(self, **styles) |
| 66 |
|
| 67 |
def _get_equation(self): |
| 68 |
"""Retourne un triplet (a,b,c) tel que x**2 + y**2 + ax + by + c = 0 |
| 69 |
soit une équation du cercle.""" |
| 70 |
xO, yO = self.__centre.coordonnees |
| 71 |
r = self.rayon |
| 72 |
return (-2*xO, -2*yO, xO**2 + yO**2 - r**2) |
| 73 |
|
| 74 |
|
| 75 |
@property |
| 76 |
def rayon(self): |
| 77 |
raise NotImplementedError |
| 78 |
|
| 79 |
@property |
| 80 |
def diametre(self): |
| 81 |
return 2*self.rayon |
| 82 |
|
| 83 |
def _distance_inf(self, x, y, d): s |
| 84 |
x0, y0 = self._pixel(self.__centre) |
| 85 |
rx, ry = self.feuille.dcoo2pix(self.rayon, self.rayon) |
| 86 |
rx = abs(rx) ; ry = abs(ry) |
| 87 |
if x0 - rx - d < x < x0 + rx + d and y0 - ry - d < y < y0 + ry + d: |
| 88 |
return carre_distance_point_ellipse((x0, y0), rx, ry, (x, y), epsilon = .000001) < d**2 |
| 89 |
return False |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
class Arc_generique(Cercle_Arc_generique): |
| 96 |
"""La classe mère de tous les arcs de cercles.""" |
| 97 |
|
| 98 |
_style_defaut = param.arcs |
| 99 |
_prefixe_nom = "a" |
| 100 |
_affichage_depend_de_la_fenetre = True |
| 101 |
|
| 102 |
centre = __centre = Argument("Point_generique") |
| 103 |
point = __point = Argument("Point_generique") |
| 104 |
|
| 105 |
def __init__(self, centre, point, **styles): |
| 106 |
self.__centre = centre = Ref(centre) |
| 107 |
self.__point = point = Ref(point) |
| 108 |
Cercle_Arc_generique.__init__(self, centre, **styles) |
| 109 |
self.etiquette = Label_arc_cercle(self) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@property |
| 118 |
def rayon(self): |
| 119 |
try: |
| 120 |
return distance(self.__centre, self.__point) |
| 121 |
except TypeError: |
| 122 |
return 0 |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
@property |
| 127 |
def equation_formatee(self): |
| 128 |
"Equation sous forme lisible par l'utilisateur." |
| 129 |
eq = self.equation |
| 130 |
if eq is None: |
| 131 |
return "L'objet n'est pas défini." |
| 132 |
a, b, c = (nice_display(coeff) for coeff in eq) |
| 133 |
e |
| 134 |
xmin, xmax, ymin, ymax = (nice_display(extremum) for extremum in self._espace_vital()) |
| 135 |
return formatage("x² + y² + %s x + %s y + %s = 0 avec %s<x<%s et %s<y<%s" %(a, b, c, xmin, xmax, ymin, ymax)) |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
def _distance_inf(self, x, y, d): |
| 149 |
e |
| 150 |
a, b = self._intervalle() |
| 151 |
z0 = self.__centre.z |
| 152 |
feuille = self.feuille |
| 153 |
xM, yM = feuille.pix2coo(x, y) |
| 154 |
zM = xM + 1j*yM |
| 155 |
if abs(zM - z0) > 10*contexte['tolerance']: |
| 156 |
phi = phase(zM - z0) |
| 157 |
if not (a <= trigshift(phi, a) <= b): |
| 158 |
|
| 159 |
, |
| 160 |
e, |
| 161 |
se |
| 162 |
zA = z0 + rect(self.rayon, a) |
| 163 |
zB = z0 + rect(self.rayon, b) |
| 164 |
l |
| 165 |
_xA, _yA = feuille.coo2pix(zA.real, zA.imag) |
| 166 |
_xB, _yB = feuille.coo2pix(zB.real, zB.imag) |
| 167 |
return min((_xA - x)**2 + (_yA - y)**2, (_xB - x)**2 + (_yB - y)**2) < d**2 |
| 168 |
return Cercle_Arc_generique._distance_inf(self, x, y, d) |
| 169 |
|
| 170 |
|
| 171 |
def _intervalle(self, exact=False): |
| 172 |
"Renvoie deux nombres a < b. L'arc est l'ensemble des points (r*cos(t), r*sin(t)) pour t apartenant à [a, b]." |
| 173 |
raise NotImplementedError |
| 174 |
|
| 175 |
def _sens(self): |
| 176 |
return 1 |
| 177 |
|
| 178 |
def _longueur(self): |
| 179 |
a, b = self._intervalle(exact=True) |
| 180 |
return (b - a)*self.rayon |
| 181 |
|
| 182 |
@property |
| 183 |
def longueur(self): |
| 184 |
try: |
| 185 |
return self._longueur() |
| 186 |
except TypeError: |
| 187 |
return 0 |
| 188 |
|
| 189 |
@property |
| 190 |
def extremites(self): |
| 191 |
"Extrémités de l'arc." |
| 192 |
raise NotImplementedError |
| 193 |
|
| 194 |
@property |
| 195 |
def info(self): |
| 196 |
return self.nom_complet + ' de longueur ' + nice_display(self.longueur) |
| 197 |
|
| 198 |
|
| 199 |
def _t(self): |
| 200 |
u, v = self._intervalle() |
| 201 |
x, y = self.__centre.coordonnees |
| 202 |
xmin, xmax, ymin, ymax = self.feuille.fenetre |
| 203 |
w = 3*(xmax - xmin) |
| 204 |
h = 3*(ymax - ymin) |
| 205 |
if xmin - w < x < xmax + w and ymin - h < y < ymax + h: |
| 206 |
return [fullrange(u, v, self.canvas.pas())] |
| 207 |
else: |
| 208 |
re. |
| 209 |
A = xmin + 1j*ymin |
| 210 |
B = xmax + 1j*ymin |
| 211 |
C = xmax + 1j*ymax |
| 212 |
D = xmin + 1j*ymax |
| 213 |
O = x + 1j*y |
| 214 |
a = phase(A - O) |
| 215 |
b = phase(B - O) |
| 216 |
c = phase(C - O) |
| 217 |
d = phase(D - O) |
| 218 |
if x >= xmax and ymin <= y <= ymax: |
| 219 |
assert (a <= 0 and b <= 0 and c >= 0 and d >= 0) |
| 220 |
a += 2*pi |
| 221 |
b += 2*pi |
| 222 |
r : |
| 223 |
a, b = min(a, b, c, d), max(a, b, c, d) |
| 224 |
|
| 225 |
: |
| 226 |
k = trigshift(a, u) - a |
| 227 |
a += k |
| 228 |
b += k |
| 229 |
x |
| 230 |
intersection = [] |
| 231 |
if a < v: |
| 232 |
c = min(b, v) |
| 233 |
print(a, c) |
| 234 |
intersection.append(fullrange(a, c, self.canvas.pas())) |
| 235 |
u += 2*pi |
| 236 |
v += 2*pi |
| 237 |
if b > u: |
| 238 |
c = min(b, v) |
| 239 |
intersection.append(fullrange(u, c, self.canvas.pas())) |
| 240 |
return intersection |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
def _creer_figure(self): |
| 245 |
if not self._representation: |
| 246 |
self._representation = [self.rendu.ligne(), self.rendu.ligne(), self.rendu.codage()] |
| 247 |
e |
| 248 |
|
| 249 |
for plot in self._representation[1:]: |
| 250 |
plot.set_visible(False) |
| 251 |
|
| 252 |
x, y = self._Arc_generique__centre.coordonnees |
| 253 |
r = self.rayon |
| 254 |
niveau, couleur, style, epaisseur = self.style(('niveau', 'couleur', 'style', 'epaisseur')) |
| 255 |
|
| 256 |
for i, t in enumerate(self._t()): |
| 257 |
plot = self._representation[i] |
| 258 |
plot.set_data(x + r*ncos(t), y + r*nsin(t)) |
| 259 |
plot.set(color=couleur, linestyle=style, linewidth=epaisseur, zorder=niveau, visible=True) |
| 260 |
|
| 261 |
r) |
| 262 |
if self.style("codage"): |
| 263 |
a, b = self._intervalle() |
| 264 |
c = .5*(a + b) |
| 265 |
x0 = x + r*cos(c) |
| 266 |
y0 = y + r*sin(c) |
| 267 |
self._representation[2].set(visible=True, style=self.style('codage'), |
| 268 |
position=(x0, y0), |
| 269 |
direction=(y0 - y, x - x0), |
| 270 |
taille=param.codage["taille"], angle=param.codage["angle"], |
| 271 |
color=couleur, linewidth=epaisseur, |
| 272 |
zorder=niveau + 0.01, |
| 273 |
) |
| 274 |
|
| 275 |
|
| 276 |
def _espace_vital(self): |
| 277 |
x0, y0 = self.__centre.coordonnees |
| 278 |
a, b = self._intervalle() |
| 279 |
t = arange(a, b, .003) |
| 280 |
r = self.rayon |
| 281 |
u = x0 + r*ncos(t) |
| 282 |
v = y0 + r*nsin(t) |
| 283 |
return min(u), max(u), min(v), max(v) |
| 284 |
|
| 285 |
|
| 286 |
def _contains(self, M): |
| 287 |
O = self.__centre |
| 288 |
a, b = self._intervalle() |
| 289 |
vec = vect(O, M) |
| 290 |
eps = contexte['tolerance'] |
| 291 |
if hypot(*vec) < eps: |
| 292 |
return self.rayon < eps l |
| 293 |
else: |
| 294 |
c = angle_vectoriel(G.vecteur_unite, vec) |
| 295 |
if c < a: |
| 296 |
c += 2*pi |
| 297 |
|
| 298 |
return abs(distance(O, M) - self.rayon) < eps and a - eps < c < b + eps |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
class Arc_cercle(Arc_generique): |
| 306 |
"""Un arc de cercle. |
| 307 |
|
| 308 |
Un arc de cercle orienté, défini par son centre et ses extremités(*), dans le sens direct. |
| 309 |
|
| 310 |
(*) note : le troisième point n'appartient pas forcément à l'arc de cercle, mais sert à en délimiter l'angle au centre.""" |
| 311 |
|
| 312 |
|
| 313 |
centre = __centre = Argument("Point_generique", defaut = Point) |
| 314 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 315 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 316 |
|
| 317 |
def __init__(self, centre = None, point1 = None, point2 = None, **styles): |
| 318 |
self.__centre = centre = Ref(centre) |
| 319 |
self.__point1 = point1 = Ref(point1) |
| 320 |
self.__point2 = point2 = Ref(point2) |
| 321 |
Arc_generique.__init__(self, centre, point1, **styles) |
| 322 |
self._angle1 = Angle_vectoriel(G.vecteur_unite, Vecteur(centre, point1)) |
| 323 |
self._angle2 = Angle_vectoriel(G.vecteur_unite, Vecteur(centre, point2)) |
| 324 |
|
| 325 |
def image_par(self, transformation): |
| 326 |
return Arc_cercle(self.__centre.image_par(transformation), self.__point1.image_par(transformation), self.__point2.image_par(transformation)) |
| 327 |
|
| 328 |
def _intervalle(self, exact=False): |
| 329 |
a = self._angle1.valeur |
| 330 |
b = self._angle2.valeur |
| 331 |
if not exact: |
| 332 |
a = float(a) |
| 333 |
b = float(b) |
| 334 |
if b < a: |
| 335 |
b += 2*(PI if issympy(b) else pi) |
| 336 |
return a, b |
| 337 |
|
| 338 |
@property |
| 339 |
def extremites(self): |
| 340 |
return self.__point1, self.__point2 |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
class Arc_points(Arc_generique): |
| 349 |
"""Un arc défini par 3 points. |
| 350 |
|
| 351 |
Un arc de cercle, défini par ses extrémités, et un autre point.""" |
| 352 |
|
| 353 |
|
| 354 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 355 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 356 |
point3 = __point3 = Argument("Point_generique", defaut = Point) |
| 357 |
|
| 358 |
def __init__(self, point1 = None, point2 = None, point3 = None, **styles): |
| 359 |
self.__point1 = point1 = Ref(point1) |
| 360 |
self.__point2 = point2 = Ref(point2) |
| 361 |
self.__point3 = point3 = Ref(point3) |
| 362 |
Arc_generique.__init__(self, Point_equidistant(point1, point2, point3), point1, **styles) |
| 363 |
centre = self._Arc_generique__centre |
| 364 |
self._angle1 = Angle_vectoriel(G.vecteur_unite, Vecteur(centre, point1)) |
| 365 |
self._angle2 = Angle_vectoriel(G.vecteur_unite, Vecteur(centre, point2)) |
| 366 |
self._angle3 = Angle_vectoriel(G.vecteur_unite, Vecteur(centre, point3)) |
| 367 |
|
| 368 |
|
| 369 |
def image_par(self, transformation): |
| 370 |
return Arc_points(self.__point1.image_par(transformation), self.__point2.image_par(transformation), self.__point3.image_par(transformation)) |
| 371 |
|
| 372 |
def _intervalle(self, exact=False): |
| 373 |
|
| 374 |
a = self._angle1.valeur |
| 375 |
b = self._angle2.valeur |
| 376 |
c = self._angle3.valeur |
| 377 |
if not exact: |
| 378 |
a = float(a) |
| 379 |
b = float(b) |
| 380 |
c = float(c) |
| 381 |
if b < a: |
| 382 |
b += 2*(PI if issympy(b) else pi) |
| 383 |
if c < a: |
| 384 |
c += 2*(PI if issympy(c) else pi) |
| 385 |
if b < c: |
| 386 |
return a, c |
| 387 |
return c, a + 2*(PI if issympy(a) else pi) |
| 388 |
|
| 389 |
def _sens(self): |
| 390 |
"Sens de parcours de l'arc : direct (1) ou indirect (-1)" |
| 391 |
|
| 392 |
a = float(self._angle1.valeur) |
| 393 |
b = float(self._angle2.valeur) |
| 394 |
c = float(self._angle3.valeur) |
| 395 |
if b < a: |
| 396 |
b += 2*pi |
| 397 |
if c < a: |
| 398 |
c += 2*pi |
| 399 |
if b < c: |
| 400 |
return 1 |
| 401 |
return -1 |
| 402 |
|
| 403 |
|
| 404 |
def _conditions_existence(self): |
| 405 |
return self._Arc_generique__centre.existe |
| 406 |
|
| 407 |
@property |
| 408 |
def extremites(self): |
| 409 |
return self.__point1, self.__point3 |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
class Arc_oriente(Arc_points): |
| 414 |
"""Un arc de cercle orienté. |
| 415 |
|
| 416 |
Un arc de cercle orienté, défini par ses extrémités, et un autre point.""" |
| 417 |
|
| 418 |
_style_defaut = param.arcs_orientes |
| 419 |
|
| 420 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 421 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 422 |
point3 = __point3 = Argument("Point_generique", defaut = Point) |
| 423 |
|
| 424 |
def __init__(self, point1 = None, point2 = None, point3 = None, **styles): |
| 425 |
self.__point1 = point1 = Ref(point1) |
| 426 |
self.__point2 = point2 = Ref(point2) |
| 427 |
self.__point3 = point3 = Ref(point3) |
| 428 |
rcs) |
| 429 |
Arc_points.__init__(self, point1, point2, point3, **styles) |
| 430 |
|
| 431 |
def image_par(self, transformation): |
| 432 |
return Arc_oriente(self.__point1.image_par(transformation), self.__point2.image_par(transformation), self.__point3.image_par(transformation)) |
| 433 |
|
| 434 |
@property |
| 435 |
def sens(self): |
| 436 |
if self._sens() == 1: |
| 437 |
return "direct" |
| 438 |
return "indirect" |
| 439 |
|
| 440 |
def _creer_figure(self): |
| 441 |
if not self._representation: |
| 442 |
self._representation = [self.rendu.fleche_courbe()] |
| 443 |
fleche = self._representation[0] |
| 444 |
|
| 445 |
fleche.set(centre=self._Arc_generique__centre.coordonnees, rayon=self.rayon, |
| 446 |
intervalle = self._intervalle(), position=self.style("position"), |
| 447 |
double=self.style("double_fleche"), taille=self.style("taille"), |
| 448 |
angle=self.style("angle"), zorder=self.style("niveau"), |
| 449 |
color=self.style("couleur"), linewidth = self.style("epaisseur"), |
| 450 |
linestyle = self.style("style"), sens=self._sens(), |
| 451 |
) |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
class Demicercle(Arc_cercle): |
| 457 |
"""Un demi-cercle. |
| 458 |
|
| 459 |
Un demi-cercle orienté, défini par ses extrémités, dans le sens direct.""" |
| 460 |
|
| 461 |
|
| 462 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 463 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 464 |
|
| 465 |
def __init__(self, point1 = None, point2 = None, **styles): |
| 466 |
self.__point1 = point1 = Ref(point1) |
| 467 |
self.__point2 = point2 = Ref(point2) |
| 468 |
Arc_cercle.__init__(self, Milieu(point1, point2), point1, point2, **styles) |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
class Cercle_generique(Cercle_Arc_generique): |
| 475 |
"""Un cercle générique. |
| 476 |
|
| 477 |
Usage interne : la classe mère pour tous les types de cercles.""" |
| 478 |
|
| 479 |
_style_defaut = param.cercles |
| 480 |
_prefixe_nom = "c" |
| 481 |
|
| 482 |
centre = __centre = Argument("Point_generique") |
| 483 |
|
| 484 |
def __init__(self, centre, **styles): |
| 485 |
self.__centre = centre = Ref(centre) |
| 486 |
Cercle_Arc_generique.__init__(self, centre, **styles) |
| 487 |
self.etiquette = Label_cercle(self) |
| 488 |
|
| 489 |
|
| 490 |
def _t(self): |
| 491 |
x, y = self.__centre.coordonnees |
| 492 |
xmin, xmax, ymin, ymax = self.feuille.fenetre |
| 493 |
w = 3*(xmax - xmin) |
| 494 |
h = 3*(ymax - ymin) |
| 495 |
if xmin - w < x < xmax + w and ymin - h < y < ymax + h: |
| 496 |
return fullrange(0, 2*pi, self.canvas.pas()) |
| 497 |
else: |
| 498 |
re. |
| 499 |
A = xmin + 1j*ymin |
| 500 |
B = xmax + 1j*ymin |
| 501 |
C = xmax + 1j*ymax |
| 502 |
D = xmin + 1j*ymax |
| 503 |
O = x + 1j*y |
| 504 |
a = phase(A - O) |
| 505 |
b = phase(B - O) |
| 506 |
c = phase(C - O) |
| 507 |
d = phase(D - O) |
| 508 |
if x >= xmax and ymin <= y <= ymax: |
| 509 |
assert (a <= 0 and b <= 0 and c >= 0 and d >= 0) |
| 510 |
a += 2*pi |
| 511 |
b += 2*pi |
| 512 |
return arange(min(a, b, c, d), max(a, b, c, d), self.canvas.pas()) |
| 513 |
|
| 514 |
|
| 515 |
def _creer_figure(self): |
| 516 |
if not self._representation: |
| 517 |
self._representation = [self.rendu.ligne()] |
| 518 |
|
| 519 |
plot = self._representation[0] |
| 520 |
x, y = self.__centre.coordonnees |
| 521 |
r = self.rayon |
| 522 |
t = self._t() |
| 523 |
plot.set_data(x + r*ncos(t), y + r*nsin(t)) |
| 524 |
plot.set(color=self.style("couleur"), linestyle = self.style("style"), |
| 525 |
linewidth=self.style("epaisseur")) |
| 526 |
plot.zorder = self.style("niveau") |
| 527 |
|
| 528 |
|
| 529 |
def _longueur(self): |
| 530 |
r = self.rayon |
| 531 |
if contexte['exact'] and issympy(r): |
| 532 |
return 2*r*PI |
| 533 |
else: |
| 534 |
return 2*r*pi |
| 535 |
|
| 536 |
perimetre = property(_longueur) |
| 537 |
|
| 538 |
|
| 539 |
def _contains(self, M): |
| 540 |
O = self.__centre |
| 541 |
return abs(distance(O, M) - self.rayon) < contexte['tolerance'] |
| 542 |
|
| 543 |
|
| 544 |
def _espace_vital(self): |
| 545 |
x, y = self.__centre.coordonnees |
| 546 |
r = self.rayon |
| 547 |
return (x - r, x + r, y - r, y + r) |
| 548 |
|
| 549 |
|
| 550 |
@property |
| 551 |
def equation_formatee(self): |
| 552 |
"Equation sous forme lisible par l'utilisateur." |
| 553 |
eq = self.equation |
| 554 |
if eq is None: |
| 555 |
return "L'objet n'est pas défini." |
| 556 |
a, b, c = (nice_display(coeff) for coeff in eq) |
| 557 |
e |
| 558 |
return formatage("x² + y² + %s x + %s y + %s = 0" %(a, b, c)) |
| 559 |
|
| 560 |
|
| 561 |
def _creer_nom_latex(self): |
| 562 |
"""Crée le nom formaté en LaTeX. Ex: M1 -> $M_1$.""" |
| 563 |
Objet._creer_nom_latex(self) |
| 564 |
nom = self.latex_police_cursive(self.nom_latex[1:-1]) |
| 565 |
self.nom_latex = "$" + nom + "$" |
| 566 |
|
| 567 |
@property |
| 568 |
def info(self): |
| 569 |
return self.nom_complet + ' de rayon ' + nice_display(self.rayon) |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
class Cercle_rayon(Cercle_generique): |
| 577 |
"""Un cercle de rayon fixé. |
| 578 |
|
| 579 |
Un cercle défini par son centre et son rayon.""" |
| 580 |
|
| 581 |
|
| 582 |
centre = __centre = Argument("Point_generique", defaut = Point) |
| 583 |
rayon = __rayon = Argument("Variable_generique", defaut = 1) |
| 584 |
|
| 585 |
def __init__(self, centre = None, rayon = None, **styles): |
| 586 |
self.__centre = centre = Ref(centre) |
| 587 |
self.__rayon = rayon = Ref(rayon) |
| 588 |
Cercle_generique.__init__(self, centre, **styles) |
| 589 |
|
| 590 |
def image_par(self, transformation): |
| 591 |
from .transformations import Homothetie, Rotation, Translation, Reflexion |
| 592 |
if isinstance(transformation, Homothetie): |
| 593 |
return Cercle_rayon(self.__centre.image_par(transformation), Mul(Rayon(self), transformation.rapport)) |
| 594 |
elif isinstance(transformation, (Rotation, Translation, Reflexion)): |
| 595 |
return Cercle_rayon(self.__centre.image_par(transformation), Rayon(self)) |
| 596 |
raise NotImplementedError |
| 597 |
|
| 598 |
|
| 599 |
def _conditions_existence(self): |
| 600 |
return self.__rayon >= 0 |
| 601 |
|
| 602 |
def _set_feuille(self): |
| 603 |
if "_Cercle__rayon" in self._valeurs_par_defaut: |
| 604 |
xmin, xmax, ymin, ymax = self.feuille.fenetre |
| 605 |
self.__rayon = .5*uniform(0, min(abs(xmin - xmax), abs(ymin - ymax))) |
| 606 |
|
| 607 |
if "_Cercle__centre" in self._valeurs_par_defaut: |
| 608 |
xmin, xmax, ymin, ymax = self.feuille.fenetre |
| 609 |
r = self.__rayon |
| 610 |
self.__centre.coordonnees = uniform(xmin + r, xmax - r), uniform(ymin + r, ymax - r) |
| 611 |
|
| 612 |
Objet._set_feuille(self) |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
class Cercle(Cercle_generique): |
| 617 |
"""Un cercle. |
| 618 |
|
| 619 |
Un cercle défini par son centre et un point du cercle.""" |
| 620 |
|
| 621 |
centre = __centre = Argument("Point_generique", defaut = Point) |
| 622 |
point = __point = Argument("Point_generique", defaut = Point) |
| 623 |
|
| 624 |
def __new__(cls, *args, **kw): |
| 625 |
if len(args) == 1 and isinstance(args[0], str): |
| 626 |
newclass = Cercle_equation |
| 627 |
elif len(args) == 1 and isinstance(args[0], Segment): |
| 628 |
newclass = Cercle_diametre |
| 629 |
elif len(args) == 2 and (isinstance(args[1], TYPES_REELS) |
| 630 |
or isinstance(args[1], str)): |
| 631 |
newclass = Cercle_rayon |
| 632 |
elif len(args) == 3 and isinstance(args[0], Point_generique) \ |
| 633 |
and isinstance(args[1], Point_generique) \ |
| 634 |
and isinstance(args[2], Point_generique): |
| 635 |
newclass = Cercle_points |
| 636 |
else: |
| 637 |
return object.__new__(cls) |
| 638 |
objet = newclass.__new__(newclass, *args, **kw) |
| 639 |
objet.__init__(*args, **kw) |
| 640 |
return objet |
| 641 |
|
| 642 |
def __init__(self, centre = None, point = None, **styles): |
| 643 |
self.__centre = centre = Ref(centre) |
| 644 |
self.__point = point = Ref(point) |
| 645 |
Cercle_generique.__init__(self, centre, **styles) |
| 646 |
|
| 647 |
@property |
| 648 |
def rayon(self): |
| 649 |
return distance(self.__centre, self.__point) |
| 650 |
|
| 651 |
|
| 652 |
def image_par(self, transformation): |
| 653 |
return Cercle(self.__centre.image_par(transformation), self.__point.image_par(transformation)) |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
class Cercle_diametre(Cercle_generique): |
| 659 |
"""Un cercle défini par un diamètre. |
| 660 |
|
| 661 |
Un cercle défini par un diamètre [AB].""" |
| 662 |
|
| 663 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 664 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 665 |
|
| 666 |
def __init__(self, point1 = None, point2 = None, **styles): |
| 667 |
if isinstance(point1, Segment) and point2 is None: |
| 668 |
point2 = point1.point2 |
| 669 |
point1 = point1.point1 |
| 670 |
self.__point1 = point1 = Ref(point1) |
| 671 |
self.__point2 = point2 = Ref(point2) |
| 672 |
Cercle_generique.__init__(self, Milieu(point1, point2), **styles) |
| 673 |
|
| 674 |
@property |
| 675 |
def rayon(self): |
| 676 |
return distance(self._Cercle_generique__centre, self.__point1) |
| 677 |
|
| 678 |
def image_par(self, transformation): |
| 679 |
return Cercle_diametre(self.__point1.image_par(transformation), self.__point2.image_par(transformation)) |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
class Cercle_points(Cercle_generique): |
| 686 |
"""Un cercle défini par 3 points. |
| 687 |
|
| 688 |
Un cercle défini par la donnée de 3 points du cercle.""" |
| 689 |
|
| 690 |
point1 = __point1 = Argument("Point_generique", defaut = Point) |
| 691 |
point2 = __point2 = Argument("Point_generique", defaut = Point) |
| 692 |
point3 = __point3 = Argument("Point_generique", defaut = Point) |
| 693 |
|
| 694 |
def __init__(self, point1 = None, point2 = None, point3 = None, **styles): |
| 695 |
self.__point1 = point1 = Ref(point1) |
| 696 |
self.__point2 = point2 = Ref(point2) |
| 697 |
self.__point3 = point3 = Ref(point3) |
| 698 |
Cercle_generique.__init__(self, Point_equidistant(point1, point2, point3), **styles) |
| 699 |
|
| 700 |
@property |
| 701 |
def rayon(self): |
| 702 |
try: |
| 703 |
return distance(self._Cercle_generique__centre, self.__point1) |
| 704 |
except TypeError: |
| 705 |
return 0 |
| 706 |
|
| 707 |
def _conditions_existence(self): |
| 708 |
return self._Cercle_generique__centre.existe |
| 709 |
|
| 710 |
def image_par(self, transformation): |
| 711 |
return Cercle_points(self.__point1.image_par(transformation), self.__point2.image_par(transformation), self.__point3.image_par(transformation)) |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
class Cercle_equation(Cercle_generique): |
| 719 |
"""Un cercle défini par une équation. |
| 720 |
|
| 721 |
Un cercle d'équation donnée sous forme d'un triplet (a, b, c). (x**2 + y**2 + ax + by + c = 0)""" |
| 722 |
|
| 723 |
a = __a = Argument("Variable_generique") |
| 724 |
b = __b = Argument("Variable_generique") |
| 725 |
c = __c = Argument("Variable_generique") |
| 726 |
|
| 727 |
def __init__(self, a = 0, b = 0, c = -1, **styles): |
| 728 |
self.__a = a = Ref(a) |
| 729 |
self.__b = b = Ref(b) |
| 730 |
self.__c = c = Ref(c) |
| 731 |
Objet.__init__(self) isé |
| 732 |
Cercle_generique.__init__(self, Centre(self), **styles) |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
@property |
| 737 |
def rayon(self): |
| 738 |
return sqrt((self.__a**2 + self.__b**2)/4 - self.__c) |
| 739 |
|
| 740 |
|
| 741 |
def _get_equation(self): |
| 742 |
"Retourne un triplet (a, b, c), tel que x**2 + y**2 + ax + by + c = 0 soit une équation du cercle." |
| 743 |
return self.__a, self.__b, self.__c |
| 744 |
|
| 745 |
def _set_equation(self, a = None, b = 0, c = -1): |
| 746 |
if a is not None: |
| 747 |
self.__a = a |
| 748 |
self.__b = b |
| 749 |
self.__c = c |
| 750 |
|
| 751 |
def _conditions_existence(self): |
| 752 |
return self.__a**2 + self.__b**2 - 4*self.__c >= 0 |
| 753 |
|
| 754 |
|
| 755 |
def image_par(self, transformation): |
| 756 |
return Cercle_rayon(Centre(self).image_par(transformation), Glisseur_cercle(self).image_par(transformation)) |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
class Disque(Cercle_generique): |
| 762 |
"""Un disque. |
| 763 |
|
| 764 |
Un disque défini par le cercle le délimitant.""" |
| 765 |
|
| 766 |
_style_defaut = param.polygones |
| 767 |
_prefixe_nom = "d" |
| 768 |
|
| 769 |
cercle = __cercle = Argument("Cercle_generique", defaut=Cercle) |
| 770 |
|
| 771 |
def __init__(self, cercle = None, **styles): |
| 772 |
styles.setdefault('couleur', cercle.style('couleur')) |
| 773 |
self.__cercle = cercle = Ref(cercle) |
| 774 |
Cercle_generique.__init__(self, Centre(cercle), **styles) |
| 775 |
|
| 776 |
def _creer_figure(self): |
| 777 |
if not self._representation: |
| 778 |
self._representation = [self.rendu.polygone()] |
| 779 |
|
| 780 |
fill = self._representation[0] |
| 781 |
x, y = self._Cercle_generique__centre.coordonnees |
| 782 |
r = self.rayon |
| 783 |
t = fullrange(0, 2*pi , self.canvas.pas()) |
| 784 |
fill.set(xy = list(zip(x + r*ncos(t), y + r*nsin(t))), |
| 785 |
alpha = self.style("alpha"), color = self.style("couleur"), |
| 786 |
linestyle = FILL_STYLES.get(self.style("style"), "solid"), |
| 787 |
linewidth = self.style("epaisseur"), |
| 788 |
zorder = self.style("niveau")) |
| 789 |
|
| 790 |
|
| 791 |
@property |
| 792 |
def rayon(self): |
| 793 |
return self.__cercle.rayon |
| 794 |
|
| 795 |
def _distance_inf(self, x, y, d): |
| 796 |
x, y = self.feuille.pix2coo(x, y) |
| 797 |
return distance(self._Cercle_generique__centre, (x, y)) <= self.rayon |
| 798 |
|
| 799 |
def _contains(self, M): |
| 800 |
return distance(self._Cercle_generique__centre, M) - self.rayon <= contexte['tolerance'] |
| 801 |
|
| 802 |
@property |
| 803 |
def aire(self): |
| 804 |
r = self.rayon |
| 805 |
if contexte['exact'] and issympy(r): |
| 806 |
return r**2*PI |
| 807 |
else: |
| 808 |
return r**2*pi |
| 809 |
|
| 810 |
@property |
| 811 |
def info(self): |
| 812 |
return self.nom_complet + " d'aire " + nice_display(self.aire) |
| 813 |
|
| 814 |
|
| 815 |
@property |
| 816 |
def equation_formatee(self): |
| 817 |
"Equation sous forme lisible par l'utilisateur." |
| 818 |
eq = self.equation |
| 819 |
if eq is None: |
| 820 |
return "L'objet n'est pas défini." |
| 821 |
a, b, c = (nice_display(coeff) for coeff in eq) e |
| 822 |
return formatage("x² + y² + %s x + %s y + %s <= 0" %(a, b, c)) |
| 823 |
|
| 824 |
def image_par(self, transformation): |
| 825 |
return Disque(self.__cercle.image_par(transformation)) |