/
PaperLager.pde
339 lines (299 loc) · 7.32 KB
/
PaperLager.pde
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* PaperLager
* Herbert Spencer 2013
*/
void setup() {
size(960, 600);
numPuntos = numX * numY;
puntos = new Punto[numPuntos];
achurados = new ArrayList();
creaPuntos();
creaAchurados();
strokeCap(SQUARE);
strokeJoin(BEVEL);
}
void creaPuntos() {
// se crean puntos en una retícula ordenada
float xSpacer = (width - (2*m)) / float(numX - 1);
float ySpacer = (height -(2*m)) / float(numY - 1);
int i = 0; // contador para recorrer el arreglo de puntos
for (int y = 0; y < numY; y++) {
for (int x = 0; x < numX; x++) {
// las coordenadas del nuevo punto
float px = m + xSpacer * x;
float py = m + ySpacer * y;
puntos[i] = new Punto(px, py, i);
i++;
}
}
}
void creaAchurados() {
for (int i = 0; i < puntos.length - numX - 1; i++) {
if ((i - (numX - 1)) % numX != 0) {
Achurado a = new Achurado(puntos[i], puntos[i+1], puntos[i+numX], puntos[i+numX+1]);
achurados.add(a);
}
}
}
void draw() {
background(fondo);
dibujaAchurados();
}
void dibujaAchurados() {
stroke(0, 40);
for (int i = 0; i < achurados.size(); i++) {
Achurado a = (Achurado)achurados.get(i);
a.draw();
}
for (int i = 0; i < puntos.length; i++) {
if (puntos[i].over()) {
puntos[i].draw();
}
}
}
var ancho = 960;
var alto = 600;
Punto[] puntos; // los puntos
ArrayList achurados; // los achurados, compuestos cada uno por 4 puntos
// número de puntos
int numX = 15;
int numY = 10;
int numPuntos; // numX * numY
float m = 100; // margen
// colores
color fondo = color(#8B3F0D);
color lineas = color(#B2581D);
// trama
float anchoTrama = 5;
float anchoTrazo = 1;
void hatch(
float x1, float y1,
float x2, float y2,
float x3, float y3,
float x4, float y4,
int num
) {
float incx1 = (x2 - x1) / (float)(num - 1);
float incy1 = (y2 - y1) / (float)(num - 1);
float incx2 = (x4 - x3) / (float)(num - 1);
float incy2 = (y4 - y3) / (float)(num - 1);
for (int i = 0; i < num; i++) {
line(x1 + incx1 * i, y1 + incy1 * i,
x3 + incx2 * i, y3 + incy2 * i);
}
}
void hatch(Punto a, Punto b, Punto c, Punto d, int num) {
float incx1 = (b.x - a.x) / (float)(num - 1);
float incy1 = (b.y - a.y) / (float)(num - 1);
float incx2 = (d.x - c.x) / (float)(num - 1);
float incy2 = (d.y - c.y) / (float)(num - 1);
for (int i = 0; i < num; i++) {
line(a.x + incx1 * i, b.y + incy1 * i,
c.x + incx2 * i, c.y + incy2 * i);
}
}
void hatch(Punto a, Punto b, Punto c, Punto d) {
int num = round(((dist(a.x, a.y, b.x, b.y)+dist(c.x, c.y, d.x, d.y))/2) / anchoTrama);
float incx1 = (b.x - a.x) / (float)(num - 1);
float incy1 = (b.y - a.y) / (float)(num - 1);
float incx2 = (d.x - c.x) / (float)(num - 1);
float incy2 = (d.y - c.y) / (float)(num - 1);
for (int i = 0; i < num; i++) {
line(a.x + incx1 * i, a.y + incy1 * i,
c.x + incx2 * i, c.y + incy2 * i);
}
}
void regen(){
puntos = new Punto[numPuntos];
achurados = new ArrayList();
creaPuntos();
creaAchurados();
width = ancho;
height = alto;
}
void zoomIn() {
float inc = 10;
for (int i = 0; i < puntos.length; i++) {
puntos[i].x = map(puntos[i].x, 0, ancho, -inc, ancho + inc);
puntos[i].y = map(puntos[i].y, 0, alto, -inc, alto + inc);
}
}
void zoomOut() {
float inc = -10;
for (int i = 0; i < puntos.length; i++) {
puntos[i].x = map(puntos[i].x, 0, ancho, -inc, ancho + inc);
puntos[i].y = map(puntos[i].y, 0, alto, -inc, alto + inc);
}
}
void updateAnchoTrazo(float at) {
anchoTrazo = at;
}
void updateAnchoTrama(float at) {
anchoTrama = at;
}
void addNoise() {
for (int i = 0; i < puntos.length; i++) {
puntos[i].x += (noise((millis()/2000 + puntos[i].x)/100, (millis()/2000 + puntos[i].y)/100, 0) - 0.5) * 10;
puntos[i].y += (noise((millis()/2000 + puntos[i].x)/100, (millis()/2000 + puntos[i].y)/100, 100) - 0.5) * 10;
}
}
void updateLineas(String col){
lineas = toColor(col);
}
void updateFondo(String col){
fondo = toColor(col);
}
color toColor(String s){
s = "FF" + s.substring(1);
return color(unhex(s));
}
void updateSize(int nuevoAncho, int nuevoAlto){
for (int i = 0; i < puntos.length; i++) {
puntos[i].x = map(puntos[i].x, 0, ancho, 0, nuevoAncho);
puntos[i].y = map(puntos[i].y, 0, alto, 0, nuevoAlto);
}
size(nuevoAncho, nuevoAlto);
ancho = nuevoAncho;
alto = nuevoAlto;
// width = ancho;
// height = alto;
}
void keyPressed() {
if (key == 'r') {
achurados.clear();
creaPuntos();
creaAchurados();
}
if (key == 's' || key == 'S') {
String filename = "img/hatch-"+year()+"_"+month()+"_"+day()+"___"+hour()+"-"+minute()+"-"+second()+".png";
saveFrame(filename);
// println("se ha grabado exitosamente el archivo "+filename);
}
if (key == 'a' || key == 'A') {
anchoTrazo += .5;
}
if (key == 'z' || key == 'Z') {
if (anchoTrazo >= 1) anchoTrazo -= .5;
}
if (key == 'd' || key == 'D') {
anchoTrama += .5;
}
if (key == 'c' || key == 'C') {
if (anchoTrama >= 1)anchoTrama -= .5;
}
if (key == 'n' || key == 'N') {
addNoise();
}
if (key == 'f' || key == 'F') {
zoomOut();
}
if (key == 'v' || key == 'V') {
zoomIn();
}
}
void mouseReleased() {
for (int i = 0; i < achurados.size(); i++) {
Achurado a = (Achurado)achurados.get(i);
if (a.over) {
a.horizontal = !a.horizontal;
}
}
}
void mouseDragged() {
for (int i = 0; i < puntos.length; i++) {
if (puntos[i].over()) {
puntos[i].x = mouseX;
puntos[i].y = mouseY;
puntos[i].over() = true;
}
}
}
class Punto {
float x, y;
int num;
Punto() {
x = random(m, width-m);
y = random(m, height-m);
}
Punto(float _x, float _y) {
x = _x;
y = _y;
num = 999;
}
Punto(float _x, float _y, int n) {
x = _x;
y = _y;
num = n;
}
boolean over() {
if (dist(x, y, mouseX, mouseY) < 8) {
return true;
}
else if(mousePressed == false){
return false;
}
}
void draw() {
fill(color(red(lineas)+20, green(lineas)+20, blue(lineas)+20));
noStroke();
ellipse(x, y, anchoTrazo*4, anchoTrazo*4);
}
}
class Achurado {
Punto a, b, c, d, centro;
float radio;
boolean over;
boolean horizontal;
Achurado(Punto a, Punto b, Punto c, Punto d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
float n = random(1);
if (n < .5) {
horizontal = true;
}
else {
horizontal = false;
}
// el promedio
float x = (a.x + b.x + c.x + d.x)/4;
float y = (a.y + b.y + c.y + d.y)/4;
centro = new Punto(x, y);
// println(centro.x);
}
void draw() {
calc();
strokeWeight(anchoTrazo);
stroke(lineas);
if (horizontal) {
hatch(a, b, c, d);
}
else {
hatch(c, a, d, b);
}
noFill();
quad(a.x, a.y, b.x, b.y, d.x, d.y, c.x, c.y);
}
void calc() {
centro.x = (a.x + b.x + c.x + d.x)/4;
centro.y = (a.y + b.y + c.y + d.y)/4;
float[] coordenadasX = {
a.x, b.x, c.x, d.x
};
float[] coordenadasY = {
a.y, b.y, c.y, d.y
};
float minX = min(coordenadasX);
float maxX = max(coordenadasX);
float minY = min(coordenadasY);
float maxY = max(coordenadasY);
radio = min((maxX - minX), (maxY - minY)) / 2;
if (dist(mouseX, mouseY, centro.x, centro.y) < radio) {
over = true;
}
else {
over = false;
}
}
}