maths_trigonometrie.py

Created by antarctus

Created on February 27, 2022

2.3 KB

Programme pour faire de la trigonométrie.
SQRT veut dire racine carrée. Et écrire pi dans l’entrée texte pour en avoir la valeur.


from math import *
from kandinsky import *
from ion import *

BACK=(255,)*3
COLOR=(0,)*3

COS_COLOR=(255,0,0)
SIN_COLOR=(0,0,255)
ANG_COLOR=(0,255,0)

ang_txt=""
sin_txt=""
cos_txt=""
a_input="pi/3"
a=0

def draw_line(x1,y1,x2,y2,c):
  width=x2-x1
  height=y2-y1
  if abs(width)>=abs(height):
    div=height/width
    for i in range(0,width,(width>0)*2-1):
      set_pixel(x1+i,y1+int(div*i),c)
  else:
    div=width/height
    for i in range(0,height,(height>0)*2-1):
      set_pixel(x1+int(div*i),y1+i,c)


def draw_all():
  fill_rect(0,0,320,222,BACK)
  fill_rect(0,160,320,1,COLOR)
  fill_rect(0,80,320,1,COLOR)
  fill_rect(160,0,1,160,COLOR)
  for i in range(500):
    set_pixel(160+int(cos(i/500*2*pi)*70),80-int(sin(i/500*2*pi)*70),COLOR)

  x,y=160+int(cos(a)*70),80-int(sin(a)*70)
  draw_line(160,80,x,y,COLOR)

  for i in range(int(500*a/(2*pi))):
    set_pixel(160+int(cos(i/500*2*pi)*20),80-int(sin(i/500*2*pi)*20),ANG_COLOR)
  fill_rect(0,y,320,1,SIN_COLOR)
  fill_rect(x,0,1,160,COS_COLOR)

  draw_string("    a ="+ang_txt,5,162,ANG_COLOR,BACK)
  draw_string("sin(a)="+sin_txt,5,182,SIN_COLOR,BACK)
  draw_string("cos(a)="+cos_txt,5,202,COS_COLOR,BACK)

  fill_rect(240,170,70,40,(200,200,0))
  draw_string("Ok pour",240,173,(0,)*3,(200,200,0))
  draw_string("changer",240,193,(0,)*3,(200,200,0))


def get_txt_values(a):
  l=[
  ["0"      ,"0"         ,"1"],
  ["pi/6"   ,"0.5"      ,"sqrt(3)/2"],
  ["pi/4"   ,"sqrt(2)/2","sqrt(2)/2"],
  ["pi/3"   ,"sqrt(3)/2","0.5"],
  ["pi/2"   ,"1"        ,"0"],
  ["2*pi/3" ,"sqrt(3)/2","-0.5"],
  ["3*pi/4" ,"sqrt(2)/2","-sqrt(2)/2"],
  ["5*pi/6" ,"0.5"      ,"-sqrt(3)/2"],
  ["pi"     ,"0"         ,"-1"],
  ["-pi/6"  ,"-0.5"      ,"sqrt(3)/2"],
  ["-pi/4"  ,"-sqrt(2)/2","sqrt(2)/2"],
  ["-pi/3"  ,"-sqrt(3)/2","0.5"],
  ["-pi/2"  ,"-1"        ,"0"],
  ["-2*pi/3","-sqrt(3)/2","-0.5"],
  ["-3*pi/4","-sqrt(2)/2","-sqrt(2)/2"],
  ["-5*pi/6","-0.5"      ,"-sqrt(3)/2"],
  ]
  for i in l:
    if eval(i[0])%(2*pi)==a:
      return i

while a_input!="":
  a=eval(a_input)%(2*pi)
  if get_txt_values(a):
    ang_txt,sin_txt,cos_txt=get_txt_values(a)
  else:
    ang_txt,sin_txt,cos_txt=str(a)[:8],str(sin(a))[:8],str(cos(a))[:8]
  draw_all()
  while not keydown(KEY_OK):
    pass  
  a_input=input("Valeur de l'angle, en radians:")

print("Rien mis, donc fin du programme")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

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.