begin drawing faces

This commit is contained in:
root 2025-04-25 14:47:03 +00:00
parent c20044c459
commit 620896fb49

View file

@ -5,6 +5,10 @@ let sphere_radius = 200;
let physics = false; let physics = false;
let earth = false; let earth = false;
let skeleton = false; let skeleton = false;
let planes = false;
let red;
let yellow;
function preload() { function preload() {
earth_image = loadImage("atlas1.jpg"); earth_image = loadImage("atlas1.jpg");
@ -12,6 +16,8 @@ function preload() {
function setup() { function setup() {
createCanvas(600, 600, WEBGL); createCanvas(600, 600, WEBGL);
red = color(0xbf, 0x00, 0x00);
yellow = color(0xff, 0xff, 0x00);
} }
function draw() { function draw() {
@ -25,6 +31,41 @@ function draw() {
if (skeleton) { if (skeleton) {
draw_skeleton(sphere_radius); draw_skeleton(sphere_radius);
} }
if (planes) {
let p = charges[0];
let a = charges[1];
let b = charges[2];
p.color = yellow;
a.color = yellow;
b.color = yellow;
let n = p5.Vector.sub(p.position, a.position).cross(p5.Vector.sub(p.position, b.position));
n.normalize(); // unnecessary
push();
strokeWeight(5);
stroke(0x7f);
line(
0,0,0,
n.x*sphere_radius,n.y*sphere_radius,n.z*sphere_radius,
)
fill(0xff);
strokeWeight(3);
stroke(0x00);
beginShape(TRIANGLES);
const v1 = p5.Vector.mult(p.position, sphere_radius);
const v2 = p5.Vector.mult(a.position, sphere_radius);
const v3 = p5.Vector.mult(b.position, sphere_radius);
vertex(v1.x, v1.y, v1.z);
vertex(v2.x, v2.y, v2.z);
vertex(v3.x, v3.y, v3.z);
endShape();
pop();
} else {
for (let charge of charges) {
charge.color = red;
}
}
draw_charges(sphere_radius); draw_charges(sphere_radius);
draw_sphere(sphere_radius, 25); draw_sphere(sphere_radius, 25);
} }
@ -67,6 +108,7 @@ function make_charges(n) {
position: position, position: position,
velocity: createVector(), velocity: createVector(),
acceleration: createVector(), acceleration: createVector(),
color: red,
}); });
} }
} }
@ -74,8 +116,8 @@ function make_charges(n) {
function draw_charges(radius) { function draw_charges(radius) {
push(); push();
noStroke(); noStroke();
ambientMaterial(0xbf, 0x00, 0x00);
for (let charge of charges.values()) { for (let charge of charges.values()) {
ambientMaterial(charge.color);
let position = charge.position.copy(); let position = charge.position.copy();
position.mult(radius); position.mult(radius);
push(); push();
@ -156,7 +198,13 @@ function keyPressed() {
earth = !earth; earth = !earth;
} else if (key == 'f') { } else if (key == 'f') {
skeleton = !skeleton; skeleton = !skeleton;
} else if (key == 'g') {
planes = !planes;
} else if (key >= '0' && key <= '9') { } else if (key >= '0' && key <= '9') {
make_charges(int(key)); make_charges(int(key));
} }
} }
// TODO draw faces
// algorithm: choose 3 vertices until 2-partition of other vertices has one empty set
// done when V - E + F = 2. V is known. count E and F while creating faces