xxxxxxxxxx
294
let helvetica;
let cube;
let SCALE = 200;
function preload(){
helvetica = loadFont('Helvetica Roman.ttf');
}
function setup() {
createCanvas(400, 400, WEBGL);
ortho(-width / 2, width / 2, -height / 2, height / 2, 0, 800);
smooth();
textFont(helvetica);
textSize(15)
cube = new Cube();
}
function draw() {
rotateX(PI/8);
rotateY(PI/6);
background(220);
cube.drawCube();
cube.getTriangles();
noLoop();
}
///////////////////////////////////////////////////////////////////
class Cube{
constructor(){
this.vertices = [];
this.vertices.push(new Vertex(createVector(0,-0.5,-0.5),true,[12,13],"",0)) // 0
this.vertices.push(new Vertex(createVector(0.5,-0.5,0),true,[13,14],"",1)) //1
this.vertices.push(new Vertex(createVector(0,-0.5,0.5),true,[14,15],"",2)) // 2
this.vertices.push(new Vertex(createVector(-0.5,-0.5,0),true,[15,12],"",3)) // 3
this.vertices.push(new Vertex(createVector(0,0.5,-0.5),true,[16,17],"",4)) // 4
this.vertices.push(new Vertex(createVector(0.5,0.5,0),true,[17,18],"",5)) // 5
this.vertices.push(new Vertex(createVector(0,0.5,0.5),true,[18,19],"",6)) // 6
this.vertices.push(new Vertex(createVector(-0.5,0.5,0),true,[19,16],"",7)) // 7
this.vertices.push(new Vertex(createVector(-0.5,0,-0.5),true,[12,16],"",8)) // 8
this.vertices.push(new Vertex(createVector(0.5,0,-0.5),true,[13,17],"",9)) // 9
this.vertices.push(new Vertex(createVector(0.5,0,0.5),true,[14,18],"",10)) // 10
this.vertices.push(new Vertex(createVector(-0.5,0,0.5),true,[15,19],"",11)) // 11
this.vertices.push(new Vertex(createVector(-0.5,-0.5,-0.5),false,[0,3,8],"1",12)) // 12
this.vertices.push(new Vertex(createVector(0.5,-0.5,-0.5),false,[1,0,9],"2",13)) // 13
this.vertices.push(new Vertex(createVector(0.5,-0.5,0.5),false,[10,1,2],"4",14)) // 14
this.vertices.push(new Vertex(createVector(-0.5,-0.5,0.5),false,[3,2,11],"8",15)) // 15
this.vertices.push(new Vertex(createVector(-0.5,0.5,-0.5),false,[7,4,8],"16",16)) // 16
this.vertices.push(new Vertex(createVector(0.5,0.5,-0.5),false,[4,5,9],"32",17)) // 17
this.vertices.push(new Vertex(createVector(0.5,0.5,0.5),false,[6,5,10],"64",18)) // 18
this.vertices.push(new Vertex(createVector(-0.5,0.5,0.5),false,[7,6,11],"128",19)) // 19
}
drawVertices(){
for(let i = 0; i < this.vertices.length;i++){
push();
translate(this.vertices[i].pos.copy().mult(SCALE));
sphere(4);
pop();
}
}
drawLines(){
function drawLine(n1, n2){
beginShape();
vertex(n1.x,n1.y,n1.z);
vertex(n2.x,n2.y,n2.z);
endShape();
}
for(let i = 0; i < this.vertices.length;i++){
for(let j = 0; j < this.vertices[i].links.length ; j++){
push();
let v1 = this.vertices[i].pos.copy().mult(SCALE);
let v2 = this.vertices[this.vertices[i].links[j]].pos.copy().mult(SCALE);
drawLine(v1,v2);
pop();
}
}
}
drawNumbers(){
for(let i = 0; i < this.vertices.length;i++){
push();
translate(this.vertices[i].pos.copy().mult(SCALE));
rotateY(-PI/6);
rotateX(-PI/8);
fill(0);
text(i, -8, -8);
pop();
}
}
drawLabels(){
for(let i = 0; i < this.vertices.length;i++){
push();
translate(this.vertices[i].pos.copy().mult(SCALE));
rotateY(-PI/6);
rotateX(-PI/8);
fill(255,0,0);
text(this.vertices[i].label, 8, 8);
pop();
}
}
drawCube(){
this.drawVertices();
this.drawLines();
this.drawNumbers();
this.drawLabels();
}
// SPECIFIC GETS FOR GENERATION OF TRIANGLE BIZ
getTriangles(){
// get combinations of three
let combinations = new Combinations();
for(let i = 12; i < this.vertices.length; i++){
let v = this.vertices[i];
for(let j = 0; j < 3; j++){
let mid = this.vertices[v.links[j]] // get the midpoint
let vnext; // get vnext
let inext;
if(mid.links[0] == i){ // don´t go back to main point
inext = mid.links[1];
vnext = this.vertices[mid.links[1]]
}
else{
inext = mid.links[0];
vnext = this.vertices[mid.links[0]]
}
for(let k = 0; k < 3; k++){
let mid2 = this.vertices[vnext.links[k]] // get the midpoint
let vnext2; // get vnext
if(mid2.links[0] == inext){ // don´t go back to main point
vnext2 = this.vertices[mid2.links[1]]
}
else{
vnext2 = this.vertices[mid2.links[0]]
}
if( v != vnext2){
combinations.add(new Combination([v,vnext,vnext2],this));
}
}
}
}
combinations.display(); // calculate the triangles for each combination of three
}
}
class Vertex{
constructor(position,isMid=false,links=[],label,idx){
this.pos = position;
this.isMid = isMid;
this.links =links;
this.label = label;
this.idx = idx;
}
}
class Combinations{
constructor(){
this.list =[];
}
add(combination){
this.list.push(combination);
}
filterDupes(){
const itemUIds = this.list.map(o => o.caseNo)
const filteredOption1 = this.list.filter(({caseNo}, index) => !itemUIds.includes(caseNo, index + 1));
this.list = filteredOption1;
}
calculate(){
for(let i = 0; i < this.list.length; i++){
this.list[i].threeConsecutivePoints();
}
}
display(){
this.filterDupes();
this.calculate();
for(let i = 0; i < this.list.length; i++){
let t = this.list[i].caseNo;
console.log("case : " + t);
let t2 = "";
for(let j = 0; j < this.list[i].points.length; j++){
t2 += "p" + j + " : " + this.list[i].points[j].label + " | "
}
// console.log(t2)
console.log(JSON.stringify(this.list[i].triangles));
}
console.log("total : " + this.list.length);
}
}
class Combination{
constructor(points, cubeReference){
this.points = points;
this.caseNo = 0;
for(let i = 0; i < this.points.length; i++){
this.caseNo += int(this.points[i].label);
}
this.triangles = [];
this.cube = cubeReference;
}
getMidPointUnder(a,b,c){ // gets the midpoint under a not on the face abc
for(let i = 0; i < 3;i++){
let midx = a.links[i]; // get vertice index
let m = this.cube.vertices[a.links[i]]; // get actual vertice
// first is point NOT between a and b
// second is point NOT on plane of a,b and c
if(!b.links.includes(midx) && !c.links.includes(midx)){
if(!isPointOnPlane(a.pos,b.pos,c.pos,m.pos)){
return m;
}
}
}
}
getMidPointOver(a,b,c){ // gets the midpoint over a not on the face abc
for(let i = 0; i < 3;i++){
let midx = a.links[i]; // get vertice index
let m = this.cube.vertices[a.links[i]]; // get actual vertice
// first is point NOT between a and b and c
// second is point on plane of a,b and c
if(!b.links.includes(midx) && !c.links.includes(midx)){
if(isPointOnPlane(a.pos,b.pos,c.pos,m.pos)){
return m;
}
}
}
}
threeConsecutivePoints(){ // build triangles for three consecutive points
let x = this.points[0];
let y = this.points[1];
let z = this.points[2];
let xx = this.points[0].idx;
let yy = this.points[1].idx;
let zz = this.points[2].idx;
let xa = this.getMidPointOver(x,y,z).idx;
let xb = this.getMidPointUnder(x,y,z).idx;
let yb = this.getMidPointUnder(y,z,x).idx;
let za = this.getMidPointOver(z,x,y).idx;
let zb = this.getMidPointUnder(z,x,y).idx;
this.triangles = [
xx, xb, xa,
zz, zb, za,
yy, xb, xx,
yy, xb, yb,
yy, zb, zz,
yy, zb, yb,
xx, yy, xa,
yy, xa, za,
zz, yy, za
]
}
}
// MATH HELPERS
function isPointOnPlane(A, B, C, P) {
let AB = p5.Vector.sub(B, A);
let AC = p5.Vector.sub(C, A);
let AP = p5.Vector.sub(P, A);
// Compute the normal vector
let normal = AB.cross(AC);
// Check dot product
let dot = normal.dot(AP);
// Check if dot product is close to 0
let epsilon = 0.0001; // Adjust as necessary for precision
return Math.abs(dot) < epsilon;
}