-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
182 lines (144 loc) · 4.27 KB
/
Copy pathdraw.js
File metadata and controls
182 lines (144 loc) · 4.27 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
// Particle simulation with attraction/repulsion and toroidal wrapping
let particles = [];
let inertia = 0.5;
let forceConstant = 400;
let attractionMode = -1; // -1: opposites attract, +1: same attract
let minDistanceFactor = 5;
let roundingPrecision = 1e7;
let useLinearDecay = 1;
let animationId = null;
const amount=441;
let centerOffsetX = 0;
let centerOffsetY = 0;
let canvas;
let ctx;
// Initialize the grid of particles
function initializeParticles() {
const grid = [];
let x = 0;
let y = 0;
for (let i = 0; i < amount; i++) {
const charge = (i % 2 === 0) ? +1 : -1;
grid.push([(x + 3) * 30, (y + 3) * 30, 0, 0, charge]);
x++;
if (x >= 21) {
x = 0;
y++;
}
}
return grid;
}
// Start animation
function startSimulation() {
if (animationId) return; // Already running
animate();
}
// Stop animation
function stopSimulation() {
if (animationId) cancelAnimationFrame(animationId);
animationId = null;
}
function drawParticlesOnce() {
const W = ctx.canvas.width;
const H = ctx.canvas.height;
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, W, H);
for (const p of particles) {
ctx.fillStyle = p[4] === 1 ? '#0f0' : '#f00';
ctx.fillRect(p[0], p[1], 5, 5);
}
}
// Initialize canvas and particles
function init() {
canvas = document.getElementById('myCanvas');
ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 800;
// Read controls only once for initialization
centerOffsetX = parseFloat(document.getElementById("cx").value);
centerOffsetY = parseFloat(document.getElementById("cy").value);
particles = initializeParticles();
if (document.getElementById("sd").checked) {
for (const p of particles) {
p[0] += centerOffsetX - Math.random() * 2 * centerOffsetX;
p[1] += centerOffsetY - Math.random() * 2 * centerOffsetY;
}
} else {
particles[220][0] += centerOffsetX;
particles[220][1] += centerOffsetY;
}
drawParticlesOnce();
}
// Animation loop
function animate() {
// Read controls dynamically every frame
attractionMode = document.getElementById("polar").checked ? -1 : +1;
forceConstant = parseFloat(document.getElementById("k").value);
minDistanceFactor = parseFloat(document.getElementById("kk").value);
inertia = parseFloat(document.getElementById("inert").value);
roundingPrecision = parseFloat(document.getElementById("symmetry").value);
useLinearDecay = document.getElementById("linearDecay").checked;
const W = ctx.canvas.width;
const H = ctx.canvas.height;
// Clear canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, W, H);
// Draw particles
for (const p of particles) {
ctx.fillStyle = p[4] === 1 ? '#0f0' : '#f00';
ctx.fillRect(p[0], p[1], 5, 5);
}
// Compute forces with symmetry
const netForces = particles.map(() => [0, 0]);
for (let i = 0; i < particles.length; i++) {
const [x0, y0, , , q0] = particles[i];
for (let j = i + 1; j < particles.length; j++) {
const [x1, y1, , , q1] = particles[j];
/*
// Toroidal wrapping
let dx = x1 - x0;
let dy = y1 - y0;
dx = ((dx + W / 2) % W) - W / 2;
dy = ((dy + H / 2) % H) - H / 2;
// % isn't mod
*/
let dx = (x1 - x0 + W) % W;
if (dx > W / 2) dx -= W;
let dy = (y1 - y0 + H) % H;
if (dy > H / 2) dy -= H;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance === 0) continue;
let decayDenominator;
if (useLinearDecay) {
decayDenominator = distance * distance; // 1/r decay
} else {
decayDenominator = distance * distance * distance; // Coulomb-like decay
}
if (decayDenominator <= forceConstant * minDistanceFactor) continue;
const factor = q0 * q1 * attractionMode * forceConstant / decayDenominator;
const fx = Math.round(dx * factor * roundingPrecision) / roundingPrecision;
const fy = Math.round(dy * factor * roundingPrecision) / roundingPrecision;
// Symmetric application
netForces[i][0] += fx;
netForces[i][1] += fy;
netForces[j][0] -= fx;
netForces[j][1] -= fy;
}
}
// Update positions
for (let i = 0; i < particles.length; i++) {
const p = particles[i];
const [fx, fy] = netForces[i];
p[0] += fx + p[2] * inertia;
p[1] += fy + p[3] * inertia;
p[2] = fx;
p[3] = fy;
// Wrap positions toroidally
p[0] = (p[0] + W) % W;
p[1] = (p[1] + H) % H;
}
animationId = requestAnimationFrame(animate);
}
window.onload = function() {
init();
};