diamant.py

Created by schraf

Created on March 29, 2022

1.08 KB


import turtle
T = turtle

T.speed(0)

PTS = [(-1, -87),(105, 43),(68, 87),(-67, 87),(-106, 42),(-75, 42),\
       (-39, 84),(-30, 42),(-1, 85),(28, 42),(37, 82),(75, 42)]

def position(a,b,t):
 return (1-t) * a[0] + t * b[0], (1-t) * a[1] + t * b[1]

def segments(l,coul=(0,0,0), taille = 1):
 T.pensize(taille)
 T.color(coul)
 for i in range(len(l)):
  T.penup() if i == 0 else T.pendown()
  T.goto(PTS[l[i]-1][0],PTS[l[i]-1][1])

def region(data, coul=(0,0,0), taille=1, n=10):
 for i in range(len(data)-2):
  hachure((data[i], data[i+1],data[i+2],coul,taille),n)

def hachure(data, n):
 (a,b,c,coul,taille) = data
 T.color(coul)
 T.pensize(taille)
 for i in range(n+1):
  t = i / n
  T.penup()
  x, y = position(PTS[a-1], PTS[b-1], t)
  T.goto(x, y)
  T.pendown()
  x, y = position(PTS[a-1], PTS[c-1], t)
  T.goto(x, y)      

for r in [[12,2,1],[2,12,3],[5,6,1],[5,6,4]]: region(r,n=10)
for r in [[10,12,1],[8,6,1],[10,11,12],[6,7,8]]: region(r,n=8)
for r in [[1,8,10]]: region(r,n=12)
for r in [[9,8,10]]: region(r,n=5)
for s in [[1,2,3,4,5,1]]: segments(s,taille=2)

T.hideturtle()