Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenSCAD: Rotating around a particular point?

Tags:

openscad

The following code rotates the second cube around the origin. How can I rotate the second cube around its center point ([5,5,0]) instead?

cube([10,10,1]);
rotate([0,0,45]) cube([10,10,1]);

like image 578
Mark Harrison Avatar asked Aug 22 '17 20:08

Mark Harrison


2 Answers

This module will perform the desired rotation.

// rotate as per a, v, but around point pt
module rotate_about_pt(a, v, pt) {
    translate(pt)
        rotate(a,v)
            translate(-pt)
                children();   
}

cube([10,10,1]);
rotate_about_pt(45,0,[5,5,0]) cube([10,10,1]);

In newer versions (tested with the January 2019 preview) the above code generates a warning. To fix that, update the parameters to rotate:

module rotate_about_pt(z, y, pt) {
    translate(pt)
        rotate([0, y, z]) // CHANGE HERE
            translate(-pt)
                children();   
}
like image 198
Mark Harrison Avatar answered Oct 05 '22 05:10

Mark Harrison


If you are willing to 'center' the shape it is much easier:

cube(center =true,[10,10,1]);
rotate([0,0,45]) cube(center =true,[10,10,1]);
like image 38
Ryan Jarvis Avatar answered Oct 05 '22 05:10

Ryan Jarvis