From 620896fb49506ae8357c9f9048d73808eebed4bc Mon Sep 17 00:00:00 2001 From: root <> Date: Fri, 25 Apr 2025 14:47:03 +0000 Subject: [PATCH] begin drawing faces --- sketch.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/sketch.js b/sketch.js index 65842a6..479ebf8 100644 --- a/sketch.js +++ b/sketch.js @@ -5,6 +5,10 @@ let sphere_radius = 200; let physics = false; let earth = false; let skeleton = false; +let planes = false; + +let red; +let yellow; function preload() { earth_image = loadImage("atlas1.jpg"); @@ -12,6 +16,8 @@ function preload() { function setup() { createCanvas(600, 600, WEBGL); + red = color(0xbf, 0x00, 0x00); + yellow = color(0xff, 0xff, 0x00); } function draw() { @@ -25,6 +31,41 @@ function draw() { if (skeleton) { 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_sphere(sphere_radius, 25); } @@ -67,6 +108,7 @@ function make_charges(n) { position: position, velocity: createVector(), acceleration: createVector(), + color: red, }); } } @@ -74,8 +116,8 @@ function make_charges(n) { function draw_charges(radius) { push(); noStroke(); - ambientMaterial(0xbf, 0x00, 0x00); for (let charge of charges.values()) { + ambientMaterial(charge.color); let position = charge.position.copy(); position.mult(radius); push(); @@ -156,7 +198,13 @@ function keyPressed() { earth = !earth; } else if (key == 'f') { skeleton = !skeleton; + } else if (key == 'g') { + planes = !planes; } else if (key >= '0' && key <= '9') { 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