[OpenSCAD] extending MCAD with an example for rotation
Peter Falke
stempeldergeschichte at googlemail.com
Wed Apr 1 13:52:44 EDT 2015
Rotating something into the direction of a vector is a common problem.
It only requires high school math.
But to get the rotation into the right order is often tricky.
I feel this would be a good addition to the MCAD library.
Please have a look at the following code:
// rotation.scad - Basic example of rotation to draw a vector as an arrow
vect1=[2,3,4];
%cube(vect1);
vector(vect1);
chain=[
[0,0,0],
[0,1,0],
[4,2,3],
[5,2,1],
[7,8,9],
[6,5,2],
];
vector_chain();
//
// Draw an arrow from the origin to the position of the vector v.
// To do this draw an arrow with the length of the length of the vector v.
// Then rotate this arrow into the correct direction.
//
module vector(v){
// radius of the arrow
r=.05;
// number of sides of the arrow
fn_vector=16; //change this to: 3, 4, 16, 32, or 100 (set to 4 makes it
square, set to higher number to make it rounder. But be aware: high numbers
lead to (excessively) long render times.
// length of the arrow
l=norm(v);
// rotate the projection of the vector onto the x-y-plane around the z-axis
// rotate(scalar)=rotate(scalar*[0,0,1])=rotation around the z-axis
rotate(atan2(v[1],v[0]))
// rotate from out of the x-y-plane into the z-direction
rotate([0,-atan2(v[2],norm([v[0],v[1]])),0])
// rotate the cylinder into the plus x-direction
rotate([0,90,0]){
// make an arrow, the tip is 20% of the total length
// shaft
cylinder(h=l*.8,r1=r,r2=r,$fn=fn_vector);
// tip
translate([0,0,l*.8])cylinder(h=l*.2,r1=3*r,r2=0,$fn=fn_vector);
}
}
// Draw a chain of vectors along an array of points
module vector_chain(){
// iterate over all elements of the chain
// ( len(array) gives the number of elements in the array )
for(i=[0:len(chain)-2]){
// translate to the starting point of the vector
translate(chain[i])
// change the color [r,g,b] of the vector
// The first one is blue:[0,0,1], the last one is red:[1,0,0].
color([i/(len(chain)-2),0,1-i/(len(chain)-2)])
// draw the vector from point chain[i] to chain[i+1]
vector(chain[i+1]-chain[i]);
}
}
echo(version=version());
// Written by: TakeItAndRun
// 2015
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any
// warranty.
//
// You should have received a copy of the CC0 Public Domain
// Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150401/193fc5c5/attachment-0002.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rotation.png
Type: image/png
Size: 13612 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150401/193fc5c5/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rotation.scad
Type: application/octet-stream
Size: 2313 bytes
Desc: not available
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150401/193fc5c5/attachment.scad>
More information about the Discuss
mailing list