archimede.py

Created by frederic-junier

Created on August 19, 2019

2.07 KB

Approximation de Pi par la méthode d’Archimède


from turtle import *
from math import *
from kandinsky import draw_string

def cercle(r, n):
    """Trace un cercle de centre (0,0) et de rayon r avec n points"""
    up()
    goto(r,0)
    down()
    color("black")
    for t in range(1, n + 1):
        goto(r * cos(2 * pi / n * t), r * sin(2 * pi / n * t))
        
def distance(x1, y1, x2, y2):
    return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

def methode_archimede(r, n):
    """Approximation de pi par la méthode d'Archimède
    Encadrement d'un cercle de rayon r par des polygones de n cotés
    """
    #cercle
    cercle(r, 1000)
    #coordonnées du point du polygone inscrit
    xi = r
    yi = 0    
    #rapport entre polygone exinscritet polygone inscrit
    coef = 1 / cos(pi/n)
    #coordonnées du point du polygone exinscrit
    xe = coef * xi
    ye = coef * yi 
    #perimetre inscrit
    pin = 0
    #perimetre exinscrit
    pout = 0
    #initialisation du tracé
    up()
    goto(xi, yi)
    down()
    for k in range(1, n + 1):
        #prochain point du polygone inscrit
        xi2 = r * cos(k * 2 * pi / n)
        yi2 = r * sin(k * 2 * pi / n)
        #prochain point du polygone exinscrit
        xe2 = coef * xi2
        ye2 = coef * yi2
        #tracé du côté du polygone inscrit
        color("blue")
        goto(xi2, yi2)
        up()
        #tracé du côté du polygone exinscrit
        goto(xe, ye)
        down()
        color("red")
        goto(xe2, ye2)
        up()
        goto(xi2, yi2)
        #mise a jour des perimetres
        pin = pin + distance(xi, yi, xi2, yi2)
        pout = pout + distance(xe, ye, xe2, ye2)
        #mise a jour des points
        xi = xi2
        yi = yi2 
        xe = xe2
        ye = ye2
        down()  
    draw_string(str(pin / (2 * rayon)),0,0)
    draw_string("<",0,25)
    draw_string("Pi",0,50)
    draw_string("<",0,75)
    draw_string(str(pout / (2 * rayon)),0,100)

def trace(nbcote):
  methode_archimede(rayon, nbcote)
  
#Tracé des polygones inscrit et exinscrit par la méthode d'Archimède
rayon = 75
nbcote = int(input("Nombre de cotés ? "))
trace(nbcote)

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.