[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





// 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
// 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
// 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 from out of the x-y-plane into the z-direction
// rotate the cylinder into the plus x-direction
// make an arrow, the tip is 20% of the total length
// shaft
// tip


// 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 )
// translate to the starting point of the vector
// 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].
// draw the vector from point chain[i] to chain[i+1]

//     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