-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAesthetic.html
More file actions
308 lines (255 loc) · 9.83 KB
/
Aesthetic.html
File metadata and controls
308 lines (255 loc) · 9.83 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2D Particles Elastic Collisions Simulator</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
canvas {
display: block;
margin: 0 auto;
background-color: black;
max-width: 100%;
}
.controls {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 1rem;
margin-bottom: 1rem;
}
.controls > div {
display: flex;
flex-direction: column;
align-items: center;
}
button {
background-color: #4CAF50;
border: none;
color: white;
padding: 8px 16px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
transition-duration: 0.4s;
}
button:hover {
background-color: #45a049;
}
@media (max-width: 768px) {
.controls {
flex-direction: column;
}
}
</style>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-416T4GK1DB"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-416T4GK1DB');
</script>
</head>
<body>
<canvas id="simulation" width="720" height="720"></canvas>
<div class="controls">
<label for="max_depth">Max Depth:</label>
<input type="range" id="max_depth" name="max_depth" min="1" max="8" step="1" value="7">
<input type="number" id="max_depth_value" value="7" min="1" max="8" step="1">
</div>
<div class="controls">
<label for="particle_speed">Particle Speed:</label>
<input type="range" id="particle_speed" name="particle_speed" min="0" max="5" step="0.1" value="3.5">
<input type="number" id="particle_speed_value" value="3.5" min="0" max="5" step="0.1">
</div>
<div class="controls">
<label for="num_particles">Number of Particles:</label>
<input type="range" id="num_particles" name="num_particles" min="3" max="50" value="16">
<input type="number" id="num_particles_value" value="16" min="3" max="50">
</div>
<button id="refresh">Refresh</button>
<script>
class Particle {
constructor(x, y, dx, dy) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.radius = 10;
this.color = 'white';
}
move() {
this.x += this.dx;
this.y += this.dy;
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
wallCollision(width, height) {
if (this.x - this.radius < 0 || this.x + this.radius > width) {
this.dx = -this.dx;
}
if (this.y - this.radius < 0 || this.y + this.radius > height) {
this.dy = -this.dy;
}
}
}
function distance(p1, p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
function resolveCollision(p1, p2) {
const dist = distance(p1, p2);
if (dist <= p1.radius + p2.radius) {
const nx = (p2.x - p1.x) / dist;
const ny = (p2.y - p1.y) / dist;
const tx = -ny;
const ty = nx;
const dp1n = p1.dx * nx + p1.dy * ny;
const dp2n = p2.dx * nx + p2.dy * ny;
const dp1t = p1.dx * tx + p1.dy * ty;
const dp2t = p2.dx * tx + p2.dy * ty;
const p1NewNormalSpeed = dp2n;
const p2NewNormalSpeed = dp1n;
p1.dx = p1NewNormalSpeed * nx + dp1t * tx;
p1.dy = p1NewNormalSpeed * ny + dp1t * ty;
p2.dx = p2NewNormalSpeed * nx + dp2t * tx;
p2.dy = p2NewNormalSpeed * ny + dp2t * ty;
/*const overlap = (p1.radius + p2.radius) - dist;
const separationX = overlap * nx / 2;
const separationY = overlap * ny / 2;
p1.x += separationX;
p1.y += separationY;
p2.x -= separationX;
p2.y -= separationY;*/
}
}
function drawLine(ctx, p1, p2) {
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.strokeStyle = p1.color;
ctx.stroke();
}
function drawPolygons(particles, ctx, depth, ratio) {
if (depth === 0 || particles.length < 3) {
return;
}
const newParticles = [];
for (let i = 0; i < particles.length; i++) {
const p1 = particles[i];
const p2 = particles[(i + 1) % particles.length];
const x = p1.x + ratio * (p2.x - p1.x);
const y = p1.y + ratio * (p2.y - p1.y);
const newParticle = new Particle(x, y, 0, 0);
drawLine(ctx, p1, newParticle);
newParticles.push(newParticle);
}
drawPolygons(newParticles, ctx, depth - 1, ratio);
}
const canvas = document.getElementById('simulation');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
let numParticles = 16;
let particles = [];
const centerX = width / 2;
const centerY = height / 2;
let initialSpeed = 3.5;
const circleRadius = 150;
for (let i = 0; i < numParticles; i++) {
const angle = 2 * Math.PI * i / numParticles;
const x = centerX + circleRadius * Math.cos(angle);
const y = centerY + circleRadius * Math.sin(angle);
const dx = initialSpeed * Math.cos(angle);
const dy = initialSpeed * Math.sin(angle);
particles.push(new Particle(x, y, dx, dy));
}
let ratio = 0;
const ratioSpeed = 0.003;
let maxDepth = 64;
function animate() {
ctx.clearRect(0, 0, width, height);
for (const p of particles) {
p.move();
p.wallCollision(width, height);
p.draw(ctx);
}
for (let i = 0; i < particles.length; i++) {
for (let j = i + 1; j < particles.length; j++) {
resolveCollision(particles[i], particles[j]);
}
}
drawPolygons(particles, ctx, maxDepth, ratio);
ratio += ratioSpeed;
if (ratio > 1) {
ratio = 0;
}
requestAnimationFrame(animate);
}
animate();
const maxDepthInput = document.getElementById('max_depth');
const maxDepthValue = document.getElementById('max_depth_value');
const particleSpeedInput = document.getElementById('particle_speed');
const particleSpeedValue = document.getElementById('particle_speed_value');
const numParticlesInput = document.getElementById('num_particles');
const numParticlesValue = document.getElementById('num_particles_value');
const refreshButton = document.getElementById('refresh');
maxDepthInput.addEventListener('input', () => {
maxDepthValue.value = maxDepthInput.value;
maxDepth = Math.pow(2,parseFloat(maxDepthValue.value));
});
maxDepthValue.addEventListener('input', () => {
maxDepthInput.value = maxDepthValue.value;
maxDepth = Math.pow(2,parseFloat(maxDepthValue.value));
});
particleSpeedInput.addEventListener('input', () => {
particleSpeedValue.value = particleSpeedInput.value;
initialSpeed = parseFloat(particleSpeedInput.value);
});
particleSpeedInput.addEventListener('input', () => {
particleSpeedInput.value = particleSpeedValue.value;
initialSpeed = parseFloat(particleSpeedValue.value);
});
numParticlesInput.addEventListener('input', () => {
numParticlesValue.value = numParticlesInput.value;
numParticles = parseInt(numParticlesInput.value);
});
numParticlesValue.addEventListener('input', () => {
numParticlesInput.value = numParticlesValue.value;
numParticles = parseInt(numParticlesValue.value);
});
refreshButton.addEventListener('click', () => {
particles.length = 0;
ratio = 0;
for (let i = 0; i < numParticles; i++) {
const angle = 2 * Math.PI * i / numParticles;
const x = centerX + circleRadius * Math.cos(angle);
const y = centerY + circleRadius * Math.sin(angle);
const dx = initialSpeed * Math.cos(angle);
const dy = initialSpeed * Math.sin(angle);
particles.push(new Particle(x, y, dx, dy));
}
});
</script>
</body>
</html>