lentilles_convergentes.py

Created by dan-tabet

Created on May 20, 2025

5.89 KB

représente les schémas des 3 visions des lentilles converentes


from kandinsky import *
from math import *

# Configuration de la fenêtre
width = 320
height = 240
centre_x = width // 2
centre_y = height // 2
noir = (0, 0, 0)
rouge = (255, 0, 0)
bleu = (0, 0, 255)
gris = (128, 128, 128)

def effacer():
    """Efface l'écran de la calculatrice."""
    fill_rect(0, 0, width, height, (255, 255, 255)) # Remplit l'écran en blanc

def afficher_axes():
    """
    Affiche les axes x et y.
    """
    # Axe des abscisses (horizontal)
    for x in range(0, width, 5):
        if x == centre_x:
          fill_rect(x, centre_y - 2, 1, 5, rouge)
        else:
            fill_rect(x, centre_y, 1, 1, gris)

    # Axe des ordonnées (vertical)
    for y in range(0, height, 5):
        if y == centre_y:
            fill_rect(centre_x - 2, y, 5, 1, rouge)
        else:
             fill_rect(centre_x, y, 1, 1, gris)
    # Légende des axes
    draw_string("O", centre_x + 2, centre_y + 2, noir)

def afficher_lentille(x, f):
    """
    Affiche la lentille convergente.

    Args:
        x (int): Position x de la lentille.
        f (int): Distance focale (non utilisé ici, mais pourrait l'être pour des graduations).
    """
    # Dessine un symbole de lentille convergente
    fill_rect(x - 2, centre_y - 10, 4, 20, noir)  # Barre verticale
    #Pointes de flèche
    points = [(x - 10, centre_y - 10), (x-2, centre_y-5), (x-2, centre_y-15)]
    for i in range(len(points)-1):
        x1,y1 = points[i]
        x2,y2 = points[i+1]
        tracer_segment(x1,y1,x2,y2,noir)

    points = [(x + 10, centre_y - 10), (x+2, centre_y-5), (x+2, centre_y-15)]
    for i in range(len(points)-1):
        x1,y1 = points[i]
        x2,y2 = points[i+1]
        tracer_segment(x1,y1,x2,y2,noir)

def afficher_foyer(x, f):
    """
    Affiche le foyer principal de la lentille.

    Args:
        x (int): Position x du foyer.
        f (int): Distance focale.
    """
    # Dessine un point pour le foyer
    fill_rect(x - 2, centre_y - 2, 5, 5, noir)
    draw_string("F", x + 5, centre_y - 5, noir)

def afficher_objet(x, h):
    """
    Affiche l'objet.

    Args:
        x (int): Position x de l'objet.
        h (int): Hauteur de l'objet.
    """
    # Dessine une flèche verticale pour l'objet
    x1 = x
    y1 = centre_y - h
    x2 = x
    y2 = centre_y
    tracer_segment(x1,y1,x2,y2,bleu)
    # Ajoute la pointe de la flèche
    if h > 0: #Si l'objet est au dessus de l'axe
      points = [(x, centre_y - h), (x - 3, centre_y - h + 5), (x + 3, centre_y - h + 5)]
      for i in range(len(points)-1):
        x1,y1 = points[i]
        x2,y2 = points[i+1]
        tracer_segment(x1,y1,x2,y2,bleu)

def afficher_image(x, h):
    """
    Affiche l'image.

    Args:
        x (int): Position x de l'image.
        h (int): Hauteur de l'image.
    """
    # Dessine une flèche verticale pour l'image
    x1 = x
    y1 = centre_y - h
    x2 = x
    y2 = centre_y
    tracer_segment(x1,y1,x2,y2,rouge)
    # Ajoute la pointe de la flèche
    if h > 0:
        points = [(x, centre_y - h), (x - 3, centre_y - h + 5), (x + 3, centre_y - h + 5)]
        for i in range(len(points)-1):
          x1,y1 = points[i]
          x2,y2 = points[i+1]
          tracer_segment(x1,y1,x2,y2,rouge)
    else: #Image en dessous de l'axe optique
        points = [(x, centre_y - h), (x - 3, centre_y - h - 5), (x + 3, centre_y - h - 5)]
        for i in range(len(points)-1):
          x1,y1 = points[i]
          x2,y2 = points[i+1]
          tracer_segment(x1,y1,x2,y2,rouge)

def tracer_segment(x1, y1, x2, y2, couleur):
    """
    Trace un segment de droite entre deux points.
    Remplace la fonction draw_line de kandinsky.
    Args:
        x1 (int): Coordonnée x du point de départ.
        y1 (int): Coordonnée y du point de départ.
        x2 (int): Coordonnée x du point d'arrivée.
        y2 (int): Coordonnée y du point d'arrivée.
        couleur (tuple): Couleur du segment (RGB).
    """
    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    sx = 1 if x1 < x2 else -1
    sy = 1 if y1 < y2 else -1
    err = dx - dy
    x = x1
    y = y1
    while True:
        set_pixel(x, y, couleur)
        if x == x2 and y == y2:
            break
        e2 = 2 * err
        if e2 > -dy:
            err -= dy
            x += sx
        if e2 < dx:
            err += dx
            y += sy

def exemple_schema_1():
    """
    Affiche un exemple de schéma de formation d'image (premier cas).
    """
    effacer()
    afficher_axes()
    afficher_lentille(centre_x, 50)  # Lentille au centre
    f = 50 #Distance focale
    afficher_foyer(centre_x + f, f)  # Foyer à droite
    afficher_foyer(centre_x - f, f) # Foyer à gauche
    afficher_objet(centre_x - 100, 30)  # Objet à gauche de la lentille
    afficher_image(centre_x + 66, -20)  # Image réelle, renversée

def exemple_schema_2():
    """
    Affiche un exemple de schéma de formation d'image (deuxième cas).
    """
    effacer()
    afficher_axes()
    afficher_lentille(centre_x, 50)
    f = 50
    afficher_foyer(centre_x + f, f)
    afficher_foyer(centre_x - f, f)
    afficher_objet(centre_x + 30, 20)  # Objet entre le foyer et la lentille
    afficher_image(centre_x + 150, 100)  # Image virtuelle, droite, agrandie

def exemple_schema_3():
    """
    Affiche un exemple de schéma de formation d'image (troisième cas).
    """
    effacer()
    afficher_axes()
    afficher_lentille(centre_x, 50)
    f = 50
    afficher_foyer(centre_x + f, f)
    afficher_foyer(centre_x - f, f)
    afficher_objet(centre_x + 80, 40)  # Objet après le foyer
    afficher_image(centre_x + 133, -66)  # Image réelle, renversée, agrandie

# Exemple d'utilisation :
# Pour afficher le premier schéma, exécutez : exemple_schema_1()
# Pour afficher le deuxième schéma, exécutez : exemple_schema_2()
# Pour afficher le troisième schéma, exécutez : exemple_schema_3()

# Vous pouvez choisir quel schéma afficher en appelant la fonction correspondante.
exemple_schema_3() # Affiche le premier schéma par défaut

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.