Tetrahedron

Examples shows how to create a parametric Tetrahedron shape.

function build() {



  return tetrahedron(10);
    

};

function tetrahedron(sideLength){

    //For convenience
    const Vertex = cadlib.Vertex;
    const Edge = cadlib.Edge;
    const Face = cadlib.Face;
    const Wire = cadlib.Wire;
    var halfLength = sideLength/2;
    var height = sideLength*Math.sqrt(6)/3;
 
    //create vertices
    let vright = new Vertex(halfLength, 0, halfLength/Math.sqrt(3));
    let vback = new Vertex(0, 0, -halfLength*2/Math.sqrt(3));
    let vleft = new Vertex(-halfLength, 0, halfLength/Math.sqrt(3));
    let vtop = new Vertex(0, height, 0);
 
    //create edges
    let eBaseRight = new Edge().createLine(vright, vback);
    let eBaseLeft = new Edge().createLine(vback, vleft);
    let eBaseFront = new Edge().createLine(vleft, vright);
    let eBackTop = new Edge().createLine(vback, vtop);
    let eRightTop = new Edge().createLine(vright, vtop);
    let eLeftTop = new Edge().createLine(vleft, vtop);
 
    //create faces by connecting edges (and creating wires)
    let fBase = new Face([new Wire([eBaseRight, eBaseLeft, eBaseFront]) ] );
    let fRight = new Face([new Wire([eBaseRight, eRightTop, eBackTop]) ] );
    let fLeft = new Face([new Wire([eBaseLeft, eLeftTop, eBackTop]) ] );
    let fFront = new Face([new Wire([eBaseFront, eRightTop, eLeftTop]) ] );
    
     //create shell from faces
     let shell = cadlib.makeShell([fBase, fRight, fLeft, fFront]);
    
    //finally, build a solid from shell
    let solid = cadlib.makeSolidFromShells([shell]);

    return cadlib.fixShape(solid);
    
};
 

 //Code for tetrahedron using small circle as top point:
function buildUsingLoft(sideLength) {

    
    var sideLength = params.sideLength;
    var triWire = eqTriangle(sideLength,0);
    var height = Math.sqrt(2/3)*sideLength;
 
    var loftPoint = cadlib.circle({x:0, y:height, z:0}, {x:0, y:1, z:0}, 0.0001);
    var s = cadlib.loft([cadlib.wire(triWire), cadlib.wire(loftPoint)]);
    return s;
};
 
function eqTriangle(sideLength, yOffset){
    //create equilateral triangle centered on origin
    var path = new cadlib.Path(true);
    var halfLength = sideLength/2;
    path.start(halfLength, yOffset, halfLength / Math.sqrt(3));
    path.lineTo(-halfLength, 0, -halfLength*Math.sqrt(3));
    path.lineTo(-halfLength, 0, halfLength*Math.sqrt(3));
    path.close();
    return path.wire();
    
};