[OpenSCAD] Can I apply a transformation to a point (list)?

Troberg troberg.anders at gmail.com
Mon Oct 15 03:22:25 EDT 2018


Some very simple functions I made for the purpose. You need to use the two
first, but they rely on some of the others, so you need to include them as
well.

//Translate coordinate
function xtrancoord(dx,co)=[co[0]+dx,co[1],co[2]];

//Rotate coordinate
function
rotcoord(rot,co)=zrotcoord(rot[2],yrotcoord(rot[1],xrotcoord(rot[0],co)));

//Translate along one axis only
function ytrancoord(dy,co)=[co[0],co[1]+dy,co[2]];
function ztrancoord(dz,co)=[co[0],co[1],co[2]+dz];
function trancoord(d,co)=[co[0]+d[0],co[1]+d[1],co[2]+d[2]];

//Rotate around one axis only
function xrotcoord(rx,co)=[co[0],
							rotcoord_yz(co[1],co[2],rx)[1],
							rotcoord_yz(co[1],co[2],rx)[2]];
function yrotcoord(ry,co)=[rotcoord_xz(co[0],co[2],-ry)[0],
							co[1],
							rotcoord_xz(co[0],co[2],-ry)[2]];
function zrotcoord(rz,co)=[rotcoord_xy(co[0],co[1],rz)[0],
							rotcoord_xy(co[0],co[1],rz)[1],
							co[2]];

//Angle between origin and 2D point
function
angle2d(x2,z2)=-(-90+((0<z2)?0:180)+((0<x2)?-1:1)*atan(sqrt(pow(0-x2,2)+pow(0-0,2))/(0-z2)));

Distance between origin and 2D point
function distance2d(x2,y2)=sqrt(pow(0-x2,2)+pow(0-y2,2));

//Returns rotation vector between two points, assuming the object to be
rotated is along the X axis
function angle(x1,y1,z1,x2,y2,z2)=[0,
                                  
-90+((z1<z2)?0:180)+((x1<x2)?-1:1)*atan(sqrt(pow(x1-x2,2)+pow(y1-y2,2))/(z1-z2)),
                                   atan((y1-y2)/(x1-x2))];

//Distance between two points in 3D
function
distance(x1,y1,z1,x2,y2,z2)=sqrt(pow(x1-x2,2)+pow(y1-y2,2)+pow(z1-z2,2));

//These are just helpers
function rotcoord_yz(y,z,rot)=pol2rec_yz(distance2d(y,z),angle2d(y,z)+rot);
function rotcoord_xz(x,z,rot)=pol2rec_xz(distance2d(x,z),angle2d(x,z)+rot);
function rotcoord_xy(x,y,rot)=pol2rec_xy(distance2d(x,y),angle2d(x,y)+rot);	
function pol2rec_yz(dist,rot)=[0,
								dist*cos(rot),
								dist*sin(rot)];
function pol2rec_xz(dist,rot)=[dist*cos(rot),
								0,
								dist*sin(rot)];
function pol2rec_xy(dist,rot)=[dist*cos(rot),
								dist*sin(rot),
								0];


Note: For convenience and clearer code, I use modules to do xtran, ytran,
ztran, xrot, yrot, zrot, with counterparts for coordinates. Makes things
much clearer when you only move/rotate in one direction.



--
Sent from: http://forum.openscad.org/



More information about the Discuss mailing list