I thought it would be fun to make such function.
It is a product of using atan acos asin until I got something and then guessing some more until it worked . It seems to be working, though I actually dont understand it completely
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 dxy = Math.sqrt( sqr(dx) + sqr(dy))
let height = Math.sqrt( sqr(dx) + sqr(dy) + sqr(dz))
let ay = Math.atan(dxy/dz) *(dx < 0 ? -1:1)
let az = Math.atan(dy/dx)
let ax = dz < 0 ? -Math.PI:0
let obj = cylinder({radius, height})
let mid = [p1[0]+dx/2,p1[1]+dy/2,p1[2]+dz/2]
return translate(mid, rotate([ax,ay,az], obj))
}
I used these to test visually
function main(){
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([0,0,0],[-45,-45,-45])
return ret;
}