Skip to content

Instantly share code, notes, and snippets.

@fujidig
Created January 23, 2025 09:24
Show Gist options
  • Save fujidig/24117a7a2f4869e5f4a824f8c43ebcfc to your computer and use it in GitHub Desktop.
Save fujidig/24117a7a2f4869e5f4a824f8c43ebcfc to your computer and use it in GitHub Desktop.
// Run this in https://editor.p5js.org/
// https://qiita.com/aa_debdeb/items/e416ae8a018692fc07eb
function randomOnSphere() {
const cosTheta = -2.0 * Math.random() + 1.0;
const sinTheta = Math.sqrt(1.0 - cosTheta * cosTheta);
const phi = 2.0 * Math.PI * Math.random();
return [
sinTheta * Math.cos(phi),
sinTheta * Math.sin(phi),
cosTheta
];
}
function setup() {
createCanvas(700, 700, WEBGL);
noLoop();
}
function draw() {
camera(0, 0, 300, 0, 0, 0, 0, 1, 0);
lights();
background(255);
stroke("black");
strokeWeight(0.01);
let positions = [];
const N = 500;
for(let i = 0; i < N; i++) {
const pos = randomOnSphere();
positions.push(pos);
}
for(let i = 0; i < N; i++) {
for(let j = 0; j < i; j++) {
let p = positions[i], q = positions[j];
if (dist(p[0], p[1], p[2], q[0], q[1], q[2]) >= 2 - 1 / 8) {
line(100.0 * p[0], 100.0 * p[1], 100.0 * p[2], 100.0 * q[0], 100.0 * q[1], 100.0 * q[2]);
}
}
}
noStroke();
for(let i = 0; i < N; i++) {
push();
translate(100.0 * positions[i][0], 100.0 * positions[i][1], 100.0 * positions[i][2]);
sphere(2.0);
pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment