# Cylinders

5 messages
Open this post in threaded view
|

## Cylinders

 Me again - minor correction in the translation (note p1 from translate(vector/2 + p1) which was missing)   module cylinder_ep(p1, p2, r1, r2) { translate(p1)sphere(r=r1,center=true); translate(p2)sphere(r=r2,center=true); assign(vector = [p2[0] - p1[0],p2[1] - p1[1],p2[2] - p1[2]]) assign(distance = sqrt(pow(vector[0], 2) + pow(vector[1], 2) + pow(vector[2], 2))) translate(vector/2 + p1) //rotation of XoY plane by the Z axis with the angle of the [p1 p2] line projection with the X axis on the XoY plane rotate([0, 0, atan2(vector[1], vector[0])]) //rotation //rotation of ZoX plane by the y axis with the angle given by the z coordinate and the sqrt(x^2 + y^2)) point in the XoY plane rotate([0, atan2(sqrt(pow(vector[0], 2)+pow(vector[1], 2)),vector[2]), 0]) cylinder(h = distance, r1 = r1, r2 = r2,center=true); } cylinder_ep([0, 20, -30], [-5, 10, 15], 2, 1, \$fn=20);   Andrei
Open this post in threaded view
|

## Re: Cylinders

 beautiful!please submit this to the MCAD project, http://github.com/elmom/MCAD and/or post on thingiverse On Sat, Jan 28, 2012 at 7:51 AM, Pasca Andrei wrote: Me again - minor correction in the translation (note p1 from translate(vector/2 + p1) which was missing)   module cylinder_ep(p1, p2, r1, r2) { translate(p1)sphere(r=r1,center=true); translate(p2)sphere(r=r2,center=true); assign(vector = [p2[0] - p1[0],p2[1] - p1[1],p2[2] - p1[2]]) assign(distance = sqrt(pow(vector[0], 2) + pow(vector[1], 2) + pow(vector[2], 2))) translate(vector/2 + p1) //rotation of XoY plane by the Z axis with the angle of the [p1 p2] line projection with the X axis on the XoY plane rotate([0, 0, atan2(vector[1], vector[0])]) //rotation //rotation of ZoX plane by the y axis with the angle given by the z coordinate and the sqrt(x^2 + y^2)) point in the XoY plane rotate([0, atan2(sqrt(pow(vector[0], 2)+pow(vector[1], 2)),vector[2]), 0]) cylinder(h = distance, r1 = r1, r2 = r2,center=true); } cylinder_ep([0, 20, -30], [-5, 10, 15], 2, 1, \$fn=20);   Andrei_______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad
Open this post in threaded view
|

## Re: Cylinders

 That's great you figured out the math behind it, although with the rounded ends, a simpler solution emerges. module cylinder_ep(p1, p2, r1, r2) { hull() { translate(p1)sphere(r=r1,center=true); translate(p2)sphere(r=r2,center=true); } } cylinder_ep([0, 0, 0], [6, 11, 15], 3, 1, \$fn=20); I guess the downside to this approach is that it takes a bit longer to render, but it removes the seems between the spheres and cylinders.On Fri, Jan 27, 2012 at 4:51 PM, Triffid Hunter wrote: beautiful!please submit this to the MCAD project, http://github.com/elmom/MCAD and/or post on thingiverse On Sat, Jan 28, 2012 at 7:51 AM, Pasca Andrei wrote: Me again - minor correction in the translation (note p1 from translate(vector/2 + p1) which was missing)   module cylinder_ep(p1, p2, r1, r2) { translate(p1)sphere(r=r1,center=true); translate(p2)sphere(r=r2,center=true); assign(vector = [p2[0] - p1[0],p2[1] - p1[1],p2[2] - p1[2]]) assign(distance = sqrt(pow(vector[0], 2) + pow(vector[1], 2) + pow(vector[2], 2))) translate(vector/2 + p1) //rotation of XoY plane by the Z axis with the angle of the [p1 p2] line projection with the X axis on the XoY plane rotate([0, 0, atan2(vector[1], vector[0])]) //rotation //rotation of ZoX plane by the y axis with the angle given by the z coordinate and the sqrt(x^2 + y^2)) point in the XoY plane rotate([0, atan2(sqrt(pow(vector[0], 2)+pow(vector[1], 2)),vector[2]), 0]) cylinder(h = distance, r1 = r1, r2 = r2,center=true); } cylinder_ep([0, 20, -30], [-5, 10, 15], 2, 1, \$fn=20);   Andrei_______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad