Created
December 11, 2021 09:11
-
-
Save jul/9f041a37a53801745ff7125741f5eabd to your computer and use it in GitHub Desktop.
dessiner un pavage triangulaire en python en moins de 100 sloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PySimpleGUI as sg | |
### uitliser ipython -i argv[0] pour être en interactif et faire joujou | |
can = sg.Canvas(size=(400,600)) | |
win = sg.Window("titre",[[can,],],finalize=True) | |
c = can.TKCanvas | |
SZ=50 | |
def tr(x,y, up=1,**kw): | |
""" créer un triangle équilatéral centré en x,y avec une homothétie / | |
symmétrie sur l'axe des y (up / down) exprimée en coordonnées | |
unitaire de suffisamment gros pour voir (histoire de pouvoir grossir | |
diminuer le réseau sans changer le code)""" | |
hs=0.8666 | |
x,y=x*SZ+SZ,y*hs*SZ+SZ | |
i=c.create_polygon( | |
x+up*SZ/2, y+up*(SZ*hs/3), | |
x-up*SZ/2, y+up*(SZ*hs/3), | |
x, y-up*(SZ*2*hs/3), | |
fill=kw.get("fill"),outline="white") | |
# et on dessine un cercle en son centre pour unittester à l'oeil | |
c.create_oval(x-SZ/4,y-SZ/4,SZ/4+x,y+SZ/4, | |
fill=x%2 and "yellow" or "blue", outline="white") | |
c.update() | |
return i | |
# recodons canvas.move car on oublie de lire la doc | |
def move(c, tag,dx,dy): | |
xy=c.coords(tag) | |
c.coords(tag, sum(map(lambda x,y:[x+dx,y+dy],xy[::2],xy[1::2]), start=[])) | |
c.update() | |
for x in range(12): | |
for y in range(12): | |
tr( | |
x/2-1/2*(y%2), | |
y-[2/3,1/3][x%2], | |
[-1,1][(x)% 2], | |
fill=["red" , "green" ][y%2]) |
Author
jul
commented
Dec 11, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment