prisme.py

Created by schraf

Created on May 13, 2023

1.26 KB

Vidéo d’explication

D’autres exemples


from turtle import *
from math import *
hideturtle()

pf, h = 40, 10
cx, cy = 30, 43 # proportions ecran Numworks

def drte(*pts):
    (xa, ya), (xb, yb) = pts
    return (yb - ya, xa - xb, xa * yb - xb * ya)

def persp(xyz):
    x, y, z = xyz
    pt1 = solve22(drte((x, y), (x + 1, y - 1)), (0, 1, 0))
    pt2 = solve22(drte((x, y), (x - 1, y - 1)), (0, 1, 0))
    if z != h:
        pt1[1] += z
        pt2[1] += z
    sol = solve22(drte(pt1, (-pf, h)), drte(pt2, (pf, h)))
    if z == h:
        return sol[0], h
    else:
        return sol

def solve22(*eq):
    (a, b, e), (c, d, f) = eq
    det = a * d - b * c
    if det != 0:
        x = (e * d - b * f) / det
        y = (a * f - e * c) / det
    return [x, y]

def polygon(p, coul):
  color(coul)
  penup()
  for (x, y) in p + [p[0]]:
    goto(cx * x, cy * y - 110)
    pendown()
    goto(cx * x, cy * y - 110)
    
def poly(p, coul = 'black'):
    res = []
    for (x, y, z) in p:
        res.append(persp((x, y, z)))
    polygon(res,coul)

haut, bas = [], []

n = 20
for i in range(n):
    x , y = 4 * cos(2 * i * pi / n), 6 + 4 * sin(2 * i * pi / n)
    haut.append((x,y,2))
    bas.append((x,y,0))
    
for i in range(n):
    poly([bas[i],haut[i],haut[(i+1)%n],bas[(i+1)%n]], 'grey' if i < n/2 else 'black')