From 6cbdb04786b381c948abd4f6b82d802f9de373b7 Mon Sep 17 00:00:00 2001 From: root <> Date: Fri, 25 Apr 2025 08:56:49 +0000 Subject: [PATCH] move charge repelling stuff to its own file --- index.html | 1 + sketch.js | 42 +----------------------------------------- thomson-problem.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 thomson-problem.js diff --git a/index.html b/index.html index cf15339..6129f5f 100644 --- a/index.html +++ b/index.html @@ -16,6 +16,7 @@ body {
+ diff --git a/sketch.js b/sketch.js index e89068d..cf036fc 100644 --- a/sketch.js +++ b/sketch.js @@ -19,52 +19,12 @@ function draw() { make_lights(); if (physics) { - move_charges(); + move_charges(charges); } draw_charges(sphere_radius); draw_sphere(sphere_radius, 25); } -function move_charges() { - for (charge of charges) { - charge.acceleration.setMag(0); - } - for (let i = 0; i < charges.length; i += 1) { - for (let j = 0; j < i; j += 1) { - const displacement = p5.Vector.sub( - charges[i].position, - charges[j].position, - ); - const acceleration_mag = 1 / displacement.mag() * 0.001; - let ai = displacement.copy().normalize().mult(acceleration_mag); - let aj = p5.Vector.mult(ai, -1); - project_onto_plane(ai, charges[i].position); - project_onto_plane(aj, charges[j].position); - charges[i].acceleration.add(ai); - charges[j].acceleration.add(aj); - } - } - for (let i = 0; i < charges.length; i += 1) { - let charge = charges[i]; - charge.velocity = charge.velocity.add(charge.acceleration); - //project_onto_plane(charge.velocity, charge.position); - charge.position = charge.position.add(charge.velocity); - charge.position.normalize(); - } -} - -function project_onto_unit_vector(v, unit_vector) { - let size = p5.Vector.dot(v, unit_vector); - return p5.Vector.mult(unit_vector, size); -} - -/// Project `v` onto the plane normal to `unit_vector` -/// Mutates `v` -function project_onto_plane(v, unit_vector) { - let v_proj = project_onto_unit_vector(v, unit_vector); - v.sub(v_proj) -} - function make_charges(n) { charges = []; for (let i = 0; i < n; i += 1) { diff --git a/thomson-problem.js b/thomson-problem.js new file mode 100644 index 0000000..0cbbdaa --- /dev/null +++ b/thomson-problem.js @@ -0,0 +1,44 @@ +function move_charges(charges) { + for (charge of charges) { + charge.acceleration.setMag(0); + } + for (let i = 0; i < charges.length; i += 1) { + for (let j = 0; j < i; j += 1) { + const displacement = p5.Vector.sub( + charges[i].position, + charges[j].position, + ); + let acceleration_mag = 1 / displacement.mag() * 0.001; + let ai; + if (acceleration_mag === Infinity) { + ai = createVector(random(-1, 1), random(-1, 1), random(-1, 1)); + } else { + ai = displacement.copy(); + } + ai = ai.normalize().mult(acceleration_mag); + let aj = p5.Vector.mult(ai, -1); + project_onto_plane(ai, charges[i].position); + project_onto_plane(aj, charges[j].position); + charges[i].acceleration.add(ai); + charges[j].acceleration.add(aj); + } + } + for (let i = 0; i < charges.length; i += 1) { + let charge = charges[i]; + charge.velocity = charge.velocity.add(charge.acceleration); + charge.position = charge.position.add(charge.velocity); + charge.position.normalize(); + } +} + +function project_onto_unit_vector(v, unit_vector) { + let size = p5.Vector.dot(v, unit_vector); + return p5.Vector.mult(unit_vector, size); +} + +/// Project `v` onto the plane normal to `unit_vector` +/// Mutates `v` +function project_onto_plane(v, unit_vector) { + let v_proj = project_onto_unit_vector(v, unit_vector); + v.sub(v_proj) +}