RE: Rotation of 2D graphics

*To*: mathgroup at smc.vnet.net*Subject*: [mg26459] RE: [mg26432] Rotation of 2D graphics*From*: "David Park" <djmp at earthlink.net>*Date*: Thu, 21 Dec 2000 01:52:01 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

Stephen, 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: Needs["Graphics`Animation`"] 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 statements. 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 - y), 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 -> Automatic, 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] FrontEndTokenExecute["OpenCloseGroup"] FrontEndTokenExecute["SelectionAnimate"] David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ > From: Stephen Sheppard [mailto:Stephen.C.Sheppard at williams.edu] To: mathgroup at smc.vnet.net > > 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 >