From 9d0618aa77bbba2643b9da8c0572bb76e2682ca8 Mon Sep 17 00:00:00 2001 From: root <> Date: Thu, 24 Apr 2025 08:05:57 +0000 Subject: [PATCH] place charges randomly --- sketch.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/sketch.js b/sketch.js index a88cb82..0a6646c 100644 --- a/sketch.js +++ b/sketch.js @@ -18,6 +18,42 @@ function draw() { make_lights(); draw_sphere(sphere_radius, 25); + draw_charges(sphere_radius); +} + +function make_charges(n) { + charges.clear(); + for (let i = 0; i < n; i += 1) { + let position; + if (i == 0) { + position = createVector(0, -1, 0); + } else { + const lat = random(-TAU / 4, TAU / 4); + const lon = random(0, TAU); + position = createVector( + cos(lat) * cos(lon), + sin(lat), + cos(lat) * sin(lon), + ); + } + charges.set(counter, position); + counter += 1; + } +} + +function draw_charges(radius) { + push(); + noStroke(); + ambientMaterial(0xbf, 0x00, 0x00); + for (let charge of charges.values()) { + let position = charge.copy(); + position.mult(radius); + push(); + translate(position.x, position.y, position.z); + sphere(15); + pop(); + } + pop(); } function draw_sphere(radius, n_axis_circles) { @@ -86,5 +122,7 @@ function make_lights() { function keyPressed() { if (key == 'd') { sphere_mode = sphere_mode === 'earth' ? 'circles' : 'earth'; + } else if (key >= '0' && key <= '9') { + make_charges(int(key)); } }