-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday_14.pde
321 lines (264 loc) · 6.56 KB
/
day_14.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
import java.util.List;
float margin = 80;
/**
* 2D vector class of int
*/
class IntVector2D {
int x, y;
IntVector2D(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Copy constructor
*/
IntVector2D copy() {
return new IntVector2D(x, y);
}
}
/**
* Stone, storing a location and a size
*/
class Stone {
IntVector2D location;
float size;
Stone(IntVector2D location, float size) {
this.location = location;
this.size = size;
}
}
/**
* Compute the taxicab distance
* See : https://en.wikipedia.org/wiki/Taxicab_geometry
*/
int taxicabDistance(IntVector2D p, IntVector2D q) {
return abs(p.x - q.x) + abs(p.y - q.y);
}
/**
* The class representing the manhattan grid
*/
class ManhattanGrid {
// The gap between lines on both directions
float gapX, gapY;
// The number of rows and columns
int cols, rows;
// The list of points on the grid
List<IntVector2D> points;
// Points put down some stones when they move
List<Stone> stones;
ManhattanGrid(int cols, int rows) {
this.cols = cols;
this.rows = rows;
gapX = (width - 2 * margin) / cols;
gapY = (height - 2 * margin) / rows;
points = new ArrayList<IntVector2D>();
stones = new ArrayList<Stone>();
}
/**
* Add a point on the grid
*/
void addPoint(int x, int y) {
points.add(new IntVector2D(x, y));
}
/**
* Add a point on the grid at a random location
*/
void addRandomPointOnGrid() {
addPoint(int(random(cols)), int(random(rows)));
}
/**
* Randomly populate the grid with points
*/
void randomlyPopulate(int n) {
for (int i = 0; i < n; i++) {
addRandomPointOnGrid();
}
}
/**
* Return true if the cell is not occupied
*/
boolean isCellFree(int x, int y) {
// Check for stones
for (Stone stone : stones) {
if (stone.location.x == x && stone.location.y == y) {
return false;
}
}
// Check for points
for (IntVector2D point : points) {
if (point.x == x && point.y == y) {
return false;
}
}
return true;
}
/**
* Add Brownian motion to the points on the grid
* See : https://en.wikipedia.org/wiki/Brownian_motion
*/
void addBrownianMotion() {
int stopped = 0;
// For each point
for (int i = 0; i < points.size(); i++) {
IntVector2D point = points.get(i);
// Add a stone at that place
float normDistToNext = getNormalizedDistance(point, points.get((i + 1) % points.size()));
stones.add(new Stone(point.copy(), normDistToNext * 20));
// Stores the possible directions
int[] free = new int[4];
int added = 0;
// Check if it can move
if (point.x > 0 && isCellFree(point.x - 1, point.y)) free[added++] = 0;
if (point.x < cols && isCellFree(point.x + 1, point.y)) free[added++] = 1;
if (point.y > 0 && isCellFree(point.x, point.y - 1)) free[added++] = 2;
if (point.y < rows && isCellFree(point.x, point.y + 1)) free[added++] = 3;
// If we can't go anywhere break
if (added == 0) {
stopped++;
continue;
}
// Choose a random direction from previous ones
int choice = free[int(random(added))];
// Do the appropriate operation
if (choice == 0) point.x--;
else if (choice == 1) point.x++;
else if (choice == 2) point.y--;
else if (choice == 3) point.y++;
}
// Stop the sketch if all the points are stopped
if (stopped == points.size()) noLoop();
}
/**
* Return the x coordinate of the point on the screen with the col number
*/
float getPointX(int col) {
return margin + gapX * col;
}
/**
* Same as getPointX() but for the row
*/
float getPointY(int row) {
return margin + gapY * row;
}
/**
* Return the normalized taxicab distance between two points
*/
float getNormalizedDistance(IntVector2D p, IntVector2D q) {
return taxicabDistance(p, q) / (float) (cols + rows);
}
/*
* Display the taxicab distance lines
*/
void displayPathBetween(IntVector2D p, IntVector2D q) {
// Get points coordinates
float px = getPointX(p.x);
float py = getPointY(p.y);
float qx = getPointX(q.x);
float qy = getPointY(q.y);
strokeWeight(map(getNormalizedDistance(p, q), 0, 1, 8, 0));
stroke(0, 20);
// If not aligned on the x axis
if (p.x != q.x) {
// Display the x line
line(px, py, qx, py);
}
// If not aligned on the y axis
if (p.y != q.y) {
// Display the x line
line(qx, qy, qx, py);
}
}
/**
* Display the grid lines
*/
void displayGrid() {
strokeWeight(2);
stroke(0, 20);
// Vertical lines
for (int i = 0; i <= cols; i++) {
float x = getPointX(i);
line(x, margin, x, height - margin);
}
// Horizontal lines
for (int i = 0; i <= rows; i++) {
float y = getPointY(i);
line(margin, y, width - margin, y);
}
}
/**
* Display the taxicab distances between all the points
*/
void displayConnections() {
for (int i = 0; i < points.size(); i++) {
for (int j = 0; j < points.size(); j++) {
if (i != j) {
displayPathBetween(points.get(i), points.get(j));
}
}
}
}
/**
* Display the points on the grid
*/
void displayPoints() {
stroke(0);
strokeWeight(15);
for (IntVector2D point : points) {
point(getPointX(point.x), getPointY(point.y));
}
}
/**
* Display the stones as circles
*/
void displayStones() {
for (Stone stone : stones) {
//stroke(0, 200);
//noFill();
noStroke();
fill(0, 100);
//strokeWeight(1);
circle(getPointX(stone.location.x), getPointY(stone.location.y), stone.size);
}
}
/**
* Display all the elements on the screen
*/
void display() {
displayGrid();
displayConnections();
displayStones();
displayPoints();
}
/**
* Update the grid
*/
void update() {
// Move the points
addBrownianMotion();
}
}
int updateFrame = 10;
ManhattanGrid grid;
void setup() {
size(500, 500);
// Create the grid with random points
grid = new ManhattanGrid(20, 20);
grid.randomlyPopulate(10);
updateView();
}
/**
* Redraw the background and the grid
* Prevent from redrawing 60 times per second because the
* grid methods are not well optimized and do a lot of loops...
*/
void updateView() {
background(255);
grid.display();
grid.update();
}
void draw() {
// Every updateFrame, redraw the screen
if (frameCount % updateFrame == 0) {
updateView();
}
}