-
Notifications
You must be signed in to change notification settings - Fork 0
/
circlePacking.js
135 lines (107 loc) · 2.65 KB
/
circlePacking.js
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
// Based on https://www.youtube.com/watch?v=QHEQuoIKgNE
class Circle {
constructor(x, y) {
this.x = x;
this.y = y;
this.r = 0;
this.growing = true;
}
show() {
push();
const sb = 150;
const hu = map(this.r, 0, 40, 0, 255, true);
stroke(hu, sb, sb);
fill(hu, sb, sb);
strokeWeight(1);
ellipse(this.x, this.y, this.r*2);
pop();
}
grow() {
if (this.growing && !this.edges() && !this.collide()) {
++this.r;
}
}
edges() {
const result = this.x + this.r > width ||
this.x - this.r < 0 ||
this.y + this.r > height ||
this.y - this.r < 0;
if (result) {
this.growing = false;
}
return result;
}
collide() {
for (const other of circles) {
if (other != this) {
const d = dist(this.x, this.y, other.x, other.y);
if (d - 1 < this.r + other.r) {
this.growing = false;
return true;
}
}
}
return false;
}
}
let circles = [];
let spots = [];
let image;
let fr;
function preload() {
image = loadImage('data/2017.png', function(){}, function(e) {console.log(e);});
}
function setup() {
createCanvas(900, 400);
colorMode(HSB, 255);
circles.push(new Circle(200, 200));
image.loadPixels();
for (const x of Array(image.width).keys()) {
for (const y of Array(image.height).keys()) {
const index = (x + y * image.width) * 4;
if (image.pixels[index+0] > 1 &&
image.pixels[index+1] > 1 &&
image.pixels[index+2] > 1) {
spots.push(createVector(x, y));
}
}
}
fr = createP('');
}
function draw() {
background(30);
newCircle();
for (const c of circles) {
c.show();
c.grow();
}
if (frameCount % 5 == 0) {
fr.html('FPS: ' + floor(frameRate()));
}
}
function newCircle() {
let attempts = 0;
let n = 0;
while (n < 100) {
const pos = random(spots);
let valid = true;
for (const c of circles) {
const d = dist(pos.x, pos.y, c.x, c.y);
if (d < c.r) {
// we are inside a circle
valid = false;
break;
}
}
if (valid) {
const c = new Circle(pos.x, pos.y);
circles.push(c);
++n;
}
++attempts;
if (attempts > 1000) {
noLoop();
break;
}
}
}