-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox_fitting_mod.pde
167 lines (133 loc) · 2.79 KB
/
box_fitting_mod.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
// mod by D.Guzzo -- dguzzo at gmail dot com
// Box Fitting
// j.tarbell January, 2004
// Processing 0085 Beta syntax update April, 2005
// Albuquerque, New Mexico
// complexification.net
int num = 0;
int maxnum = 1000;
int dim = 600;
Box[] boxes;
int maxpal = 256;
int numpal = 0;
color[] goodcolor = new color[maxpal];
// MAIN -----------------------------------------------------------
void setup() {
framerate(30);
rectMode(CENTER);
noStroke();
takecolor("bosque2.gif");
background(255);
boxes = new Box[maxnum];
for (int i=0;i<3;i++) {
makeNewBox();
}
}
void draw() {
for (int n=0;n<num;n++) {
boxes[n].draw();
}
}
void makeNewBox() {
if (num<maxnum) {
boxes[num] = new Box();
num++;
}
}
// OBJECTS ------------------------------------------------------
// space filling box
class Box {
int x;
int y;
int d;
color myc;
boolean okToDraw;
boolean chaste = true;
Box() {
// random initial conditions
selfinit();
}
void selfinit() {
// position
okToDraw = false;
x = int(random(dim));
y = int(random(dim));
d = 0;
myc = somecolor(1.0*y/dim);
}
void draw() {
expand();
if (okToDraw) {
fill(myc);
rect(x,y,d,d);
}
}
void expand() {
// assume expansion is ok
d+=2;
// look for obstructions around perimeter at width d
int obstructions = 0;
for (int j=int(x-d/2-1);j<int(x+d/2);j++) {
int k=int(y-d/2-1);
obstructions += checkPixel(j,k);
k=int(y+d/2);
obstructions += checkPixel(j,k);
}
for (int k=int(y-d/2-1);k<int(y+d/2);k++) {
int j=int(x-d/2-1);
obstructions += checkPixel(j,k);
j=int(x+d/2);
obstructions += checkPixel(j,k);
}
if (obstructions>0) {
// reset
selfinit();
if (chaste) {
makeNewBox();
chaste = false;
}
} else {
okToDraw = true;
}
}
int checkPixel(int x, int y) {
color c = get(x, y);
if (brightness(c)<254) {
// a lit pixel has been found
return 1;
} else {
return 0;
}
}
}
// COLOR METHODS ---------------------------------------------------
color somecolor(float p) {
// pick color according to range
return goodcolor[int(p*numpal)];
}
void takecolor(String fn) {
PImage b;
b = loadImage(fn);
image(b,0,0);
for (int x=0;x<b.width;x++){
for (int y=0;y<b.height;y++) {
color c = get(x,y);
boolean exists = false;
for (int n=0;n<numpal;n++) {
if (c==goodcolor[n]) {
exists = true;
break;
}
}
if (!exists) {
// add color to pal
if (numpal<maxpal) {
goodcolor[numpal] = c;
numpal++;
} else {
break;
}
}
}
}
}