-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcirclePacking2.mjs
More file actions
207 lines (186 loc) · 4.4 KB
/
circlePacking2.mjs
File metadata and controls
207 lines (186 loc) · 4.4 KB
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
// https://thecodingtrain.com/challenges/50-animated-circle-packing
import chroma from 'chroma-js'
import ControlPanel, { Range, Select } from '../lib/ControlPanel/index.mjs'
/**
* @param {import('p5')} p
*/
export default function (p) {
const metadata = {
name: 'circlePacking2',
frameRate: 30,
pixelDensity: 6,
}
const [w, h] = [500, 500]
const scales = {
PiYG: chroma.scale('PiYG'),
cmk: chroma.scale(['cyan', 'magenta', 'black']),
}
const schemes = {
PiYG: () => scales.PiYG(p.random()).rgba(),
black: () => 'black',
cmk: () => scales.cmk(p.random()).rgba(),
}
const controlPanel = new ControlPanel({
p,
id: metadata.name,
autoRedraw: false,
controls: {
colorScheme: new Select({
name: 'colorScheme',
value: 'cmk',
options: Object.keys(schemes),
}),
mode: new Select({
name: 'mode',
value: 'stroke',
options: ['fill', 'stroke'],
}),
minRadius: new Range({
name: 'minRadius',
value: 2,
min: 1,
max: 100,
}),
total: new Range({
name: 'total',
value: 5,
min: 1,
max: 20,
}),
},
})
const circles = []
const buffer = 1
let done = false
function setup() {
controlPanel.init()
const canvas = p.createCanvas(w, h)
p.colorMode(p.RGB, 255, 255, 255, 1)
p.noLoop()
console.group('Setup')
console.time('Execution time')
console.log('Creating circles')
while (!done) {
done = generateCircles()
}
console.log({ circles })
console.log('Done')
console.timeEnd('Execution time')
console.groupEnd('Setup')
return {
canvas,
}
}
function draw() {
p.background(255)
circles.forEach((circle) => {
circle.show()
})
}
function generateCircles() {
const { colorScheme, mode, minRadius, total } = controlPanel.values()
let count = 0
const maxFailedAttempts = 10_000
let failedAttempts = 0
let finished = false
const pad = 20
const box = {
x: pad,
y: pad,
w: w - pad * 2,
h: h - pad * 2,
}
while (count < total) {
let valid = true
const x = p.random(box.x, box.x + box.w)
const y = p.random(box.y, box.y + box.h)
for (let i = 0; i < circles.length; i++) {
const circle = circles[i]
const d = p.dist(x, y, circle.x, circle.y)
if (d < circle.r + minRadius + buffer) {
valid = false
break
}
}
if (valid) {
const color = schemes[colorScheme]()
const circle = new Circle(x, y, color, mode, minRadius, box)
circles.push(circle)
count++
} else {
failedAttempts++
if (failedAttempts > maxFailedAttempts) {
finished = true
break
}
}
}
for (let i = 0; i < circles.length; i++) {
const circle = circles[i]
if (circle.growing) {
if (circle.isWithinBoundaries()) {
for (let j = 0; j < circles.length; j++) {
const other = circles[j]
if (circle !== other) {
const distance = p.dist(circle.x, circle.y, other.x, other.y)
const combinedRadii = circle.r + other.r + buffer
if (distance < combinedRadii) {
circle.growing = false
break
}
}
}
} else {
circle.growing = false
}
}
circle.grow()
}
return finished
}
class Circle {
constructor(x, y, color, mode, minRadius, box) {
this.x = x
this.y = y
this.r = minRadius
this.growing = true
this.color = color
this.strokeWeight = 1
this.mode = mode
this.box = box
}
grow() {
if (this.growing) {
this.r += 1
}
}
show() {
if (this.mode === 'stroke') {
p.noFill()
p.strokeWeight(this.strokeWeight)
p.stroke(this.color)
} else {
p.noStroke()
p.fill(this.color)
}
p.circle(this.x, this.y, this.r * 2)
}
isWithinBoundaries() {
const { x, y, w, h } = this.box
return (
this.x - this.r > x &&
this.x + this.r < x + w &&
this.y - this.r > y &&
this.y + this.r < y + h
)
}
}
return {
setup,
draw,
destroy() {
controlPanel.destroy()
},
metadata,
}
}