perspective.py

Created by schraf

Created on May 14, 2023

1.83 KB

Vidéo d’explication

D’autres exemples

```from turtle import *
hideturtle()

pf, h = 15, 4
cx, cy = 30, 43 # proportions ecran Numworks

sol = (-7, 0, 0), (5, 0, 0), (5, 15, 0), (-7, 15, 0)
fond = (-7, 15, 0), (-7, 15, 5), (5, 15, 5), (5, 15, 0)
gauche = (-7, 0, 0), (-7, 15, 0), (-7, 15, 5), (-7, 0, 5)
droite = (5, 0, 0), (5, 15, 0), (5, 15, 5), (5, 0, 5)
chevet_cotes = (-7, 6, 0), (-5, 6, 0), (-5, 8, 0), (-5, 8, 1), (-5, 6, 1), (-7, 6, 1)
chevet_haut = (-7, 6, 1), (-7, 8, 1), (-5, 8, 1), (-5, 6, 1)
lit_cotes = (-7, 9, 0), (-1, 9, 0), (-1, 13, 0), (-1, 13, 1), (-1, 9, 1), (-7, 9, 1)
lit_haut = (-7, 9, 1), (-1, 9, 1), (-1, 13, 1), (-7, 13, 1)
armoire_cotes = (3, 10, 0), (5, 10, 0), (5, 10, 5), (3, 10, 5)
armoire_face = (3, 10, 0), (3, 15, 0), (3, 15, 5), (3, 10, 5)
cadre = (5, 6, 2), (5, 8, 2), (5, 8, 4), (5, 6, 4)

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 = [x + y, 0]
pt2 = [x - y, 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)

poly(sol, 'green')
poly(fond, 'grey')
poly(gauche, 'grey')
poly(droite, 'grey')
poly(chevet_cotes, 'red')
poly(chevet_haut, 'red')
poly(lit_cotes, 'blue')
poly(lit_haut, 'blue')
poly(armoire_cotes, 'brown')
poly(armoire_face, 'brown')