| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
| |
| 5 |
|
| 6 |
r |
| 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.exercice import ExerciceMenuBar, Exercice |
| 39 |
from ...GUI.proprietes_objets import Proprietes |
| 40 |
from ...geolib import Segment, Texte, Point, Droite, Champ |
| 41 |
from ...geolib.routines import nice_str, det, vect |
| 42 |
from ...pylib import OrderedDict, print_error |
| 43 |
from ...mathlib.parsers import convertir_en_latex, traduire_formule |
| 44 |
from ... import param |
| 45 |
from .barre_outils_ex_eq_dte import BarreOutilsExEqDte |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class EqMenuBar(ExerciceMenuBar): |
| 51 |
pass |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
class ExercicesEquationsDroites(Exercice): |
| 56 |
|
| 57 |
titre = "Équations de droites"e |
| 58 |
|
| 59 |
def __init__(self, *args, **kw): |
| 60 |
Exercice.__init__(self, BarreOutils = BarreOutilsExEqDte, *args, **kw) |
| 61 |
|
| 62 |
|
| 63 |
def reinitialiser(self): |
| 64 |
"""Paramètres par défaut.""" |
| 65 |
Exercice.reinitialiser(self) |
| 66 |
self.canvas.afficher_axes = True |
| 67 |
self.canvas.quadrillage_defaut() |
| 68 |
self.canvas.repere = ('O', 'i', 'j') |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
te |
| 74 |
|
| 75 |
|
| 76 |
def exercice_lire_equation_AB(self, pointA, pointB): |
| 77 |
"""Lire graphiquement l'équation de la droite (AB). |
| 78 |
|
| 79 |
C'est l'exercice à la base des niveaux 1 à 7. |
| 80 |
""" |
| 81 |
self.canvas.fenetre = -8, 8, -10, 8 |
| 82 |
xA, yA = pointA |
| 83 |
xB, yB = pointB |
| 84 |
A = Point(*pointA, fixe=True) |
| 85 |
B = Point(*pointB, fixe=True) |
| 86 |
self.feuille_actuelle.objets['A'] = A |
| 87 |
self.feuille_actuelle.objets['B'] = B |
| 88 |
! |
| 89 |
d = Droite(A, B, afficher_info=False) |
| 90 |
self.feuille_actuelle.objets['d'] = d |
| 91 |
if xA == xB: |
| 92 |
reponse = 'x=' + str(xA) |
| 93 |
else: |
| 94 |
reponse = ('y=%s*x+%s' % self.eq_reduite(pointA, pointB)) |
| 95 |
print(('fen::', self.canvas.fenetre)) |
| 96 |
xmin, xmax, ymin, ymax = self.canvas.fenetre |
| 97 |
print('Fenetre::', self.canvas.fenetre, '--', xmin, ymin) |
| 98 |
champ = Champ('', xmin, ymin, couleur_fond='#ffffb5', |
| 99 |
prefixe=(r"Dans le repère $(O;\,\vec\imath,\,\vec\jmath)$, " |
| 100 |
"la droite $(AB)$ a pour équation "), |
| 101 |
alignement_horizontal='left', alignement_vertical='bottom', |
| 102 |
attendu=reponse) |
| 103 |
print('xy::', champ.xy) |
| 104 |
champ.valider = self.valider_eq |
| 105 |
champ.evt_valider = self.compter_points |
| 106 |
self.feuille_actuelle.objets['champ1'] = champ |
| 107 |
|
| 108 |
|
| 109 |
def niveau1(self): |
| 110 |
yA = self.relatif(7) |
| 111 |
if yA > 0: |
| 112 |
yB = yA - randint(1, 3) |
| 113 |
else: |
| 114 |
yB = yA + randint(1, 3) |
| 115 |
self.exercice_lire_equation_AB((0, yA), (1, yB)) |
| 116 |
|
| 117 |
|
| 118 |
def niveau2(self): |
| 119 |
yA = self.relatif(7) |
| 120 |
if yA > 0: |
| 121 |
yB = yA - choice([1, 3, 5]) |
| 122 |
else: |
| 123 |
yB = yA + choice([1, 3, 5]) |
| 124 |
self.exercice_lire_equation_AB((0, yA), (-2, yB)) |
| 125 |
|
| 126 |
|
| 127 |
def niveau3(self): |
| 128 |
while True: |
| 129 |
yA = self.relatif(7) |
| 130 |
yB = self.relatif(7) |
| 131 |
if abs(yB - yA) > 5: |
| 132 |
break |
| 133 |
while True: |
| 134 |
xB = self.relatif(7) |
| 135 |
if abs(xB) > 5: |
| 136 |
break |
| 137 |
self.exercice_lire_equation_AB((0, yA), (xB, yB)) |
| 138 |
|
| 139 |
|
| 140 |
def niveau4(self): |
| 141 |
"""Droite horizontale.""" |
| 142 |
yA = self.relatif(7) |
| 143 |
while True: |
| 144 |
xA = self.relatif(7) |
| 145 |
xB = self.relatif(7) |
| 146 |
if abs(xB - xA) > 5: |
| 147 |
break |
| 148 |
self.exercice_lire_equation_AB((xA, yA), (xB, yA)) |
| 149 |
|
| 150 |
|
| 151 |
def niveau5(self): |
| 152 |
"""Droite verticale.""" |
| 153 |
xA = self.relatif(7) |
| 154 |
while True: |
| 155 |
yA = self.relatif(7) |
| 156 |
yB = self.relatif(7) |
| 157 |
if abs(yB - yA) > 5: |
| 158 |
break |
| 159 |
self.exercice_lire_equation_AB((xA, yA), (xA, yB)) |
| 160 |
|
| 161 |
|
| 162 |
def niveau6(self, n=7): |
| 163 |
"""Droite oblique ne coupant pas l'axe des ordonnées sur une |
| 164 |
graduation ; il faut donc calculer (ou deviner) l'ordonnée |
| 165 |
à l'origine.""" |
| 166 |
for i in range(1000): |
| 167 |
while True: |
| 168 |
xA = self.relatif(n) |
| 169 |
xB = self.relatif(n) |
| 170 |
if abs(xB - xA) > 5: |
| 171 |
break |
| 172 |
while True: |
| 173 |
yA = self.relatif(7) |
| 174 |
yB = self.relatif(7) |
| 175 |
if abs(yB - yA) > 5: |
| 176 |
break |
| 177 |
). |
| 178 |
a, b = self.eq_reduite((xA, yA), (xB, yB)) |
| 179 |
print(b) |
| 180 |
if b.q not in (1, 2): |
| 181 |
u 2. |
| 182 |
break |
| 183 |
self.exercice_lire_equation_AB((xA, yA), (xB, yB)) |
| 184 |
|
| 185 |
|
| 186 |
def niveau7(self): |
| 187 |
self.niveau6(n=4) |
| 188 |
self.canvas.ratio = 4 |
| 189 |
self.canvas.quadrillages = (((.25, 1), ':', 0.5, 'k'),) |
| 190 |
self.canvas.fenetre = -4.5, 4.5, -10, 8 |
| 191 |
print(('fen::', self.canvas.fenetre)) |
| 192 |
xmin, xmax, ymin, ymax = self.canvas.fenetre |
| 193 |
self.feuille_actuelle.objets['champ1'].xy = xmin, ymin |
| 194 |
|
| 195 |
|
| 196 |
def valider_eq(self, reponse, attendu): |
| 197 |
reponse = traduire_formule(reponse) |
| 198 |
assert attendu.count('=') == 1 |
| 199 |
if reponse.count('=') != 1: |
| 200 |
return False |
| 201 |
g, d = attendu.split('=') |
| 202 |
attendu = '%s-(%s)' %(g, d) |
| 203 |
g, d = reponse.split('=') |
| 204 |
reponse = '%s-(%s)' %(g, d) |
| 205 |
reponse = reponse.replace(',', '.') |
| 206 |
quotient = '(%s)/(%s)' % (reponse, attendu) |
| 207 |
try: |
| 208 |
». |
| 209 |
return not simplify(S(quotient)).free_symbols |
| 210 |
except (SympifyError, ValueError): |
| 211 |
pass |
| 212 |
except Exception: |
| 213 |
print_error() |
| 214 |
return False |
| 215 |
|
| 216 |
|
| 217 |
def eq_reduite(self, A, B): |
| 218 |
"""Équation réduite exacte de la droite (AB). |
| 219 |
|
| 220 |
La droite ne doit pas être verticale.""" |
| 221 |
xA, yA = A |
| 222 |
xB, yB = B |
| 223 |
assert xA != xB, "Droite verticale !" |
| 224 |
a = S(yB - yA)/(xB - xA) |
| 225 |
b = yA - a*xA |
| 226 |
return a, b |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
me |
| 231 |
|
| 232 |
|
| 233 |
def niveau8(self): |
| 234 |
"""Résolution graphique de système. |
| 235 |
|
| 236 |
Construire deux droites d'équations données. |
| 237 |
Lire les coordonnées du point d'intersection.""" |
| 238 |
self.canvas.fenetre = -8, 8, -10, 11 |
| 239 |
self.canvas.afficher_axes = True |
| 240 |
self.canvas.quadrillage_defaut() |
| 241 |
self.afficher_barre_outils(True) |
| 242 |
self.canvas.grille_aimantee = True |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
C = self.couple() |
| 248 |
nt |
| 249 |
|
| 250 |
|
| 251 |
; |
| 252 |
, |
| 253 |
|
| 254 |
while True: |
| 255 |
A = self.couple() |
| 256 |
B = self.couple() |
| 257 |
s |
| 258 |
if det(vect(A, C), vect(B, C)) == 0: |
| 259 |
continue |
| 260 |
|
| 261 |
if all((A[0] - C[0], A[1] - C[1], B[0] - C[0], B[1] - C[1])): |
| 262 |
break |
| 263 |
if param.debug: |
| 264 |
print('A,B,C:', A, B, C) |
| 265 |
ite |
| 266 |
x = S('x') |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
def format(eq): |
| 275 |
eq = eq.replace('=1*x', '=x').replace('=-1*x', '=-x').replace('+0', '') |
| 276 |
eq = convertir_en_latex(eq) |
| 277 |
return eq |
| 278 |
a, b = self.eq_reduite(A, C) |
| 279 |
eq1 = format('y=%s*x+%s' % (a, b)) |
| 280 |
c, d = self.eq_reduite(B, C) |
| 281 |
eq2 = format('y=%s*x+%s' % (c, d)) |
| 282 |
|
| 283 |
xmin, xmax, ymin, ymax = self.canvas.fenetre |
| 284 |
|
| 285 |
txt = Texte(("On note $d_1$ la droite d'équation %s, " |
| 286 |
"et $d_2$ la droite d'équation %s.\n" |
| 287 |
"Construire les droites $d_1$ puis $d_2$ dans le repère ci-dessous.") |
| 288 |
% (eq1, eq2), "xmin", "ymax", couleur_fond='#ffffb5', fixe=True, |
| 289 |
alignement_horizontal='left', alignement_vertical='top') |
| 290 |
self.feuille_actuelle.objets['txt1'] = txt |
| 291 |
systeme = r'$\left\{\stackrel{%s}{%s}\right.$' % (eq1.strip('$'), eq2.strip('$')) |
| 292 |
champ = Champ('', "xmin", "ymin", |
| 293 |
prefixe=("Le couple solution du système %s est (" % systeme), |
| 294 |
alignement_vertical='bottom', alignement_horizontal='left', |
| 295 |
attendu=str(C), couleur_fond='#ffffb5', suffixe=')') |
| 296 |
self.feuille_actuelle.objets['champ1'] = champ |
| 297 |
champ.valider = self.valider_couple |
| 298 |
champ.evt_valider = self.compter_points |
| 299 |
ch1 = Champ('', visible=False, attendu='ok') |
| 300 |
self.feuille_actuelle.objets['champcache_d1'] = ch1 |
| 301 |
ch1.evt_valider = self.compter_points |
| 302 |
ch2 = Champ('', visible=False, attendu='ok') |
| 303 |
ch2.evt_valider = self.compter_points |
| 304 |
self.feuille_actuelle.objets['champcache_d2'] = ch2 |
| 305 |
self.feuille_actuelle.lier(partial(self.verifier_feuille, eq1=(a, b), eq2=(c, d))) |
| 306 |
|
| 307 |
|
| 308 |
def verifier_feuille(self, eq1, eq2): |
| 309 |
print(eq1, eq2) |
| 310 |
for nom, eq in (('d1', eq1), ('d2', eq2)): |
| 311 |
if nom in self.feuille_actuelle.objets.noms: |
| 312 |
d = self.feuille_actuelle.objets[nom] |
| 313 |
d.label(mode='nom') |
| 314 |
champ = self.feuille_actuelle.objets['champcache_' + nom] |
| 315 |
M, N = d |
| 316 |
M = (int(M.x), int(M.y)) |
| 317 |
N = (int(N.x), int(N.y)) |
| 318 |
if self.eq_reduite(M, N) == eq: |
| 319 |
d.style(couleur='g') |
| 320 |
champ.texte = 'ok' |
| 321 |
msg = 'La droite %s est correcte.' % nom |
| 322 |
if nom == 'd1': |
| 323 |
msg += ' Construisez maintenant d2.' |
| 324 |
else: |
| 325 |
print(self.eq_reduite(*d), eq) |
| 326 |
d.style(couleur='r') |
| 327 |
s |
| 328 |
is |
| 329 |
r |
| 330 |
|
| 331 |
|
| 332 |
champ.texte = str(d.equation) |
| 333 |
msg = "Attention, la droite %s est fausse." % nom |
| 334 |
QToolTip.showText(QCursor.pos(), msg) |
| 335 |
self.canvas.message(msg, temporaire=False) |
| 336 |
|
| 337 |
|
| 338 |
def valider_couple(self, reponse, attendu): |
| 339 |
reponse = reponse.replace(';', ',') |
| 340 |
try: |
| 341 |
if S(reponse) == S(attendu): |
| 342 |
return True |
| 343 |
except Exception: |
| 344 |
print_error() |
| 345 |
return False |
| 346 |
|
| 347 |
|
| 348 |
def ax_b(self): |
| 349 |
"Générer une expression sympy de la forme ax+b, avec a, b dans Z." |
| 350 |
return self.relatif()*S('x') + self.relatif() |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
def _ouvrir(self, fgeo): |
| 361 |
pass |
| 362 |
. |
| 363 |
l. |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|