Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
openjscad utility cylinderFromTo
const jscad = require('@jscad/modeling')
const { connectors, geometry, math, primitives, text, utils, booleans, expansions, extrusions, hulls, measurements, transforms } = jscad
const { translate, scale, center, rotate } = transforms
const { cuboid, sphere, cylinder, circle, star,cylinderElliptic } = jscad.primitives
function main(){
var ret = [];
function testCylinderFromTo(p1,p2){
ret = [ ...ret,
translate(p1,sphere({size:1})),
translate(p2,sphere({size:1})),
jscad.colors.colorize([1,0,0,0.7],cylinderFromTo(p1,p2,2)),
];
}
testCylinderFromTo([-20,30,50],[-40,-10,70]);
testCylinderFromTo([0,0,0],[10,10,10])
testCylinderFromTo([0,0,0],[20,-20,20])
testCylinderFromTo([0,0,0],[-30,30,30])
testCylinderFromTo([0,0,0],[-40,-40,40])
testCylinderFromTo([0,0,0],[15,15,-15])
testCylinderFromTo([0,0,0],[25,-25,-25])
testCylinderFromTo([0,0,0],[-35,35,-35])
testCylinderFromTo([100,0,0],[100,0,30])
testCylinderFromTo([100,0,0],[100,0,-30])
testCylinderFromTo([100,0,0],[100,30,0])
testCylinderFromTo([100,0,0],[100,-30,0])
testCylinderFromTo([100,0,0],[130,0,0])
testCylinderFromTo([100,0,0],[70,0,0])
return ret;
}
function cylinderFromTo(p1,p2, radius){
const sqr = x=>x*x
let dx = p2[0] - p1[0]
let dy = p2[1] - p1[1]
let dz = p2[2] - p1[2]
let height = Math.sqrt( sqr(dx) + sqr(dy) + sqr(dz))
let obj = cylinder({radius, height})
if(dx || dy){
let dxy = Math.sqrt( sqr(dx) + sqr(dy))
let ay = Math.atan(dxy/dz) *(dx < 0 ? -1:1)
let az = Math.atan(dy/dx)
let ax = dz < 0 ? -Math.PI:0
obj = rotate([ax,ay,az], obj)
}
let mid = [p1[0]+dx/2,p1[1]+dy/2,p1[2]+dz/2]
return translate(mid, obj)
}
module.exports = {main}
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.