-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaic.pde
68 lines (58 loc) · 1.35 KB
/
mosaic.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
class Mosaic {
int ballSize = 5, cols, rows;
float space, mWidth, mHeight, depth=(width+height)/2;
PImage img;
Ball[] balls = new Ball[40000];
Mosaic() {
init();
}
Mosaic( int ballSize_) {
ballSize = ballSize_;
space = ballSize*2.3;
init();
}
Mosaic(int ballSize_, int space_) {
ballSize = ballSize_;
space = space_;
init();
}
void init() {
float r = sqrt(width*width+height*height+depth*depth);
for (int i=0;i<balls.length;i++) {
balls[i] = new Ball(random(-r, r),
random(-r, r),
random(-r, r),
ballSize, color(255, 0));
}
}
void animateTo(String imgPath) {
img = loadImage(imgPath);
rows = img.height;
cols = img.width;
mHeight = rows*space;
mWidth = cols*space;
pushMatrix();
translate(-mWidth/2, -mHeight/2, 0);
int count=0;
for (int i=0;i<cols;i++) {
for (int j=0;j<rows;j++) {
color c = img.pixels[i+j*cols];
if (alpha(c) != 0) {
balls[count].moveTo(i*space, j*space, 0);
balls[count].colorTo(c);
balls[count].display();
count++;
}
}
}
for (int i = count-1;count<balls.length;count++) {
Ball b = balls[count];
if (alpha(b.c) > 0) {
b.home();
b.colorTo(color(b.c, 0));
b.display();
}
}
popMatrix();
}
}