frommathimportsin,cos,piimportkandinskyaskdimportionimporttime# --- Paramètres écran ---
WIDTH=320HEIGHT=240CENTER_X=WIDTH//2CENTER_Y=HEIGHT//2SCALE_X=20SCALE_Y=50# --- Fonctions mathématiques ---
defsinc(t):ift==0:return1returnsin(pi*t)/(pi*t)defrect(t,largeur=1):return1ifabs(t)<largeur/2else0defdirac_approx(t):return10ifabs(t)<0.05else0# --- Affichage axes ---
defdraw_axes():forxinrange(WIDTH):kd.set_pixel(x,CENTER_Y,(200,200,200))foryinrange(HEIGHT):kd.set_pixel(CENTER_X,y,(200,200,200))# --- Tracé de fonction ---
defdraw_function(func,color=(0,0,255),t_min=-5,t_max=5):prev=Noneforpxinrange(WIDTH):t=t_min+(t_max-t_min)*px/WIDTHy=func(t)py=CENTER_Y-int(y*SCALE_Y)if0<=py<HEIGHT:kd.set_pixel(px,py,color)ifprev:y0=prev[1]forpy2inrange(min(py,y0),max(py,y0)+1):if0<=py2<HEIGHT:kd.set_pixel(px,py2,color)prev=(px,py)# --- Nettoyage écran ---
defclear_screen():kd.fill_rect(0,0,WIDTH,HEIGHT,(255,255,255))# --- Menu simple ---
defmenu():clear_screen()kd.draw_string("Choisir la fonction :",20,20)kd.draw_string("1. sin(t)",20,50)kd.draw_string("2. cos(t)",20,70)kd.draw_string("3. sinc(t)",20,90)kd.draw_string("4. rect(t)",20,110)kd.draw_string("5. dirac approx.",20,130)kd.draw_string("OK pour quitter",20,170)# --- Boucle principale ---
defmain():whileTrue:menu()whileTrue:ifion.keypad.is_pressed(ion.keypad.KEY_ONE):plot_func(sin,(0,0,255),"sin(t)")breakelifion.keypad.is_pressed(ion.keypad.KEY_TWO):plot_func(cos,(0,128,0),"cos(t)")breakelifion.keypad.is_pressed(ion.keypad.KEY_THREE):plot_func(sinc,(255,0,0),"sinc(t)")breakelifion.keypad.is_pressed(ion.keypad.KEY_FOUR):plot_func(lambdat:rect(t,2),(255,165,0),"rect(t)")breakelifion.keypad.is_pressed(ion.keypad.KEY_FIVE):plot_func(dirac_approx,(128,0,128),"dirac approx")breakelifion.keypad.is_pressed(ion.keypad.KEY_OK):returntime.sleep(0.1)# --- Affichage d'une fonction avec titre ---
defplot_func(func,color,label):clear_screen()draw_axes()draw_function(func,color)kd.draw_string(label,10,10)kd.draw_string("OK pour retour",10,200)whilenotion.keypad.is_pressed(ion.keypad.KEY_OK):time.sleep(0.1)main()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.