# Cylinder definition

6 messages
Open this post in threaded view
|

## Cylinder definition

 Does anyone know of an "easy" way to create a cylinder of radius r with endpoints at x1, y1, z1 and x2, y2, z2? I assume there's some way to transform these endpoint coordinates into some combination of height/rotation/translation,  but the required geometry is eluding me. Thanks, Joe ------- Sent from my gewgaw.
Open this post in threaded view
|

## Re: Cylinder definition

 You can just use trig functions to calculate the height of the cylinder and the angle of rotation. Regards Giles On Jun 1, 2012 8:18 PM, "Joseph Eddy" <[hidden email]> wrote: > > Does anyone know of an "easy" way to create a cylinder of radius r with endpoints at x1, y1, z1 and x2, y2, z2? > > I assume there's some way to transform these endpoint coordinates into some combination of height/rotation/translation,  but the required geometry is eluding me. > > Thanks, > Joe > > ------- > Sent from my gewgaw. > > > _______________________________________________ > OpenSCAD mailing list > [hidden email] > http://rocklinux.net/mailman/listinfo/openscad >
Open this post in threaded view
|

## Re: Cylinder definition

 In reply to this post by jceddy On Sat, Jun 2, 2012 at 5:18 AM, Joseph Eddy wrote: Does anyone know of an "easy" way to create a cylinder of radius r with endpoints at x1, y1, z1 and x2, y2, z2?If you're ok with rounded endcaps, try this: module bar(r, x1, y1, z1, x2, y2, z2) {  hull() {    translate([x1, y1, z1]) sphere(r=r);    translate([x2, y2, z2]) sphere(r=r);  } }
Open this post in threaded view
|

## Re: Cylinder definition

 In reply to this post by jceddy Here is something I use: function elen(vec)= sqrt(vec[0]*vec[0]+vec[1]*vec[1]+vec[2]*vec[2]); function cprod(a,b)=[a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]]; function dprod(a,b)=a[0]*b[0]+a[1]*b[1]+a[2]*b[2]; function vangle(a,b)=acos(dprod(a,b)/(elen(a)*elen(b))); module link(a,b,r=3,cr=3){ diff=b-a; translate(a)sphere(r=cr,center=true,\$fn=8); translate(b)sphere(r=cr,center=true,\$fn=8); translate(a+diff/2)rotate(vangle(diff,[0,0,1]),-cprod(diff,[0,0,1]))cylinder(r=r,h=elen(diff),center=true,\$fn=8); } link([x1,y1,z1],[x2,y2,z2],r); will then do what you want. Remove the spheres if you don't want them. Kliment On 06/01/2012 09:18 PM, Joseph Eddy wrote: Does anyone know of an "easy" way to create a cylinder of radius r with endpoints at x1, y1, z1 and x2, y2, z2? I assume there's some way to transform these endpoint coordinates into some combination of height/rotation/translation,  but the required geometry is eluding me. Thanks, Joe ------- Sent from my gewgaw. ```_______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad ```
Open this post in threaded view
|

## Re: Cylinder definition

 In reply to this post by jceddy I ended up using the hull of two spheres method...this also saved me from having to use extra spheres to round the endcaps.  :)  Thanks! ------- Sent from my gewgaw.
Open this post in threaded view
|

## Re: Cylinder definition

 On Sat, Jun 2, 2012 at 11:49 AM, Joseph Eddy wrote: I ended up using the hull of two spheres method...this also saved me from having to use extra spheres to round the endcaps.  :)  Thanks!one of the advantages of the hull method is that your bar is actually manifold, which is very tricky when adding spheres on the end of a cylinder :)