xxxxxxxxxx
76
let angleX = 0;
let angleY = 0;
let points = [];
let detailX = 24;
let detailY = 16;
let r = 200;
function setup() {
createCanvas(800, 800);
noFill();
stroke(255);
// Precomputar los puntos de la esfera
for (let i = 0; i <= detailX; i++) {
let theta = map(i, 0, detailX, 0, PI);
for (let j = 0; j <= detailY; j++) {
let phi = map(j, 0, detailY, 0, TWO_PI);
let x = r * sin(theta) * cos(phi);
let y = r * cos(theta);
let z = r * sin(theta) * sin(phi);
points.push(createVector(x, y, z));
}
}
}
function draw() {
background(0);
translate(width / 2, height / 2);
// Rotar los puntos en 3D
let rotatedPoints = points.map(p => rotatePoint(p, angleX, angleY));
// Dibujar los puntos y líneas
for (let i = 0; i < detailX; i++) {
for (let j = 0; j < detailY; j++) {
let idx1 = i * (detailY + 1) + j;
let idx2 = idx1 + detailY + 1;
let p1 = rotatedPoints[idx1];
let p2 = rotatedPoints[idx1 + 1];
let p3 = rotatedPoints[idx2];
let p4 = rotatedPoints[idx2 + 1];
beginShape();
vertex(p1.x, p1.y);
vertex(p2.x, p2.y);
vertex(p4.x, p4.y);
vertex(p3.x, p3.y);
endShape(CLOSE);
}
}
angleX = 0.015;
angleY = 0.015;
}
function rotatePoint(p, angleX, angleY) {
// Rotar en el eje X
let sinX = sin(angleX);
let cosX = cos(angleX);
let y = p.y * cosX - p.z * sinX;
let z = p.y * sinX + p.z * cosX;
p.y = y;
p.z = z;
// Rotar en el eje Y
let sinY = sin(angleY);
let cosY = cos(angleY);
let x = p.x * cosY + p.z * sinY;
z = -p.x * sinY + p.z * cosY;
p.x = x;
p.z = z;
return p;
}