7
$\begingroup$

If I have the following graphic:

Rotate[Show[{
   Graphics@Line[{{0, 0}, {0, 1}}], 
   Graphics@Line[{{0, 0}, {1, 1}}], 
   Graphics@Plot[x^2, {x, -1, 1}, Axes -> False]}], Pi/3]

It looks like this:

enter image description here

I only want to rotate the content of the graphics, not the frame.

Normally you would use Graphics after rotate, however this doesn't work:

Graphics@Rotate[Show[{
  Graphics@Line[{{0, 0}, {0, 1}}], 
  Graphics@Line[{{0, 0}, {1, 1}}], 
  Graphics@Plot[x^2, {x, -1, 1}, Axes -> False]}], Pi/3]
(* Error: Graphics is not a Graphics primitive or directive. *)
$\endgroup$
1
  • 5
    $\begingroup$ OT: Does anyone else also think that the design of Rotate and associates is somewhat unfortunate? $\endgroup$
    – Yves Klett
    Jan 28, 2013 at 13:23

5 Answers 5

8
$\begingroup$

I think your biggest problem is, that Plot already creates a Graphics object, which cannot be set inside another Graphics. Why don't you extract the contents of your plotted Graphics?

Graphics@Rotate[{Line[{{0, 0}, {0, 1}}], Line[{{0, 0}, {1, 1}}], 
   First@Plot[x^2, {x, -1, 1}, Axes -> False]}, Pi/3]

enter image description here

$\endgroup$
6
$\begingroup$

You can also use MapAt to apply rotations (or other transformations) on parts at position {1} of a graphics object.

g = Show[{Graphics@Line[{{0, 0}, {0, 1}}],   Graphics@Line[{{0, 0}, {1, 1}}],
Plot[x^2, {x, -1, 1}, Axes -> False]}, ImageSize -> 300];
Row[{g, MapAt[Rotate[#, Pi/3] &, g, {1}]}]

enter image description here

 g2 = Graphics[{Line[{{0, -1/2}, {0, 1}}], {Thick, Blue, 
 Line[{{0, -1/2}, {1, 1}}]}, {Opacity[.5, Red], 
 Disk[{-1/4, 2/3}, {1/2, 1/4}, {-Pi/3, Pi}]}, 
 Plot[x Sin[6 x + 4], {x, -1, 1}, Axes -> False, PlotStyle -> {Thickness[.02], Orange}][[1]]},
 ImageSize -> 300];
 Row[{g2, MapAt[Rotate[#, Pi/3] &, g2, {1}]}]

enter image description here

Applying Rotate to individual parts:

 Grid[Partition[Column[{Row[{"Rotate  ", #}],
  MapAt[Rotate[#, Pi/3] &, g2, {1, #}]}, Center] & /@
  {All, 1, 2, 3, 4, {1, 3}, {1, 4}, {2, 3}, {3, 4}}, {3}],
 Dividers -> All, ItemSize -> {25, 20}, Alignment -> Top]

enter image description here

Other transformations:

 g2B = MapAt[ GeometricTransformation[#, 
  ReflectionTransform[{Cos[Pi/3], Sin[Pi/3]}]] &, g2, {1, 4}];
 Row[{g2, g2B, Show[g2, g2B]}]

enter image description here

 g2C = MapAt[ GeometricTransformation[#, 
 ShearingTransform[Pi/4, {1, 0}, {0, 1}]] &, g2, {1, 4}];
 Row[{g2, g2C, Show[g2, g2C]}]

enter image description here

$\endgroup$
3
$\begingroup$

It is not necessary to wrap each Line into a Graphics statement. Same holds for Line which can take more than two points as argument.

To rotate the lines you have to transform the coordinates, not the final output box. Here I use the general command GeometricTransform:

Graphics@GeometricTransformation[
  Line[{{{0, 0}, {0, 1}}, {{0, 0}, {1, 1}}}], RotationTransform[Pi/3]]

enter image description here

$\endgroup$
1
  • $\begingroup$ However I actually have other functions in show than Line, for example Plot and Circle, so I can't do this. $\endgroup$
    – Tyilo
    Jan 28, 2013 at 13:01
2
$\begingroup$

You can also get the desired result without GeometricTransformation by putting the Rotate inside the Graphics but before Line:

Graphics@Rotate[Line[{{{0, 0}, {0, 1}}, {{0, 0}, {1, 1}}}], Pi/3]

enter image description here

$\endgroup$
3
  • $\begingroup$ I'm using other Graphics types than Line, so this won't work. $\endgroup$
    – Tyilo
    Jan 28, 2013 at 13:02
  • $\begingroup$ Then please edit your post with an example that does not work like this. I'm pretty sure we can find a very similar solution to that. ;) $\endgroup$
    – einbandi
    Jan 28, 2013 at 13:07
  • $\begingroup$ I have updated the question. $\endgroup$
    – Tyilo
    Jan 28, 2013 at 13:12
1
$\begingroup$

The Presentations Application (I'm the author) is, I believe, much more convenient and intuitive for doing this kind of thing. Curves and primitives such as Line are all treated on the same level. No need to repeatedly jump between graphics levels. The transform operations have also been repackaged as postfix operators. You can rotate all the primitives as a group inside of the Draw2D wrapper (similar to Show). Everything is rotated inside of the frame. So this operation looks like the following:

<< Presentations`

Draw2D[
 {{Draw[x^2, {x, -1, 1}],
    Line[{{0, 0}, {1, 1}}],
    Line[{{0, 0}, {0, 1}}]} // RotateOp[\[Pi]/3]},
 PlotRange -> All,
 ImageSize -> 250
 ]

enter image description here

With rotation you may want to specify the center of rotation as a second argument in the RotateOp statement.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.