place charges randomly

This commit is contained in:
root 2025-04-24 08:05:57 +00:00
parent 5ac636a99c
commit 9d0618aa77

View file

@ -18,6 +18,42 @@ function draw() {
make_lights(); make_lights();
draw_sphere(sphere_radius, 25); 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) { function draw_sphere(radius, n_axis_circles) {
@ -86,5 +122,7 @@ function make_lights() {
function keyPressed() { function keyPressed() {
if (key == 'd') { if (key == 'd') {
sphere_mode = sphere_mode === 'earth' ? 'circles' : 'earth'; sphere_mode = sphere_mode === 'earth' ? 'circles' : 'earth';
} else if (key >= '0' && key <= '9') {
make_charges(int(key));
} }
} }