From 838eae2cbf198548d4e6c37865badac8dd25c440 Mon Sep 17 00:00:00 2001 From: root <> Date: Fri, 25 Apr 2025 14:59:51 +0000 Subject: [PATCH] polyhedron alg progress: face separates vertices the algorithm so far: - select 3 vertices - draw a triangle between them - for all vertex pairs (non-triangle vertices): -- draw a line between them -- if line intersects with triangle plane, red -- otherwise, green if the line is red the two vertices are on other sides of the triangle plane, and this triangle can't be a face, because we want the polyhedron to be convex to demo: press 9, press g, press space; press 9 over and over --- sketch.js | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/sketch.js b/sketch.js index 479ebf8..f97897e 100644 --- a/sketch.js +++ b/sketch.js @@ -33,33 +33,60 @@ function draw() { } 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)); + let p_charge = charges[0]; + let a_charge = charges[1]; + let b_charge = charges[2]; + p_charge.color = yellow; + a_charge.color = yellow; + b_charge.color = yellow; + let p = p_charge.position; + let a = a_charge.position; + let b = b_charge.position; + let n = p5.Vector.sub(p, a).cross(p5.Vector.sub(p, b)); n.normalize(); // unnecessary push(); strokeWeight(5); stroke(0x7f); line( - 0,0,0, - n.x*sphere_radius,n.y*sphere_radius,n.z*sphere_radius, + 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); + const v1 = p5.Vector.mult(p, sphere_radius); + const v2 = p5.Vector.mult(a, sphere_radius); + const v3 = p5.Vector.mult(b, 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(); + for (let i = 4; i < charges.length; i += 1) { + for (let j = 3; j < i; j += 1) { + push(); + const u = charges[i].position; + const v = charges[j].position; + const t = p5.Vector.dot(p5.Vector.sub(p, u), n) / p5.Vector.dot(p5.Vector.sub(v, u), n); + const intersects_plane = t >= 0 && t <= 1; + if (intersects_plane) { + stroke(0xff, 0x1f, 0x00); + } else { + stroke(0x00, 0xff, 0x00); + } + strokeWeight(3); + line( + charges[i].position.x * sphere_radius, + charges[i].position.y * sphere_radius, + charges[i].position.z * sphere_radius, + charges[j].position.x * sphere_radius, + charges[j].position.y * sphere_radius, + charges[j].position.z * sphere_radius, + ); + pop(); + } + } } else { for (let charge of charges) { charge.color = red;