place charges randomly
This commit is contained in:
parent
5ac636a99c
commit
9d0618aa77
1 changed files with 38 additions and 0 deletions
38
sketch.js
38
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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue