-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrianglify.mp
139 lines (104 loc) · 2.82 KB
/
trianglify.mp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
% 2016-05-26, fiedsch@ja-eh.at
% License: MIT
% For more info on the subject see e.g. http://qrohlf.com/trianglify/
% important only if fonts are used
% prologues := 3;
% generate (E)PS
outputtemplate := "%j-%c.mps";
% generate SVG
% outputtemplate := "%j-%c.svg";
% outputformat := "svg";
% generate PNG
% outputtemplate := "%j-%c.png";
% outputformat := "png";
% Setup rows and columns of the image's grid
rows=30;
cols=50;
% unit (width and height)
u=1cm;
% randomize results (set to 0 while debugging)
postiondistortion=.20u;
colordistortion=0.025;
% colors for the four corners of the image
% the parts in between will be interpolated
% nw ... north west
% ne ... north east
% sw .. south west
% se ... south east
color nw,ne,sw,se;
nw = 0.3[white,(20/255,245/255,255/255)];
ne = green;
sw = blue;
se = 0.3[white, black];
% line color and line width (set line width to 0 to disable drawing of lines)
color linecolor; linecolor=0.2[white,black];
linewidth:= .3pt;
% grid coordinates (see below for setup)
pair points[][];
% get fillcolor depending on position (optionally randomized)
def fillcolor(expr i,j) =
(((rows-i)/rows)+colordistortion*normaldeviate)
[
(((cols-j)/cols)+colordistortion*normaldeviate)[nw,ne],
(((cols-j)/cols)+colordistortion*normaldeviate)[sw,se]
]
enddef;
% lighten a color
def lighten(expr c) =
0.05[c,white]
enddef;
% set up points (coordinates of the image parts)
def setpoints =
for i=0 upto rows:
for j=0 upto cols:
points[i][j] := (j*u,-i*u)
shifted (normaldeviate*postiondistortion,normaldeviate*postiondistortion);
endfor;
endfor;
enddef;
% draw one piece of the image
def drawpart(expr i,j) =
save p; path p[];
% base for the current part is a rectangular shape
% x---x
% | |
% x---x
p[0]=points[i][j]--points[i][j+1]--points[i+1][j+1]--points[i+1][j]--cycle;
% next, divide rectangular into two triangles sw to ne or nw to se
% x---x x---x
% | / | or | \ |
% x---x x---x
if normaldeviate < 0:
p[1] := point 1 of p[0]--point 2 of p[0]--point 4 of p[0]--cycle;
p[2] := point 2 of p[0]--point 3 of p[0]--point 4 of p[0]--cycle;
else:
p[1] := point 1 of p[0]--point 2 of p[0]--point 3 of p[0]--cycle;
p[2] := point 3 of p[0]--point 4 of p[0]--point 1 of p[0]--cycle;
fi
fill p[1] withcolor fillcolor(i,j);
fill p[2] withcolor fillcolor(i,j);
draw p[1] withcolor linecolor;
draw p[2] withcolor linecolor;
enddef;
% draw the full picture
def drawpicture =
pickup pencircle scaled linewidth;
for i=0 upto rows-1:
for j=0 upto cols-1:
drawpart(i,j);
endfor;
endfor;
enddef;
beginfig(1);
% debug: no distortion
save postiondistortion, colordistortion;
postiondistortion:=colordistortion:=0;
setpoints;
drawpicture;
endfig;
beginfig(2);
setpoints;
drawpicture;
endfig;
end
%% EOF %%