Re: Rotating a 2D graphic on screen?
- To: mathgroup at smc.vnet.net
- Subject: [mg43699] Re: [mg43682] Rotating a 2D graphic on screen?
- From: Garry Helzer <gah at math.umd.edu>
- Date: Tue, 30 Sep 2003 16:42:55 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On Monday, September 29, 2003, at 01:47 AM, AES/newspost wrote: > Any easy way to rotate a 2D graphic about it's center by an arbitrary > number of degrees _after_ I've created it? > > I'd like to lay out and program the graphics in a certain set of x,y > coordinates where the equations and formulas are simplest -- then > rotate > and display it in a rotated set of x',y' = Horiz,Vert coordinates. I > can of course do the transformations "by hand" as I go along -- but > it's just a messy process. > > Illustrator can do it, instantly, with an EPS graphic. Sure would be > nice if Mathematica could? > > Shift the graphic coordinates to homogeneous coordinates, multiply by a 3 by 3 homogeneous coordinate rotation matrix, and switch back to graphic coordinates. Homogeneous coordinates for the point (a,b) are (1,a,b) and graphic coordinates for (a,b,c) are (b/a,c/a). The function Transform[matrix][graphic] will produce the desired graphic. It uses two utilities called Up and Down that switch between pairs and triples. Up[x_] := Flatten[{1, x}] Down[x_] := Rest[x]/First[x] Transform[M_][graphic_] := graphic /. {Point[pt_] :> Point[Down[M.Up[pt]]], Line[ptlist_] :> Line[Down[M.Up[#]] & /@ ptlist], Polygon[ptlist_] :> Polygon[Down[M.Up[#]] & /@ ptlist], Text[a_, pt_, b___] :> Text[a, Down[M.Up[pt]], b]} A 3 by 3 matrix for rotation about the origin is {{1, 0, 0}, {0, Cos[t], -Sin[t]}, {0, Sin[t], Cos[t]}} A matrix for a translation with translation vector (a,b) is {{1, 0, 0}, {a, 1, 0}, {b, 0, 1}} To rotate about a center (a,b), translate (a,b) to the origin, rotate, translate back(multiply the matrices then use Transform). Other types of transformations and examples of graphics built using them can be found in Chapter 1, Section 3 of the lecture notes at http://www.math.umd.edu/~gah/Pages/Math431Lecturesf03.html Garry Helzer Department of Mathematics University of Maryland 1303 Math Bldg College Park, MD 20742-4015