custom charge magnitude

also: s/charge/particle
to reduce confusion
This commit is contained in:
root 2025-04-27 08:03:07 +00:00
parent 5e04ad107f
commit 642d1c5cdd
3 changed files with 54 additions and 48 deletions

View file

@ -1,7 +1,7 @@
let camera;
let red;
let charges = [];
let particles = [];
let faces = [];
let sphere_radius;
@ -14,6 +14,7 @@ let surface = SURFACE_CIRCLES;
let physics = false;
let skeleton = false;
let polytope = false;
let charge = -1;
let buttons_surface;
let checkbox_physics;
@ -70,9 +71,9 @@ function draw() {
camera.centerZ = 0;
make_lights();
if (physics) move_charges(charges, 8e-4);
if (physics) move_particles(particles, 8e-4);
draw_charges(sphere_radius);
draw_particles(sphere_radius);
if (skeleton) draw_skeleton(sphere_radius);
if (polytope) {
if (physics || faces.length === 0) find_faces();
@ -88,27 +89,27 @@ function face_dist_sq([v1, v2, v3]) {
function find_faces() {
faces = [];
for (let i = 2; i < charges.length; i += 1) {
for (let i = 2; i < particles.length; i += 1) {
for (let j = 1; j < i; j += 1) {
for (let k = 0; k < j; k += 1) {
// Check if p1 p2 p3 form a face of the convex polytope
// enclosing all vertices ...
const p1 = charges[i].position;
const p2 = charges[j].position;
const p3 = charges[k].position;
const p1 = particles[i].position;
const p2 = particles[j].position;
const p3 = particles[k].position;
const normal = p5.Vector.sub(p2, p1).cross(p5.Vector.sub(p3, p1));
// ... by checking if the other vertices are on the same
// side of the plane generated by p1 p2 p3
let plane_separates_vertices = false;
let euler_formula = false;
for (let r = 1; r < charges.length; r += 1) {
for (let r = 1; r < particles.length; r += 1) {
for (let s = 0; s < r; s += 1) {
if (
r === i || r === j || r === k ||
s === i || s === j || s === k
) continue;
const q1 = charges[r].position;
const q2 = charges[s].position;
const q1 = particles[r].position;
const q2 = particles[s].position;
// Let l(t) := q1 + (q2 - q1) * t.
// L := { l(t) : 0 <= t <= 1 } is the line segment
// between q1 and q2. L intersects the plane
@ -133,7 +134,7 @@ function find_faces() {
);
plane_separates_vertices ||= t >= 0 && t <= 1;
if (plane_separates_vertices) break;
euler_formula ||= charges.length * 2 - faces.length == 4;
euler_formula ||= particles.length * 2 - faces.length == 4;
if (euler_formula) break;
}
if (plane_separates_vertices || euler_formula) break;
@ -170,21 +171,21 @@ function draw_skeleton(radius) {
fill(0xff);
sphere(4);
stroke(0xbf);
for (let charge of charges) {
for (let particle of particles) {
line(
0,
0,
0,
charge.position.x * radius,
charge.position.y * radius,
charge.position.z * radius,
particle.position.x * radius,
particle.position.y * radius,
particle.position.z * radius,
);
}
pop();
}
function make_charges(n) {
charges = [];
function make_particles(n) {
particles = [];
for (let i = 0; i < n; i += 1) {
let position;
if (i === 0) {
@ -192,21 +193,22 @@ function make_charges(n) {
} else {
position = p5.Vector.random3D();
}
charges.push({
particles.push({
position: position,
velocity: createVector(),
acceleration: createVector(),
charge: charge,
color: red,
});
}
}
function draw_charges(radius) {
function draw_particles(radius) {
push();
noStroke();
for (let charge of charges.values()) {
ambientMaterial(charge.color);
let position = charge.position.copy();
for (let particle of particles) {
ambientMaterial(particle.color);
let position = particle.position.copy();
position.mult(radius);
push();
translate(position.x, position.y, position.z);
@ -292,7 +294,7 @@ function keyPressed() {
} else if (key == 'g') {
toggle_polytope();
} else if (key >= '0' && key <= '9') {
make_charges(int(key));
make_particles(int(key));
faces = [];
}
}