dodecaedre.py

Created by schraf

Created on September 29, 2023

2.5 KB

Voir la construction des 5 solides de Platon


from math import *
from turtle import *

c1, c2 = "grey", "black"

def dist(a, b):
    return sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)

def angle(a, b):
    return degrees(atan2(b[1] - a[1], b[0] - a[0]))

def point(a):
    penup()
    goto(a)
    pendown()
    pensize(4)
    goto(position())
    pensize(1)

def segment(a, b, coul, dash=True, s=1):
    pencolor(coul)
    r = dist(a, b)
    penup()
    point(a)
    if not (dash):
        pensize(s)
    setheading(angle(a, b))
    for i in range(r):
        penup() if i % 5 and dash else pendown()
        fd(1)
    point(b)

def avance(a, b, d):
    penup()
    goto(a)
    setheading(angle(a, b))
    fd(d)
    pendown()
    pensize(4)
    goto(position())

def cercle(a, b, r, coul):
    color(coul)
    avance(a, b, r)
    b = position()
    segment(a, b, coul)
    p = position()
    point(p)
    setheading(heading() + 90)
    circle(int(r))

def mul(p, i): return (i * p[0], i * p[1])

def rot(p, i, n=3):
    return [
        p[0] * cos(pi / n * i) - p[1] * sin(i * pi / n),
        p[0] * sin(pi / n * i) + p[1] * cos(i * pi / n),
    ]

def rangee(p):
    segment(mul(p, 10), mul(p, -10), c1)
    for i in range(1, 5):
        coul = (230,) * 3 if i % 2 else c1
        for s in (1, -1):
            cercle(mul(p, s * i), mul(p, s * (i + 1)), 25, coul)

def fond():
    global pt, ec
    reset()
    speed(6)
    pencolor(c1)
    segment((0, -150), (0, 150), c1)
    for i in range(5):
        coul = (230,) * 3 if i % 2 else c1
        cercle((0, -25 * i), (0, -25 - 25 * i), 25, coul)
        if i > 0:
            cercle((0, 25 * i), (0, 25 + 25 * i), 25, coul)
    p = [25 * cos(pi / 6), 25 * sin(pi / 6)]
    rangee(p)
    p[0] *= -1
    rangee(p)
    p[0] *= -1
    cs = 100 * cos(pi / 6)
    for s in (-1, 1):
        segment((0, s * 100), (-cs, -s * 50), c1, False)
        segment((-cs, -s * 50), (cs, -s * 50), c1, False)
        segment((cs, -s * 50), (0, s * 100), c1, False)
    for i in range(6):
        segment(rot(mul(p, 4), i), rot(mul(p, 2), i + 2), c1, False)
        segment(rot(mul(p, 4), i), rot(mul(p, 2), i - 2), c1, False)

pts = (-35, -39), (-18, -49), (18, -49)
fond()
pencolor(c2)
pensize(2)

for i in range(6):
    penup()
    for j in range(2):
        goto(rot(pts[j], i))
        pendown()
        goto(rot(pts[j + 1], i))

pts = (0, 0), (0, -32), (-34, -39), (0, -32), (34, -39)

for i in range(3):
    penup()
    for j in range(4):
        goto(rot(pts[j], i, 1.5))
        pendown()
        goto(rot(pts[j + 1], i, 1.5))

hideturtle()