-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCirclePacking.pde
72 lines (65 loc) · 1.34 KB
/
CirclePacking.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
ArrayList<Circle> circles;
PImage img;
void setup(){
size(421, 441);
img = loadImage("logo.png");
img.loadPixels();
circles = new ArrayList<Circle>();
}
void draw(){
background(0);
int total = 100;
int count = 0;
int attempts = 0;
while (count < total){
Circle newC = newCircle();
if (newC != null){
circles.add(newC);
count++;
}
attempts++;
if (attempts > 5000){
noLoop();
println("Finished");
break;
}
}
for(Circle c : circles){
if (c.growing){
if (c.edges()){
c.growing = false;
}else{
for (Circle other : circles){
if(c != other){
float d = dist(c.x, c.y, other.x, other.y);
if (d - 2 < c.r + other.r){
c.growing = false;
break;
}
}
}
}
}
c.show();
c.grow();
}
}
Circle newCircle(){
float x = random(width);
float y = random(height);
boolean valid = true;
for (Circle c : circles){
float d = dist(x,y,c.x,c.y);
if (d < c.r + 2){
valid = false;
break;
}
}
if (valid){
int index = int(x) + int(y) * img.width;
color col = img.pixels[index];
return new Circle(x,y,col);
}else{
return null;
}
}