-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle_packing.html
More file actions
431 lines (381 loc) · 14.1 KB
/
Copy pathcircle_packing.html
File metadata and controls
431 lines (381 loc) · 14.1 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>p5.js Polygon Filler</title>
<style>
html, body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background: #222;
color: #eee;
}
main {
display: flex;
gap: 20px;
padding: 20px;
flex-wrap: wrap;
justify-content: center;
}
#drawing-area {
display: flex;
flex-direction: column;
align-items: center;
}
#canvas-container {
border: 1px solid #444;
line-height: 0;
}
#caption {
color: #aaa;
font-size: 0.9em;
margin-top: 8px;
}
#controls {
display: flex;
flex-direction: column;
gap: 15px;
min-width: 250px;
}
#controls div {
display: grid;
grid-template-columns: 100px 1fr;
gap: 10px;
align-items: center;
}
#controls label {
text-align: right;
font-size: 0.9em;
}
#controls input {
width: 100%;
}
#controls button {
grid-column: 1 / -1;
padding: 10px;
font-size: 1em;
cursor: pointer;
}
#controls span {
font-size: 0.9em;
color: #aaa;
}
/* Updated style */
.algorithm-radio {
grid-column: 1 / -1;
display: grid; /* Use Grid */
grid-template-columns: 1fr; /* Force single column */
gap: 10px;
background: #333;
padding: 10px;
border-radius: 5px;
color: #eee;
}
.algorithm-radio label {
text-align: left;
display: flex; /* This is for the [o] and "Text" */
align-items: center;
gap: 5px;
color: #eee;
font-size: 1em;
margin-left: 10px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.min.js"></script>
<script src="https://unpkg.com/p5.js-svg/dist/p5.svg.js"></script>
</head>
<body>
<main>
<div id="controls">
<div>
<label for="padding">Padding:</label>
<input type="range" id="padding" min="0" max="50" value="2" step="1" oninput="this.nextElementSibling.textContent = this.value">
<span>2</span>
</div>
<div>
<label for="aSize">A Size:</label>
<input type="range" id="aSize" min="2" max="100" value="4" step="1" oninput="this.nextElementSibling.textContent = this.value">
<span>4</span>
</div>
<div>
<label for="bSize">B Size:</label>
<input type="range" id="bSize" min="2" max="100" value="20" step="1" oninput="this.nextElementSibling.textContent = this.value">
<span>20</span>
</div>
<div>
<label for="ratio">A to B Ratio:</label>
<input type="range" id="ratio" min="0" max="100" value="50" step="1" oninput="this.nextElementSibling.textContent = this.value + '%'">
<span>50%</span>
</div>
<div>
<label for="density">Density:</label>
<input type="range" id="density" min="0" max="100" value="50" step="1" oninput="this.nextElementSibling.textContent = this.value + '%'">
<span>50%</span>
</div>
<button id="fillButton">Fill Polygon</button>
<button id="clearButton">Clear</button>
<button id="exportButton">Export SVG</button>
</div>
<div id="drawing-area">
<div id="canvas-container"></div>
<p id="caption">Click to draw a polygon</p>
</div>
</main>
<script>
let vertices = [];
let circles = [];
let state = 'drawing'; // 'drawing' or 'filled'
let ui = {};
let canvas;
const SNAP_DISTANCE = 10;
function setup() {
canvas = createCanvas(800, 600, SVG);
canvas.parent('canvas-container');
ui.padding = select('#padding');
ui.aSize = select('#aSize');
ui.bSize = select('#bSize');
ui.ratio = select('#ratio');
ui.density = select('#density');
ui.fillButton = select('#fillButton');
ui.clearButton = select('#clearButton');
ui.exportButton = select('#exportButton');
ui.caption = select('#caption');
ui.algorithm = createRadio();
ui.algorithm.option('random', 'Random');
ui.algorithm.option('grid', 'Grid');
ui.algorithm.selected('random');
ui.algorithm.parent('controls');
ui.algorithm.addClass('algorithm-radio');
ui.fillButton.mousePressed(fillPolygon);
ui.clearButton.mousePressed(clearAll);
ui.exportButton.mousePressed(exportSVG);
loop();
}
function clearAll() {
vertices = [];
circles = [];
state = 'drawing';
loop();
}
function fillPolygon() {
if (vertices.length < 3) return;
state = 'filled';
generateCircles();
redraw();
noLoop();
}
function exportSVG() {
save('polygon-fill.svg');
}
function draw() {
background(80);
if (state === 'drawing') {
ui.caption.style('display', 'block');
drawPolygonVertices();
} else {
ui.caption.style('display', 'none');
drawFilledShape();
drawCircles();
}
}
function drawPolygonVertices() {
stroke(255);
strokeWeight(2);
fill(255, 50);
beginShape();
for (const v of vertices) {
vertex(v.x, v.y);
}
endShape();
noStroke();
fill(255, 150);
for (const v of vertices) {
ellipse(v.x, v.y, 8, 8);
}
if (vertices.length > 0 && mouseIsOnCanvas()) {
let targetX = mouseX;
let targetY = mouseY;
let isSnapping = false;
if (vertices.length > 2) {
const d = dist(mouseX, mouseY, vertices[0].x, vertices[0].y);
if (d < SNAP_DISTANCE) {
targetX = vertices[0].x;
targetY = vertices[0].y;
isSnapping = true;
}
}
stroke(255, isSnapping ? 255 : 100);
strokeWeight(isSnapping ? 2 : 1);
line(vertices[vertices.length - 1].x, vertices[vertices.length - 1].y, targetX, targetY);
if (isSnapping) {
fill(0, 255, 0, 200);
noStroke();
ellipse(vertices[0].x, vertices[0].y, 12, 12);
}
}
}
function drawFilledShape() {
noStroke();
fill(100);
beginShape();
for (const v of vertices) {
vertex(v.x, v.y);
}
endShape(CLOSE);
}
function drawCircles() {
noStroke();
fill(200, 255, 200, 150);
for (const c of circles) {
ellipse(c.x, c.y, c.r * 2, c.r * 2);
}
}
function mousePressed() {
if (state !== 'drawing' || !mouseIsOnCanvas()) return;
const p = createVector(mouseX, mouseY);
if (vertices.length > 2) {
const d = p.dist(vertices[0]);
if (d < SNAP_DISTANCE) {
state = 'filled';
fillPolygon();
return;
}
}
vertices.push(p);
}
function mouseIsOnCanvas() {
return mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height;
}
// --- Core Logic ---
function getPolygonArea(polygon) {
let area = 0.0;
let j = polygon.length - 1;
for (let i = 0; i < polygon.length; i++) {
area += (polygon[j].x + polygon[i].x) * (polygon[j].y - polygon[i].y);
j = i;
}
return abs(area / 2.0);
}
function generateCircles() {
circles = [];
const padding = float(ui.padding.value());
const aR = float(ui.aSize.value()) / 2;
const bR = float(ui.bSize.value()) / 2;
const ratioPercent = float(ui.ratio.value()) / 100.0;
const densityPercent = float(ui.density.value()) / 100.0;
const algorithm = ui.algorithm.value();
if (algorithm === 'random') {
generateCirclesRandom(padding, aR, bR, ratioPercent, densityPercent);
} else if (algorithm === 'grid') {
generateCirclesGrid(padding, aR, bR, ratioPercent, densityPercent);
}
}
function generateCirclesRandom(padding, aR, bR, ratioPercent, densityPercent) {
const avgR = (aR * (1.0 - ratioPercent)) + (bR * ratioPercent);
const avgCircleArea = PI * avgR * avgR;
if (avgCircleArea < 1e-6) return;
const polyArea = getPolygonArea(vertices);
const maxPossibleCircles = polyArea / avgCircleArea;
const numCircles = floor(maxPossibleCircles * densityPercent);
if (numCircles <= 0) return;
const minR = min(aR, bR);
const maxR = max(aR, bR);
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
for (const v of vertices) {
if (v.x < minX) minX = v.x;
if (v.x > maxX) maxX = v.x;
if (v.y < minY) minY = v.y;
if (v.y > maxY) maxY = v.y;
}
let attempts = 0;
const maxAttempts = numCircles * 200;
while (circles.length < numCircles && attempts < maxAttempts) {
attempts++;
const r = (random(1) < ratioPercent) ? bR : aR;
const p = createVector(
random(minX + r, maxX - r),
random(minY + r, maxY - r)
);
if (!isInside(p, vertices)) continue;
if (isTooCloseToEdge(p, r, vertices, padding)) continue;
let collision = false;
for (const c of circles) {
const d = dist(p.x, p.y, c.x, c.y);
if (d < r + c.r + padding) {
collision = true;
break;
}
}
if (collision) continue;
circles.push({ x: p.x, y: p.y, r: r });
}
}
function generateCirclesGrid(padding, aR, bR, ratioPercent, densityPercent) {
const minR = min(aR, bR);
const maxR = max(aR, bR);
const gridStep = (maxR * 2) + padding;
if (gridStep < 1) return;
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
for (const v of vertices) {
if (v.x < minX) minX = v.x;
if (v.x > maxX) maxX = v.x;
if (v.y < minY) minY = v.y;
if (v.y > maxY) maxY = v.y;
}
let validPoints = [];
for (let x = minX; x <= maxX; x += gridStep) {
for (let y = minY; y <= maxY; y += gridStep) {
const p = createVector(x, y);
if (isInside(p, vertices) && !isTooCloseToEdge(p, maxR, vertices, padding)) {
validPoints.push(p);
}
}
}
if (validPoints.length === 0) return;
const numCircles = floor(validPoints.length * densityPercent);
shuffle(validPoints, true);
for (let i = 0; i < numCircles; i++) {
const p = validPoints[i];
const r = (random(1) < ratioPercent) ? bR : aR;
if (!isTooCloseToEdge(p, r, vertices, padding)) {
circles.push({ x: p.x, y: p.y, r: r });
}
}
}
function isTooCloseToEdge(p, r, polygon, padding) {
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const a = polygon[j];
const b = polygon[i];
const dist = pointToLineSegmentDistance(p, a, b);
if (dist < r + padding) {
return true;
}
}
return false;
}
function isInside(p, polygon) {
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const vi = polygon[i];
const vj = polygon[j];
const intersect = ((vi.y > p.y) !== (vj.y > p.y))
&& (p.x < (vj.x - vi.x) * (p.y - vi.y) / (vj.y - vi.y) + vi.x);
if (intersect) inside = !inside;
}
return inside;
}
function pointToLineSegmentDistance(p, a, b) {
const vAP = p5.Vector.sub(p, a);
const vAB = p5.Vector.sub(b, a);
const magAB_sq = vAB.magSq();
if (magAB_sq === 0) return p.dist(a);
const t = p5.Vector.dot(vAP, vAB) / magAB_sq;
const t_clamped = constrain(t, 0, 1);
const projection = p5.Vector.add(a, p5.Vector.mult(vAB, t_clamped));
return p.dist(projection);
}
</script>
</body>
</html>