MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Rotation of 2D graphics


This is not an easy problem. The main difficulty is including frame labels
and plot labels. Mathematica has some arcane and secret algorithim for
positioning these, which among other things depends upon the size of the
image. This is why it is so difficult to align vertical plots in
GraphicsArray. So, in my solution I am adding these labels in an extra step
in a controlled method.

I assume that you want an animation so first we need:


FullGraphics will extract all the primitive graphics for a plot, including
axes, tick marks and tick labels. So here is a sample plot.

g = FullGraphics[Plot[Sin[x], {x, 0, 2*Pi}, Frame -> True]];

I left out the frame and plot labels, so here I add them using Text

glab = Graphics[Join[First[g], {Text[x, {Pi, -1.6}], Text[y, {-1.5, 0}],
     Text["Rotating Plot", {Pi, 1.3}]}]];

This shows the plot that we will rotate. One could adjust the positions of
the labels.

Show[glab, PlotRange -> All];

This is a routine to rotate the elements of a piece of graphics. Note that
you cannot use Rectangle, Circle or Disk since these are more primitive
commands where we don't have access to the points. You can replace them with
Lines, Polygons and parametrically drawn circles. This routine moves the
rotation point to the center of the new graphics and rotates about it.

rotate[t_, {x_, y_}][g_] := Module[{g2, rule, a, p, q},
   rule = {(p_)?NumericQ, (q_)?NumericQ} -> {Cos[t]*(p - x) - Sin[t]*(q -
       Sin[t]*(p - x) + Cos[t]*(q - y)};
    rule2 = {(p_)?NumericQ, (q_)?NumericQ} -> {Cos[t]*p - Sin[t]*q,
       Sin[t]*p + Cos[t]*q};
    g2 = g /. Line[a_] :> Line[a /. rule];
    g2 = g2 /. Polygon[a_] :> Point[a /. rule];
    g2 = g2 /. Point[a_] :> Point[a /. rule];
    g2 = g2 /. Text[text_, p_, off_] :> Text[text, p /. rule, off /. rule2,
        {Cos[t], Sin[t]}];
    g2 = g2 /. Text[text_, p_] :>
       Text[text, p /. rule, {0, 0}, {Cos[t], Sin[t]}]]

This is a sample plot, rotated by Pi/4 about the center of the plot.

With[{t = Pi/4, x = Pi, y = 0},
   Show[Graphics[{rotate[t, {x, y}][First[glab]]}], AspectRatio ->
    PlotRange -> {{-10, 10}, {-10, 10}}/2, ImageSize -> 400]];

This produces one frame for a rotating plot.

frame[t_, {x_, y_}] := Show[Graphics[{rotate[t, {x, y}][First[glab]]}],
    AspectRatio -> Automatic, PlotRange -> {{-10, 10}, {-10, 10}}/2,
    ImageSize -> 400];

This spins the plot

Animate[frame[t, {Pi, 0}], {t, 0, 2*Pi - (2*Pi)/24, (2*Pi)/24}]
SelectionMove[EvaluationNotebook[], All, GeneratedCell]

David Park
djmp at

> From: Stephen Sheppard [mailto:Stephen.C.Sheppard at]
To: mathgroup at
> I have some graphics generated by showing several 2d plots, and I
> would like
> to be able to spin these about a specified point.  This is very easy if
> everything has been generated using primitives like Line, Point,
> etc.  What
> I would like to be able to do is to take an entire image that can
> be viewed
> using "Show" and rotate it by r radians about the point {x,y}.  Something
> like:
> Show[%]
> ShowRotate[%,r,{x,y}]
> would then produce the graphic with everything (including the
> axes, labels,
> etc.) rotated.
> Any suggestions?  I would be happy if this were obvious.
> Stephen Sheppard
> Department of  Economics
> Williams College

  • Prev by Date: RE: Rotation of 2D graphics
  • Next by Date: Re: Re: ListPlot bug
  • Previous by thread: RE: Rotation of 2D graphics
  • Next by thread: J/Link trouble on Win2000