Skip to content

Instantly share code, notes, and snippets.

@jmbarbier
Last active October 15, 2023 05:23
Show Gist options
  • Save jmbarbier/a83d237fe29f6a5fc51e808c1c1557a3 to your computer and use it in GitHub Desktop.
Save jmbarbier/a83d237fe29f6a5fc51e808c1c1557a3 to your computer and use it in GitHub Desktop.
Hexagonal compact jscad
// Hexagonal compact
const jscad = require('@jscad/modeling')
const {cube, sphere, polygon} = jscad.primitives
const {translate, rotate} = jscad.transforms
const {union, intersect} = jscad.booleans
const {colorize} = jscad.colors
const {sqrt} = Math
const {extrudeLinear} = jscad.extrusions
const {degToRad} = jscad.utils
const getParameterDefinitions = () => {
return [
{ name: 'radius', type: 'int', initial: 20, caption: 'Rayon en mm ?' },
{ name: 'recouvrement', type: 'int', initial: 10, caption: 'Recouvrement en 1/10 de mm ?' },
{ name: 'qualite', type:'int', initial: 40, caption: 'Qualité des faces ?' }
];
}
const main = (params) => {
const R = params.radius; // Rayon
const m = params.recouvrement / 10; // Recouvrement en mm
const sph = {radius: R+m, segments: params.qualite};
const couche1 = union(
translate([0,0,0], sphere(sph)),
colorize([1,0,0,1], rotate([0,0,degToRad(1*60)], translate([2*R, 0,0], sphere(sph)))),
colorize([1,0,0,1], rotate([0,0,degToRad(2*60)], translate([2*R, 0,0], sphere(sph)))),
colorize([1,0,0,1], rotate([0,0,degToRad(3*60)], translate([2*R, 0,0], sphere(sph)))),
colorize([1,0,0,1], rotate([0,0,degToRad(4*60)], translate([2*R, 0,0], sphere(sph)))),
colorize([1,0,0,1], rotate([0,0,degToRad(5*60)], translate([2*R, 0,0], sphere(sph)))),
colorize([1,0,0,1], rotate([0,0,degToRad(6*60)], translate([2*R, 0,0], sphere(sph))))
);
const couche2 = union(
translate([R,sqrt(3)*R/3,R*sqrt(8/3)], couche1),
translate([-R,sqrt(3)*R/3,R*sqrt(8/3)],
rotate([0,0,degToRad(4*60)], translate([2*R, 0,0], sphere(sph)))));
const couche3 = translate([0,0,2*R*sqrt(8/3)], couche1)
const poly = extrudeLinear(
{height: 2*R*sqrt(8/3)},
polygon({points: [[2*R,0], [2*R/2, 2*sqrt(3)/2*R], [-2*R/2, 2*R*sqrt(3)/2],
[-2*R, 0], [-2*R/2, -2*sqrt(3)/2*R], [2*R/2, -2*R*sqrt(3)/2]] })
);
const mesh = intersect(
union(colorize([0,1,0, 0.5], couche1),
colorize([1,0,0,0.5], couche2),
colorize([0,0,1,0.5], couche3)),
poly);
return mesh;
}
module.exports = {main, getParameterDefinitions }
@jmbarbier
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment