Skip to content

Instantly share code, notes, and snippets.

@donbright
Last active October 13, 2016 02:05
Show Gist options
  • Save donbright/b1999d40048b771d32fe9324e904a9f8 to your computer and use it in GitHub Desktop.
Save donbright/b1999d40048b771d32fe9324e904a9f8 to your computer and use it in GitHub Desktop.
cylinder_to_point.scad
// public domain, don bright 2015, http://github.com/donbright
/* Cylinder from 0,0,0 to point x,y,z
very inefficient but it seems to work OK
see also
http://www.povray.org/documentation/view/3.6.0/297/
http://planning.cs.uiuc.edu/node102.html
http://www.songho.ca/opengl/gl_anglestoaxes.html
openscad source code transform.cc
*/
/*
arc sin (sine inverse) for four quadrants.
in 2 dimensional cartesian coordinates.
Input is x coord, y coord, and Spread ( y*y / ( x*x+y*y ) )
echo( full_asin(sqrt(3),1, 1/4 ) ); // returns 30 degrees
echo( full_asin(-sqrt(3),1, 1/4 ) ); // returns 150 degrees
echo( full_asin(-sqrt(3),-1, 1/4 ) ); // returns 210 degrees
echo( full_asin(sqrt(3),-1, 1/4 ) ); // returns -30 degrees
*/
function full_asin(x,y,S) = (
(x<0) ? 180-sign(y)*asin(sqrt(S)) : sign(y)*asin(sqrt(S))
);
module cylinder_topoint(point) {
nx = point[0];
ny = point[1];
nz = point[2];
Qx = nx*nx;
Qy = ny*ny;
Qz = nz*nz;
Syaw = (Qy+Qx)==0 ? 0 : Qy/(Qy+Qx);
Spitch = Qz/(Qx+Qy+Qz);
length = sqrt(Qx+Qy+Qz);
roll = 0;
yaw = full_asin(nx,ny,Syaw);
pitch = -full_asin(abs(nx),nz,Spitch);
rotate([roll,pitch,yaw]) {
rotate([90,0,90]) cylinder(length,1);
}
}
$fn=20;
point=[5*sin($t*360),5*cos($t*360),sin(($t-0.5)*180)*5];
translate(point) sphere(0.4);
cylinder_topoint(point);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment