8

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]);

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465

2 Answers2

32

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();   
}
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
  • 2
    thanks, this really should've be built in to the rotate command – argentum2f Apr 11 '19 at 23:50
  • 1
    +1. This really should be part of the language's syntax. But thanks for the snippet. I ended up rewriting it for my purposes to take two array parameters (the three axis rotation amounts and the point), but it saved me a decent chunk of time having that as a starting point. – dgatwood Oct 16 '22 at 19:56
2

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]);