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
This commit is contained in:
parent
620896fb49
commit
838eae2cbf
1 changed files with 39 additions and 12 deletions
51
sketch.js
51
sketch.js
|
@ -33,33 +33,60 @@ function draw() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (planes) {
|
if (planes) {
|
||||||
let p = charges[0];
|
let p_charge = charges[0];
|
||||||
let a = charges[1];
|
let a_charge = charges[1];
|
||||||
let b = charges[2];
|
let b_charge = charges[2];
|
||||||
p.color = yellow;
|
p_charge.color = yellow;
|
||||||
a.color = yellow;
|
a_charge.color = yellow;
|
||||||
b.color = yellow;
|
b_charge.color = yellow;
|
||||||
let n = p5.Vector.sub(p.position, a.position).cross(p5.Vector.sub(p.position, b.position));
|
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
|
n.normalize(); // unnecessary
|
||||||
push();
|
push();
|
||||||
strokeWeight(5);
|
strokeWeight(5);
|
||||||
stroke(0x7f);
|
stroke(0x7f);
|
||||||
line(
|
line(
|
||||||
0,0,0,
|
0, 0, 0,
|
||||||
n.x*sphere_radius,n.y*sphere_radius,n.z*sphere_radius,
|
n.x * sphere_radius, n.y * sphere_radius, n.z * sphere_radius,
|
||||||
)
|
)
|
||||||
fill(0xff);
|
fill(0xff);
|
||||||
strokeWeight(3);
|
strokeWeight(3);
|
||||||
stroke(0x00);
|
stroke(0x00);
|
||||||
beginShape(TRIANGLES);
|
beginShape(TRIANGLES);
|
||||||
const v1 = p5.Vector.mult(p.position, sphere_radius);
|
const v1 = p5.Vector.mult(p, sphere_radius);
|
||||||
const v2 = p5.Vector.mult(a.position, sphere_radius);
|
const v2 = p5.Vector.mult(a, sphere_radius);
|
||||||
const v3 = p5.Vector.mult(b.position, sphere_radius);
|
const v3 = p5.Vector.mult(b, sphere_radius);
|
||||||
vertex(v1.x, v1.y, v1.z);
|
vertex(v1.x, v1.y, v1.z);
|
||||||
vertex(v2.x, v2.y, v2.z);
|
vertex(v2.x, v2.y, v2.z);
|
||||||
vertex(v3.x, v3.y, v3.z);
|
vertex(v3.x, v3.y, v3.z);
|
||||||
endShape();
|
endShape();
|
||||||
pop();
|
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 {
|
} else {
|
||||||
for (let charge of charges) {
|
for (let charge of charges) {
|
||||||
charge.color = red;
|
charge.color = red;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue