Transparence pour la main dobble
[luatex.git] / dobble / dobble.lua
1 math.randomseed(os.time())
2 function dobble(prefixe,p)
3    -- prefixe : préfixe des fichiers d'image
4    --           (symboles présents sur les cartes) pdf
5    --           les noms fichiers doivent être de la forme prefixe001.pdf etc.
6    -- p       : nombre premier (génération de 1+p+p² cartes à partir
7    --           du même nombre de symboles
8    -- on va générer un tableau où chaque case correspond
9    -- à une carte et contient la liste des numéros de symbole à mettre
10    local cartes={};
11    -- première carte
12    cartes[0]={};
13    for i=0,p do
14       cartes[0][i]=i;
15    end
16    printcard(cartes[0],p,prefixe);
17    -- p+p*p suivantes
18    for i=1,p do
19       -- p premières
20       cartes[i]={};
21       for j=0,p do
22          if j==0 then
23             cartes[i][j]=0;
24          else
25             -- on place les symboles aléatoirement
26             table.insert(cartes[i],intrand(j),i*p+j);
27          end
28       end
29      printcard(cartes[i],p,prefixe);
30       -- p*p suivantes
31       for j=1,p do
32          cartes[p+(i-1)*p+j]={};
33          for k=0,p do
34             if k==0 then
35                cartes[p+(i-1)*p+j][k]=i;
36             else
37                -- on place les symboles aléatoirement
38                table.insert(cartes[p+(i-1)*p+j],intrand(k),(k)*p+((k*i+j-1)%p)+1);
39             end
40          end
41         printcard(cartes[p+(i-1)*p+j],p,prefixe);
42       end
43    end
44    return cartes;
45 end
46
47 function printcard(carte,p,prefixe)
48    -- prend une carte en entrée (tableau de p+1 éléments)
49    -- et génère le code tex d'une page à partir des images
50    -- nommées suivant le prefixe
51    tex.sprint("\\newpage\\begin{tikzpicture}[remember picture, overlay]\\coordinate(Or) at (current page.north west);");
52    -- cadre de la carte
53    tex.sprint("\\draw[rounded corners=0.5cm, line width=.2cm] ($(Or)+(0.1,-0.1)$) rectangle ($(Or)+(7.9,-7.9)$);");
54    -- entier aléatoire pour l'élément au centre
55    local has=intrand(p+1);
56    local compt=0;
57    for i=0,p do
58       -- l'image au contre
59       if i== has then
60          tex.sprint("\\node[scale="..scalerand(0.7,1.3)..",rotate="..intrand(360).."] at ($(Or)+(4,-4)$) {\\includegraphics[width=1cm]{"..prefixe..string.format("%02d",carte[i]+1).."}};");
61       else
62          -- les autres tout le tour, taille aléatoire, rayon variant, rotation aléatoire
63          tex.sprint("\\node[scale="..scalerand(0.7,1.3)..",rotate="..intrand(360).."] at ($("..(360/(p)*compt)..":"..scalerand(2.3,3)..")+(Or)+(4,-4)$) {\\includegraphics[width=1cm]{"..prefixe..string.format("%02d",carte[i]+1).."}};");
64          compt=compt+1;
65       end
66    end
67    tex.sprint("\\end{tikzpicture}");
68 end
69
70
71 function intrand(p) -- retourne un entier entre 0 et p-1
72    return math.floor(p*math.random());
73 end
74
75 function scalerand(a,b) -- retourne un nombre aléatoire entre a et b
76    return a+(b-a)*math.random();
77 end

Licence Creative Commons Les fichiers de Syracuse sont mis à disposition (sauf mention contraire) selon les termes de la
Licence Creative Commons Attribution - Pas d’Utilisation Commerciale - Partage dans les Mêmes Conditions 4.0 International.