1. Mais comment passer des s´equences aux pavages?
C’est grˆace `a cet algorithme en language Python qu’on parvient
`a transformer une s´equence de pile ou face sur excel en un
pavage de Truchet.
1 import pygame , Buttons
2 from pygame . l o c a l s import ∗
3 from PIL import Image
4 import random
5 import x l r d
6 import os
7
8 #o u v e r t u r e du f i c h i e r e x c e l
9 workbook = x l r d . open workbook ( ’ C l a s s e u r 1 . x l s ’ )
10 s h e e t = workbook . s h e e t b y i n d e x ( 0 )
11
12 d e f av ( x , y , dh ) :
13 i f ( x==0 and y>0) :
14 r e t u r n ( dh∗x+y−1)
15 e l i f ( x>0) :
16 r e t u r n ( ( x−1)∗dh+y )
17 random . seed ( )
18 #Ouverture des images n c e s s a i r e s
19 pr = Image . open ( ” pr . png” )
20 pv = Image . open ( ”pv . png” )
21 f r = Image . open ( ” f r . png” )
22 f v = Image . open ( ” f v . png” )
23
24 ( width , h e i g h t )= pr . s i z e
25 #l e c t u r e des i n f o r m a t i o n s du t a b l e u r
26 n=s h e e t . nrows
27 c=s h e e t . n c o l s
28
29 #C r e a t i o n d ’ une l i s t e
30 pouf = [ [ ] f o r i i n xrange ( n ) ]
31 f o r i i n xrange ( 0 , n ) :
32 f o r h i n xrange ( 0 , c ) :
33 #Ajout des i n f o r m a t i o n s du t a b l e a u dans l a l i s t e pouf
34 i f s h e e t . c e l l v a l u e ( h , i )==”F” :
35 pouf [ i ] . append ( ”F” )
36 e l i f s h e e t . c e l l v a l u e ( h , i )==”P” :
37 pouf [ i ] . append ( ”P” )
38
39 #C r e a t i o n d ’ une image qui s ’ adapte l a t a i l l e du t a b l e u r
40 image=Image . new ( ”RGB” , ( n∗width , c∗ h e i g h t ) , ( 2 5 5 , 2 5 5 , 2 5 5 ) )
41 ( iwidth , i h e i g h t ) = image . s i z e
42 dh = i h e i g h t / h e i g h t
43 dw = i w i d t h / width
44 l o g = [ ]
45
46 #Ajout des images s u i v a n t l a sequence d ’ e x c e l
47 f o r x i n xrange ( 0 , dh ) :
48 f o r y i n xrange ( 0 ,dw) :
49 avant = av ( x , y , dh )
50 i f ( pouf [ x ] [ y]==”F” ) :
51 i f ( x==0 and y==0) :
52 g . p a s t e ( fv , ( x∗width , y∗ h e i g h t ) )
53 l o g . append ( ” f v ” )
54 e l s e :
55 i f ( l o g [ avant]==” f v ” or l o g [ avant]==” pr ” ) :
56 g . p a s t e ( f r , ( x∗width , y∗ h e i g h t ) )
57 l o g . append ( ” f r ” )
58 e l i f ( l o g [ avant]==” f r ” or l o g [ avant]==”pv” ) :
59 g . p a s t e ( fv , ( x∗width , y∗ h e i g h t ) )
60 l o g . append ( ” f v ” )
61 e l i f ( pouf [ x ] [ y ] . upper ( )==”P” ) :
62 i f ( x==0 and y==0) :
63 g . p a s t e ( pv , ( x∗width , y∗ h e i g h t ) )
64 l o g . append ( ”pv” )
65 e l s e :
66 i f ( l o g [ avant]==”pv” or l o g [ avant]==” f r ” ) :
67 g . p a s t e ( pr , ( x∗width , y∗ h e i g h t ) )
68 l o g . append ( ” pr ” )
69 e l i f ( l o g [ avant]==” pr ” or l o g [ avant]==” f v ” ) :
70 g . p a s t e ( pv , ( x∗width , y∗ h e i g h t ) )
71 l o g . append ( ”pv” )
72 #Ouvrir l ’ image
73 image . show ( )
Cet algorithme permet de cr´eer des chefs-d’œuvre comme
les pavages suivants:
Pour cela, il suffit de changer les images de base et inventer
un dessin unique.
Enrique Lacal et Lu˜a Streit