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')

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.