Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// title : Paste Paper Comb Generator
// author : Natalie Freed
// license : MIT License
function getParameterDefinitions() {
return [{ name: 'w', type: 'float', initial: 10, caption: "Width :" },
{ name: 'h', type: 'float', initial: 10, caption: "Height :" },
{ name: 'rows', type: 'int', initial: 5, caption: "Rows :" },
{ name: 'cols', type: 'int', initial: 5, caption: "Cols :" },
{ name: 'thickness', type: 'float', initial: 3, caption: "Extrusion Thickness (3D mode):"},
{
name: 'shapeSpacingChoice',
type: 'choice',
values: ["auto", "custom"],
captions: ["auto", "custom"],
caption: 'Shape spacing:',
initial: "auto"
},
{ name: 'spacing', type: 'float', initial: 1, caption: "Shape spacing (custom)"},
{
name: 'type',
type: 'choice',
values: ["2D", "3D"],
captions: ["2D (DXF output)", "3D (STL output)"],
caption: 'Export type:',
initial: "2D"
}];
}
var bounds = [];
function main(params) {
var shapes = [];
var shapeWidths = [];
var shapeHeights = [];
for(s=0;s<params.rows*params.cols;s++) {
var shape = makeTool(params);
shapes.push(shape);
var bounds = shape.getBounds();
var shapeW = bounds[1].x - bounds[0].x;
shapeWidths.push(shapeW);
var shapeH = bounds[1].y - bounds[0].y;
shapeHeights.push(shapeH);
}
if(params.shapeSpacingChoice == "auto") {
var colSpacing = Math.max.apply(null, shapeWidths)+2;
var rowSpacing = Math.max.apply(null, shapeHeights)+2;
}
else {
var colSpacing = params.w+params.spacing;
var rowSpacing = params.h+params.spacing;
}
for(row=0;row<params.rows;row++) {
for(col=0;col<params.cols;col++) {
shapes[row*params.cols+col] = shapes[row*params.cols+col].translate([col*colSpacing,row*rowSpacing, 0]);
}
}
//if we're in 2D mode, return the 2D shape
if(params.type === "2D") {
return shapes;
}
//if we're in 2D mode, extrude the shape to make it 3D
else {
return linear_extrude(params.thickness, union(shapes));
}
}
function makeTool(params) {
var points = [];
//top zigzag
var numPointsTop = Math.round(Math.random()*30+5);
var spacingTop = params.w/numPointsTop/2;
for(i=0;i<numPointsTop*2;i+=2) {
points.push([i*spacingTop,0]);
points.push([(i+1)*spacingTop,spacingTop*2]);
}
points.push([numPointsTop*2*spacingTop, 0]); //last "down" zag
//right square notches (all same size)
// var numNotchesR = Math.round(Math.random()*20+5);
// var notchPercentR = Math.random()*0.8+0.2;
// var totalNotchWidthR = params.h * notchPercentR;
// var notchWidthR = totalNotchWidthR / numNotchesR;
// var gapWidthR = (params.h - totalNotchWidthR) / (numNotchesR-1);
// var notchHeightR = notchWidthR*2;
// var y = 0;
// for(i=0;i<numNotchesR-1;i++) {
// points.push([params.w+notchHeightR,y]);
// points.push([params.w+notchHeightR,y-notchWidthR]);
// points.push([params.w,y-notchWidthR]);
// points.push([params.w,y-notchWidthR-gapWidthR]);
// points.push([params.w+notchHeightR,y-notchWidthR-gapWidthR]);
// y-=notchWidthR+gapWidthR;
// }
// points.push([params.w+notchHeightR,y]);
// points.push([params.w+notchHeightR,y-notchWidthR]);
// points.push([params.w,y-notchWidthR]);
//right gradient notches (go gradually up in size)
var startingNotchWidthR = Math.random()*params.h/30+0.1;
var notchWidthR = startingNotchWidthR;
var notchHeightR = startingNotchWidthR*2;
var gapWidthR = notchWidthR;
var notchTypeGradient = Math.random() > 0.5 ? 1 : 0;
var multGradient = Math.random() > 0.5 ? 1 : 0;
if(multGradient) {
var gradientChange = Math.random()/2+1.01;
}
else {
var gradientChange = Math.random()*notchWidthR+0.1;
}
var y = 0;
do {
points.push([params.w+notchHeightR,y]); //right
points.push([params.w+notchHeightR,y-notchWidthR]); //down
points.push([params.w,y-notchWidthR]); //left
points.push([params.w,y-notchWidthR-gapWidthR]); //down (in gap)
//points.push([params.w+notchHeightR,y-notchWidthR-gapWidthR]); //right
y-=notchWidthR+gapWidthR;
if(notchTypeGradient) { //notches do the gradient
if(multGradient) {
notchWidthR *= gradientChange;
}
else {
notchWidthR += gradientChange;
}
}
else { //gaps do the gradient
if(multGradient) {
gapWidthR *= gradientChange;
}
else {
gapWidthR += gradientChange;
}
}
}
while(y-notchWidthR-gapWidthR > -params.h);
if(!notchTypeGradient) {
points.push([params.w+notchHeightR,y]);
points.push([params.w+notchHeightR,y-notchWidthR]);
points.push([params.w,y-notchWidthR]);
}
//bottom zigzag (alternating pattern of 2 sizes)
var numSectionsBt = Math.round(Math.random()*params.w/2+2);
//console.log(numSectionsBt);
var sectionWidthBt = params.w / numSectionsBt;
//console.log(sectionWidthBt);
var sectionsBt = [];
sectionsBt.push(Math.random()*(sectionWidthBt/1.5)+sectionWidthBt/4);
sectionsBt.push(sectionWidthBt-sectionsBt[0]);
// console.log(sectionsBt[0], sectionsBt[1]);
var x = params.w;
var heightBt = Math.min.apply(null,sectionsBt)*1.2;
points.push([x, -params.h]);
for(i=numSectionsBt-1;i>=0;i--) {
for(j=0;j<sectionsBt.length;j++) {
points.push([x-sectionsBt[j]/2,-params.h-heightBt]);
points.push([x-sectionsBt[j],-params.h]);
x -=sectionsBt[j];
}
}
//left square notches
var numNotchesL = Math.round(Math.random()*20+5);
var notchPercentL = Math.random()*0.8+0.2;
var totalNotchWidthL = params.h * notchPercentL;
var notchWidthL = totalNotchWidthL / numNotchesL;
var gapWidthL = (params.h - totalNotchWidthL) / (numNotchesL-1);
var notchHeightL = Math.min(notchWidthL*1.2, gapWidthL*1.2);
y = -params.h;
for(i=0;i<numNotchesL-1;i++) {
points.push([-notchHeightL,y]);
points.push([-notchHeightL,y+notchWidthL]);
points.push([0,y+notchWidthL]);
points.push([0,y+notchWidthL+gapWidthL]);
points.push([-notchHeightL,y+notchWidthL+gapWidthL]);
y+=notchWidthL+gapWidthL;
}
points.push([-notchHeightL,y]);
points.push([-notchHeightL,y+notchWidthL]);
points.push([0,y+notchWidthL]);
var shape = (new CSG.Path2D(points, true)).innerToCAG();
return shape;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.