Skip to content

Instantly share code, notes, and snippets.

@udif
Last active November 25, 2018 15:02
Show Gist options
  • Save udif/9b7fa9662c9c68644a243fc364c412ad to your computer and use it in GitHub Desktop.
Save udif/9b7fa9662c9c68644a243fc364c412ad to your computer and use it in GitHub Desktop.
// title : H
// author : Udi Finkelstein
// license : MIT License
// revision : 1.0
// description:
// file : h.jscad
var cylresolution = 16;
// Here we define the user editable parameters:
function getParameterDefinitions () {
return [
{name: 'general', caption: 'General', type: 'group'},
{name: 'quality', type: 'choice', caption: 'Quality', values: [8, 16, 32, 64], captions: ['Draft', 'Med', 'High', 'Very High'], initial: 8},
{name: 'general', caption: 'Base', type: 'group'},
{name: 'bheight', caption: 'bheight:', type: 'float', initial: 12},
{name: 'bdiameter', caption: 'bdiameter:', type: 'float', initial: 50, min: 0.0, max: 1.0, step: 0.1},
{name: 'general', caption: 'Body', type: 'group'},
{name: 'hheight', caption: 'hheight:', type: 'float', initial: 100},
{name: 'hwidth', caption: 'hwidth:', type: 'float', initial: 50},
{name: 'general', caption: 'Candle holder', type: 'group'},
{name: 'cheight', caption: 'height:', type: 'float', initial: 10},
{name: 'cbase', caption: 'elevation above body:', type: 'float', initial: 7},
{name: 'chdiameter', caption: 'candle holder diameter:', type: 'float', initial: 20, min: 0.0, max: 1.0, step: 0.1},
{name: 'cdepth', caption: 'candle hole depth:', type: 'float', initial: 7, min: 0.0, max: 1.0, step: 0.1},
{name: 'cdiameter', caption: 'diameter:', type: 'float', initial: 10, min: 0.0, max: 1.0, step: 0.1}
];
}
var PI = 3.14159;
function gen_x_s(params, i, angle) {
if (i === 0) {
return 0;
} else {
return params.hheight*(i/5)*Math.sin(angle/(180/PI));
}
}
function gen_y_s(params, i, angle) {
if (i === 0) {
return 0;
} else {
return 0;
}
}
function gen_z_s(params, i, angle) {
if (i === 0) {
return params.bheight+params.hheight/90*angle;
} else {
return params.bheight+params.hheight*(1-Math.abs(i)/5*(Math.cos(angle/(180/PI))));
}
}
var cylresolution;
function gen_blob(params) {
return sphere({r:10, center:true, fn:cylresolution});
}
function gen_candle(params) {
return CSG.cylinder({start: [0, 0, 0], end: [0, 0, params.cheight], radius: params.chdiameter / 2, resolution: cylresolution})
.subtract(CSG.cylinder({start: [0, 0, params.cheight-params.cdepth], end: [0, 0, params.cheight], radius: params.cdiameter / 2, resolution: cylresolution}));
}
function main (params) {
cylresolution = params.quality;
var bheight = params.bheight;
var hheight = params.hheight;
var bwidth = params.bwidth;
var hwidth = params.hwidth;
var bdiameter = params.bdiameter;
var result = new CSG();
var gen_x = gen_x_s;
var gen_y = gen_y_s;
var gen_z = gen_z_s;
var hbase = CSG.cylinder({start: [0, 0, 0], end: [0, 0, bheight], radius: bdiameter / 2, resolution: cylresolution});
//return hbase;
for (i = -4; i <= 4; i++) {
stop = (i === 0) ? 100 : 90;
end = (i === 0) ? 95 : 90;
step = (i === 0) ? stop/7.01 : (90/2/Math.abs(i));
for (j = 0; j <= stop; j+= step) {
if (i != 0 && j === 0) {
continue;
}
hbase = hbase.union(gen_blob(params).translate([gen_x(params, i, j), gen_y(params, i, j), gen_z(params, i, j)]));
}
hbase = hbase.union(gen_candle(params).translate([gen_x(params, i, 90), gen_y(params, i, 90), gen_z(params, i, end)+params.cbase]));
}
return hbase;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment