| 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 randint, choice |
| 26 |
from itertools import chain |
| 27 |
from functools import partial |
| 28 |
|
| 29 |
from PyQt5.QtGui import QColor, QCursor |
| 30 |
from PyQt5.QtWidgets import QVBoxLayout, QLabel, QPushButton, QHBoxLayout, \ |
| 31 |
QMessageBox, QTextEdit, QToolTip |
| 32 |
|
| 33 |
from sympy import S, solve, gcd, simplify |
| 34 |
from sympy.core.sympify import SympifyError |
| 35 |
|
| 36 |
from ...GUI.menu import MenuBar |
| 37 |
from ...GUI.panel import Panel_API_graphique |
| 38 |
from ...GUI.proprietes_objets import Proprietes |
| 39 |
from ...geolib import Segment, Texte, Point, Droite, Champ |
| 40 |
from ...geolib.routines import nice_str, det, vect |
| 41 |
from ...pylib import OrderedDict, print_error |
| 42 |
from ...mathlib.parsers import convertir_en_latex, traduire_formule |
| 43 |
from ... import param |
| 44 |
from ...GUI.exercice import ExerciceMenuBar, Exercice |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class TabMenuBar(MenuBar): |
| 51 |
def __init__(self, panel): |
| 52 |
MenuBar.__init__(self, panel) |
| 53 |
|
| 54 |
self.ajouter("Fichier", ["Recommencer", "Recommencer au niveau 0.", "Ctrl+N", panel.reinitialiser], |
| 55 |
["ouvrir"], |
| 56 |
["enregistrer"], ["enregistrer_sous"], ["exporter"], |
| 57 |
["exporter&sauver"], None, ["imprimer"], ["presse-papier"], |
| 58 |
None, ["proprietes"], None, ["fermer"], ["quitter"]) |
| 59 |
self.ajouter("Editer", ["annuler"], ["refaire"], ["modifier"], ["supprimer"]) |
| 60 |
self.ajouter("Affichage", ["onglet"], ["plein_ecran"], None, ["zoom_texte"], ["zoom_ligne"], ["zoom_general"]) |
| 61 |
self.ajouter("Outils", ["options"]) |
| 62 |
self.ajouter("avance1") |
| 63 |
self.ajouter("?") |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class ExercicesTrigonometrie(Panel_API_graphique): |
| 68 |
|
| 69 |
titre = "Trigonométrie"e |
| 70 |
|
| 71 |
def __init__(self, *args, **kw): |
| 72 |
Panel_API_graphique.__init__(self, *args, **kw) |
| 73 |
self.canvas.fixe = True |
| 74 |
|
| 75 |
self.entrees = QVBoxLayout() |
| 76 |
self.entrees.addSpacing(30) |
| 77 |
|
| 78 |
self.panneau = QLabel('') |
| 79 |
self.entrees.addWidget(self.panneau) |
| 80 |
|
| 81 |
self.entrees.addStretch() |
| 82 |
self.felicitations = QLabel('') |
| 83 |
self.entrees.addWidget(self.felicitations) |
| 84 |
|
| 85 |
self.entrees.addSpacing(30) |
| 86 |
self.btn_niveau = QPushButton("Niveau suivant", self) |
| 87 |
self.btn_niveau.clicked.connect(self.niveau_suivant) |
| 88 |
self.entrees.addWidget(self.btn_niveau) |
| 89 |
self.entrees.addSpacing(50) |
| 90 |
|
| 91 |
self.sizer = QHBoxLayout() |
| 92 |
self.sizer.addWidget(self.canvas, 1) |
| 93 |
self.sizer.addLayout(self.entrees, 0.2) |
| 94 |
self.finaliser(contenu=self.sizer) |
| 95 |
|
| 96 |
self.reinitialiser() |
| 97 |
|
| 98 |
|
| 99 |
def reinitialiser(self): |
| 100 |
if param.debug: |
| 101 |
print('Module %s: réinitialisation...' % self.nom) |
| 102 |
self.score = 0 |
| 103 |
self.niveau = 0 |
| 104 |
self.erreurs = 0 |
| 105 |
self.niveau_suivant() |
| 106 |
|
| 107 |
|
| 108 |
def niveau_suivant(self, niveau=None): |
| 109 |
|
| 110 |
|
| 111 |
self.fermer_feuilles() |
| 112 |
t: |
| 113 |
self.canvas.fenetre = -8, 8, -10, 8 |
| 114 |
self.canvas.afficher_axes = True |
| 115 |
self.canvas.quadrillage_defaut() |
| 116 |
self.canvas.ratio = None |
| 117 |
self.canvas.repere = ('O', 'i', 'j') |
| 118 |
self.afficher_barre_outils(False) |
| 119 |
e] |
| 120 |
self.canvas.editeur.actif = False |
| 121 |
t |
| 122 |
self.canvas.edition_par_clic_droit = False |
| 123 |
|
| 124 |
if niveau in (None, False): |
| 125 |
) |
| 126 |
self.niveau += 1 |
| 127 |
else: |
| 128 |
self.niveau = niveau |
| 129 |
if param.debug: |
| 130 |
print("== Niveau %s ==" % self.niveau) |
| 131 |
getattr(self, 'niveau%s' % self.niveau)() |
| 132 |
|
| 133 |
self.btn_niveau.setEnabled(False) |
| 134 |
self.felicitations.setStyleSheet( |
| 135 |
"""QLabel {background-color: white; padding: 5px; border-radius: 5px; |
| 136 |
color:white;}""") |
| 137 |
self.update_panneau() |
| 138 |
|
| 139 |
n = niveau_suivant |
| 140 |
|
| 141 |
|
| 142 |
te |
| 143 |
|
| 144 |
|
| 145 |
def cercle_trigo(self): |
| 146 |
"""Construction d'un cercle trigonométrique. |
| 147 |
""" |
| 148 |
obj = self.feuille_actuelle.objets |
| 149 |
obj['O'] = O = Point(0, 0, fixe=True) |
| 150 |
obj['I'] = I = Point(1, 0, fixe=True) |
| 151 |
obj['c'] = c = Cercle(O, I) |
| 152 |
for i in range(1, 8): |
| 153 |
a = i*pi/4 |
| 154 |
M = Point(cos(a), sin(a), fixe=True) |
| 155 |
for i in range(1, 12): |
| 156 |
if i != 6: |
| 157 |
a = i*pi/6 |
| 158 |
M = Point(cos(a), sin(a), fixe=True) |
| 159 |
self.feuille_actuelle.objets['B'] = B |
| 160 |
! |
| 161 |
d = Droite(A, B, afficher_info=False) |
| 162 |
self.feuille_actuelle.objets['d'] = d |
| 163 |
if xA == xB: |
| 164 |
reponse = 'x=' + str(xA) |
| 165 |
else: |
| 166 |
reponse = ('y=%s*x+%s' % self.eq_reduite(pointA, pointB)) |
| 167 |
xmin, xmax, ymin, ymax = self.canvas.fenetre |
| 168 |
print('Fenetre::', self.canvas.fenetre, '--', xmin, ymin) |
| 169 |
champ = Champ('', xmin, ymin, fond=True, couleur_fond='#ffffb5', |
| 170 |
prefixe=(r"Dans le repère $(O;\,\vec\imath,\,\vec\jmath)$, " |
| 171 |
"la droite $(AB)$ a pour équation "), |
| 172 |
alignement_horizontal='left', alignement_vertical='bottom', |
| 173 |
attendu=reponse) |
| 174 |
print('xy::', champ.xy) |
| 175 |
champ.valider = self.valider_eq |
| 176 |
champ.evt_valider = self.compter_points |
| 177 |
self.feuille_actuelle.objets['champ1'] = champ |
| 178 |
|
| 179 |
|
| 180 |
def niveau1(self): |
| 181 |
yA = self.relatif(7) |
| 182 |
if yA > 0: |
| 183 |
yB = yA - randint(1, 3) |
| 184 |
else: |
| 185 |
yB = yA + randint(1, 3) |
| 186 |
self.exercice_lire_equation_AB((0, yA), (1, yB)) |
| 187 |
|
| 188 |
|
| 189 |
def niveau2(self): |
| 190 |
yA = self.relatif(7) |
| 191 |
if yA > 0: |
| 192 |
yB = yA - choice([1, 3, 5]) |
| 193 |
else: |
| 194 |
yB = yA + choice([1, 3, 5]) |
| 195 |
self.exercice_lire_equation_AB((0, yA), (-2, yB)) |
| 196 |
|
| 197 |
|
| 198 |
def niveau3(self): |
| 199 |
while True: |
| 200 |
yA = self.relatif(7) |
| 201 |
yB = self.relatif(7) |
| 202 |
if abs(yB - yA) > 5: |
| 203 |
break |
| 204 |
while True: |
| 205 |
xB = self.relatif(7) |
| 206 |
if abs(xB) > 5: |
| 207 |
break |
| 208 |
self.exercice_lire_equation_AB((0, yA), (xB, yB)) |
| 209 |
|
| 210 |
|
| 211 |
def niveau4(self): |
| 212 |
"""Droite horizontale.""" |
| 213 |
yA = self.relatif(7) |
| 214 |
while True: |
| 215 |
xA = self.relatif(7) |
| 216 |
xB = self.relatif(7) |
| 217 |
if abs(xB - xA) > 5: |
| 218 |
break |
| 219 |
self.exercice_lire_equation_AB((xA, yA), (xB, yA)) |
| 220 |
|
| 221 |
|
| 222 |
def niveau5(self): |
| 223 |
"""Droite verticale.""" |
| 224 |
xA = self.relatif(7) |
| 225 |
while True: |
| 226 |
yA = self.relatif(7) |
| 227 |
yB = self.relatif(7) |
| 228 |
if abs(yB - yA) > 5: |
| 229 |
break |
| 230 |
self.exercice_lire_equation_AB((xA, yA), (xA, yB)) |
| 231 |
|
| 232 |
|
| 233 |
def niveau6(self, n=7): |
| 234 |
"""Droite oblique ne coupant pas l'axe des ordonnées sur une |
| 235 |
graduation ; il faut donc calculer (ou deviner) l'ordonnée |
| 236 |
à l'origine.""" |
| 237 |
for i in range(1000): |
| 238 |
while True: |
| 239 |
xA = self.relatif(n) |
| 240 |
xB = self.relatif(n) |
| 241 |
if abs(xB - xA) > 5: |
| 242 |
break |
| 243 |
while True: |
| 244 |
yA = self.relatif(7) |
| 245 |
yB = self.relatif(7) |
| 246 |
if abs(yB - yA) > 5: |
| 247 |
break |
| 248 |
). |
| 249 |
a, b = self.eq_reduite((xA, yA), (xB, yB)) |
| 250 |
print(b) |
| 251 |
if b.q not in (1, 2): |
| 252 |
u 2. |
| 253 |
break |
| 254 |
self.exercice_lire_equation_AB((xA, yA), (xB, yB)) |
| 255 |
|
| 256 |
|
| 257 |
def niveau7(self): |
| 258 |
self.canvas.ratio = 4 |
| 259 |
self.canvas.quadrillages = (((.25, 1), ':', 0.5, 'k'),) |
| 260 |
self.canvas.fenetre = -4.5, 4.5, -10, 8 |
| 261 |
print(self.canvas.fenetre) |
| 262 |
self.niveau6(n=4) |
| 263 |
self.feuille_actuelle.objets['champ1'].y = -9 |
| 264 |
|
| 265 |
|
| 266 |
def valider_eq(self, reponse, attendu): |
| 267 |
reponse = traduire_formule(reponse) |
| 268 |
assert attendu.count('=') == 1 |
| 269 |
if reponse.count('=') != 1: |
| 270 |
return False |
| 271 |
g, d = attendu.split('=') |
| 272 |
attendu = '%s-(%s)' %(g, d) |
| 273 |
g, d = reponse.split('=') |
| 274 |
reponse = '%s-(%s)' %(g, d) |
| 275 |
reponse = reponse.replace(',', '.') |
| 276 |
quotient = '(%s)/(%s)' % (reponse, attendu) |
| 277 |
try: |
| 278 |
». |
| 279 |
return not simplify(S(quotient)).free_symbols |
| 280 |
except (SympifyError, ValueError): |
| 281 |
pass |
| 282 |
except Exception: |
| 283 |
print_error() |
| 284 |
return False |
| 285 |
|
| 286 |
|
| 287 |
def eq_reduite(self, A, B): |
| 288 |
"""Équation réduite exacte de la droite (AB). |
| 289 |
|
| 290 |
La droite ne doit pas être verticale.""" |
| 291 |
xA, yA = A |
| 292 |
xB, yB = B |
| 293 |
assert xA != xB, "Droite verticale !" |
| 294 |
a = S(yB - yA)/(xB - xA) |
| 295 |
b = yA - a*xA |
| 296 |
return a, b |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
me |
| 301 |
|
| 302 |
|
| 303 |
def niveau8(self): |
| 304 |
"""Résolution graphique de système. |
| 305 |
|
| 306 |
Construire deux droites d'équations données. |
| 307 |
Lire les coordonnées du point d'intersection.""" |
| 308 |
self.canvas.fenetre = -8, 8, -10, 11 |
| 309 |
self.canvas.afficher_axes = True |
| 310 |
self.canvas.quadrillage_defaut() |
| 311 |
self.afficher_barre_outils(True) |
| 312 |
self.canvas.grille_aimantee = True |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
C = self.couple() |
| 318 |
nt |
| 319 |
|
| 320 |
|
| 321 |
; |
| 322 |
, |
| 323 |
|
| 324 |
while True: |
| 325 |
A = self.couple() |
| 326 |
B = self.couple() |
| 327 |
s |
| 328 |
if det(vect(A, C), vect(B, C)) == 0: |
| 329 |
continue |
| 330 |
|
| 331 |
if all((A[0] - C[0], A[1] - C[1], B[0] - C[0], B[1] - C[1])): |
| 332 |
break |
| 333 |
if param.debug: |
| 334 |
print('A,B,C:', A, B, C) |
| 335 |
ite |
| 336 |
x = S('x') |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
def format(eq): |
| 345 |
eq = eq.replace('=1*x', '=x').replace('=-1*x', '=-x').replace('+0', '') |
| 346 |
eq = convertir_en_latex(eq) |
| 347 |
return eq |
| 348 |
a, b = self.eq_reduite(A, C) |
| 349 |
eq1 = format('y=%s*x+%s' % (a, b)) |
| 350 |
c, d = self.eq_reduite(B, C) |
| 351 |
eq2 = format('y=%s*x+%s' % (c, d)) |
| 352 |
|
| 353 |
xmin, xmax, ymin, ymax = self.canvas.fenetre |
| 354 |
|
| 355 |
txt = Texte(("On note $d_1$ la droite d'équation %s, " |
| 356 |
"et $d_2$ la droite d'équation %s.\n" |
| 357 |
"Construire les droites $d_1$ puis $d_2$ dans le repère ci-dessous.") |
| 358 |
% (eq1, eq2), xmin, ymax, fond=True, couleur_fond='#ffffb5', fixe=True, |
| 359 |
alignement_horizontal='left', alignement_vertical='top') |
| 360 |
self.feuille_actuelle.objets['txt1'] = txt |
| 361 |
champ = Champ('', xmin, ymin, prefixe="Le couple solution du système est (", |
| 362 |
alignement_vertical='bottom', alignement_horizontal='left', |
| 363 |
attendu=str(C), fond=True, couleur_fond='#ffffb5', suffixe=')', |
| 364 |
) |
| 365 |
self.feuille_actuelle.objets['champ1'] = champ |
| 366 |
champ.valider = self.valider_couple |
| 367 |
champ.evt_valider = self.compter_points |
| 368 |
ch1 = Champ('', visible=False, attendu='ok') |
| 369 |
self.feuille_actuelle.objets['champ_cache_d1'] = ch1 |
| 370 |
ch1.evt_valider = self.compter_points |
| 371 |
ch2 = Champ('', visible=False, attendu='ok') |
| 372 |
ch2.evt_valider = self.compter_points |
| 373 |
self.feuille_actuelle.objets['champ_cache_d2'] = ch2 |
| 374 |
self.feuille_actuelle.lier(partial(self.verifier_feuille, eq1=(a, b), eq2=(c, d))) |
| 375 |
|
| 376 |
|
| 377 |
def verifier_feuille(self, eq1, eq2): |
| 378 |
print(eq1, eq2) |
| 379 |
for nom, eq in (('d1', eq1), ('d2', eq2)): |
| 380 |
if nom in self.feuille_actuelle.objets.noms: |
| 381 |
d = self.feuille_actuelle.objets[nom] |
| 382 |
d.label(mode='nom') |
| 383 |
champ = self.feuille_actuelle.objets['champ_cache_' + nom] |
| 384 |
M, N = d |
| 385 |
M = (int(M.x), int(M.y)) |
| 386 |
N = (int(N.x), int(N.y)) |
| 387 |
if self.eq_reduite(M, N) == eq: |
| 388 |
d.style(couleur='g') |
| 389 |
champ.texte = 'ok' |
| 390 |
msg = 'La droite %s est correcte.' % nom |
| 391 |
if nom == 'd1': |
| 392 |
msg += ' Construisez maintenant d2.' |
| 393 |
else: |
| 394 |
print(self.eq_reduite(*d), eq) |
| 395 |
d.style(couleur='r') |
| 396 |
s |
| 397 |
is |
| 398 |
r |
| 399 |
|
| 400 |
|
| 401 |
champ.texte = str(d.equation) |
| 402 |
msg = "Attention, la droite %s est fausse." % nom |
| 403 |
QToolTip.showText(QCursor.pos(), msg) |
| 404 |
self.canvas.message(msg, temporaire=False) |
| 405 |
|
| 406 |
|
| 407 |
def valider_couple(self, reponse, attendu): |
| 408 |
reponse = reponse.replace(';', ',') |
| 409 |
try: |
| 410 |
if S(reponse) == S(attendu): |
| 411 |
return True |
| 412 |
except Exception: |
| 413 |
print_error() |
| 414 |
return False |
| 415 |
|
| 416 |
|
| 417 |
def ax_b(self): |
| 418 |
"Générer une expression sympy de la forme ax+b, avec a, b dans Z." |
| 419 |
return self.relatif()*S('x') + self.relatif() |
| 420 |
|
| 421 |
|
| 422 |
def update_panneau(self): |
| 423 |
self.panneau.setStyleSheet( |
| 424 |
"""QLabel { padding: 10px; border-width: 2px; border-style:solid; |
| 425 |
border-radius: 5px; border-color:%s; background-color: %s }""" |
| 426 |
%(QColor(30, 144, 255).name(), QColor(176, 226, 255).name()) |
| 427 |
) |
| 428 |
self.panneau.setText(("<p><b><i>Niveau :</i> %s</b></p>" % self.niveau) + |
| 429 |
("<p><b><i>Points :</i> %s</b></p>" % self.score) + |
| 430 |
("<p><i>Erreurs :</i> %s</p>" % self.erreurs)) |
| 431 |
champs = self.feuille_actuelle.objets.lister(type=Champ) |
| 432 |
if champs and all(obj.correct for obj in champs): |
| 433 |
if hasattr(self, 'niveau' + str(self.niveau + 1)): |
| 434 |
self.btn_niveau.setEnabled(True) |
| 435 |
self.btn_niveau.setFocus(True) |
| 436 |
self.felicitations.setText('<p><b>Félicitations !</b></p>' + |
| 437 |
'<p>Passer au niveau %s</p>' %(self.niveau + 1)) |
| 438 |
self.felicitations.setStyleSheet( |
| 439 |
"""QLabel {background-color: %s; padding: 5px; |
| 440 |
border-radius: 5px; |
| 441 |
color:white;}""" %QColor(255, 153, 0).name()) |
| 442 |
|
| 443 |
else: |
| 444 |
self.felicitations.setText('<p><b>Félicitations !</b></p>' + |
| 445 |
'<p>Dernier niveau terminé !</p>') |
| 446 |
self.felicitations.setStyleSheet( |
| 447 |
"""QLabel {background-color: %s; padding: 5px; border-radius: 5px; |
| 448 |
color:white;}""" %QColor(102, 205, 0).name()) |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
def _ouvrir(self, fgeo): |
| 458 |
pass |
| 459 |
. |
| 460 |
l. |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
res |
| 480 |
|
| 481 |
|
| 482 |
@staticmethod |
| 483 |
def signe(): |
| 484 |
return 2*randint(0, 1) - 1 |
| 485 |
|
| 486 |
def naturel(self, n=15): |
| 487 |
'''Retourne un entier entre 2 et `n`.''' |
| 488 |
return randint(2, n) |
| 489 |
|
| 490 |
def relatif(self, n=15): |
| 491 |
'''Retourne un entier entre -`n` et -2, ou entre 2 et `n`.''' |
| 492 |
|
| 493 |
signe = 2*randint(0, 1) - 1 |
| 494 |
return self.signe()*self.naturel(n) |
| 495 |
|
| 496 |
def rationnel(self, n=7): |
| 497 |
'''Retourne un quotient d'entiers.''' |
| 498 |
while True: |
| 499 |
p = self.naturel(n) |
| 500 |
q = self.naturel(n) |
| 501 |
if p%q: |
| 502 |
break |
| 503 |
return self.signe()*S(p)/S(q) |
| 504 |
|
| 505 |
def couple(self, m=7, n=7): |
| 506 |
"""Retourne un couple d'entiers relatifs.""" |
| 507 |
return self.relatif(m), self.relatif(n) |
| 508 |
|
| 509 |
def autocompleter(self): |
| 510 |
|
| 511 |
|
| 512 |
self.btn_niveau.click() |
| 513 |
for t in self.feuille_actuelle.objets.lister(type=Champ): |
| 514 |
t.texte = t.style('attendu', color='g') |
| 515 |
|
| 516 |
|
| 517 |
a = property(autocompleter) |
| 518 |
|
| 519 |
def compter_points(self, **kw): |
| 520 |
if 'correct' in kw and 'correct_old' in kw and 'champ' in kw: |
| 521 |
if kw['correct']: |
| 522 |
if not kw['correct_old']: |
| 523 |
if not kw['champ'].style('choix'): |
| 524 |
é |
| 525 |
self.score += 1 |
| 526 |
self.score += 1 |
| 527 |
else: |
| 528 |
self.score -= 1 |
| 529 |
self.erreurs += 1 |
| 530 |
if all(obj.correct for obj in self.feuille_actuelle.objets.lister(type=Champ)): |
| 531 |
self.score += 10*(self.niveau + 1) |
| 532 |
self.update_panneau() |