-
Notifications
You must be signed in to change notification settings - Fork 0
/
composition.pde
183 lines (161 loc) · 5.95 KB
/
composition.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
ArrayList v_array = new ArrayList();
ArrayList h_array = new ArrayList();
ArrayList colors = new ArrayList();
colors.add(color(175,25,25)); // red
colors.add(color(175,25,25)); // red
colors.add(color(28,24,160)); // blue
colors.add(color(28,24,160)); // blue
colors.add(color(240,204,22)); // yellow
colors.add(color(240,204,22)); // yellow
colors.add(color(18, 13, 10)); // black
colors.add(color(187, 189, 194)); // grey
colors.add(color(255,255,255)); // white
var complexity = $(document).data("complexity");
var thickness = $(document).data("thickness");
void illus() {
var w = $(document).data("width");
var h = $(document).data("height");
size(w, h);
v_array.clear();
h_array.clear();
v_array.add(0);
h_array.add(0);
background(255,255,255);
smooth();
stroke(000);
int s_w = int(random(int((thickness*w)/125),int((thickness*w)/75)));
int adj = int(s_w/2);
strokeWeight(s_w);
int min_w = int((complexity*w)/14);
int max_w = int((complexity*w)/6);
int min_h = int((complexity*h)/14);
int max_h = int((complexity*h)/6);
//adding lines to v_array and h_array
//first we add a line that can be close to the left side b/c it is possible for this to happen on the right
int x_pos = int(random(s_w, max_w));
v_array.add(x_pos);
line(x_pos, 0, x_pos, h);
while (true) {
x_pos = x_pos + int(random(min_w, max_w));
if (x_pos < w) {
v_array.add(x_pos);
line(x_pos, 0, x_pos, h);
}
else {
break;
}
}
//we do the same thing here b/c lines can be close to the bottom
int y_pos = int(random(s_w, max_w));
h_array.add(y_pos);
line(0, y_pos, w, y_pos);
while (true) {
y_pos = y_pos + int(random(min_h, max_h));
if (y_pos < h) {
h_array.add(y_pos);
line(0, y_pos, w, y_pos);
}
else {
break;
}
}
// adding the right-most and bottom-most bounds
v_array.add(w);
h_array.add(h);
//creating an arraylist to store the {x1,y1,x2,y2} coords of each rectangle
ArrayList spaces = new ArrayList();
// finding all the locations for rectangles
for (int y = 0; y < (h_array.size() - 1); y++){ // for every line in h_array except the last
for (int x = 0; x < (v_array.size() - 1); x++) { // for ever line in v_array except the last
int[] dimensions = {v_array.get(x), h_array.get(y), v_array.get(x + 1), h_array.get(y + 1)};
spaces.add(dimensions);
}
}
int max_squares = v_array.size()+h_array.size();
int num_squares = int(random(3, max_squares));
int painted_squares = 0;
ArrayList occupied = new ArrayList();
occupied.add(-1);
boolean moved_left = false;
boolean moved_right = false;
while(painted_squares < num_squares) {
int index = -1;
while(occupied.contains(index)) {
index = int(random(0, spaces.size()));
}
int[] dimensions = spaces.get(index);
occupied.add(index);
painted_squares = painted_squares + 1;
//moving right
if(painted_squares < num_squares && !((index + 1) % (v_array.size() - 1) == 0) && !(occupied.contains(index + 1)) ) {
if (int(random(0, 2))==1) {
dimensions[2] = spaces.get(index + 1)[2];
painted_squares = painted_squares + 1;
occupied.add(index + 1);
moved_right = true;
}
}
//moving left
if(painted_squares < num_squares && !((index) % (v_array.size() - 1) == 0) && !(occupied.contains(index - 1)) ) {
if (int(random(0, 2))==1) {
dimensions[0] = spaces.get(index - 1)[0];
painted_squares = painted_squares + 1;
occupied.add(index - 1);
moved_left = true;
}
}
//moving down
if(painted_squares < num_squares && !(occupied.contains(index + v_array.size() - 1)) && (index + v_array.size() - 1) < spaces.size()) {
if(!(moved_right && occupied.contains(index+v_array.size()))
&& !(moved_left && occupied.contains(index + v_array.size() - 2)) && int(random(0, 2)) == 1) {
dimensions[3] = spaces.get(index + v_array.size() - 1)[3];
painted_squares = painted_squares + 1;
occupied.add(index + v_array.size() - 1);
if(moved_right) {
occupied.add(index+v_array.size());
painted_squares = painted_squares + 1;
}
if(moved_left) {
occupied.add(index+v_array.size()-2);
painted_squares = painted_squares + 1;
}
}
}
//moving up
if(painted_squares < num_squares && !(occupied.contains(index - v_array.size() + 1)) && (index - v_array.size() + 1) >= 0) {
if(!(moved_right && occupied.contains(index - v_array.size() + 2))
&& !(moved_left && occupied.contains(index - v_array.size())) && int(random(0, 2)) == 1) {
dimensions[1] = spaces.get(index - v_array.size() + 1)[1];
painted_squares = painted_squares + 1;
occupied.add(index - v_array.size() + 1);
if(moved_right) {
occupied.add(index - v_array.size() + 2);
painted_squares = painted_squares + 1;
}
if(moved_left) {
occupied.add(index - v_array.size());
painted_squares = painted_squares + 1;
}
}
}
// adjusting the dimensions to compensate for the thickness of the lines
if(dimensions[0] != 0) {
dimensions[0] = dimensions[0] + adj;
}
if(dimensions[1] != 0) {
dimensions[1] = dimensions[1] + adj;
}
if(dimensions[2] != w) {
dimensions[2] = dimensions[2] - adj;
}
if(dimensions[3] != h) {
dimensions[3] = dimensions[3] - adj;
}
noStroke();
fill(colors.get(int(random(0, colors.size()))));
rect(dimensions[0], dimensions[1], (dimensions[2]-dimensions[0]), (dimensions[3]-dimensions[1]));
}
}
void setup() {
illus();
}