home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.graphics
- Path: sparky!uunet!cs.utexas.edu!torn!news.ccs.queensu.ca!qucis.queensu.ca!hausner
- From: hausner@qucis.queensu.ca (Alejo Hausner)
- Subject: fast bitmap rotation
- Message-ID: <1993Jan9.045327.21333@qucis.queensu.ca>
- Keywords: shear, rotation, bitmap
- Organization: M.Sc, C.S, Queen's, Kingston, Canada.
- Date: Sat, 9 Jan 1993 04:53:27 GMT
- Lines: 80
-
-
- How to rotate a bitmap using two shears:
- (from Foley and van Dam).
-
- Reference: E. Catmull and A.R. Smith, "3-D transformations of
- images in scan-line order", SIGGRAPH 80, 279-285.
-
- You want to rotate an n x n array of pixels by an angle phi.
- A mathematician would tell you that the necessary transformation
- is:
-
- x' = x cos(phi) - y sin(phi)
- y' = x sin(phi) + y cos(phi)
-
- But this involves n^2 multiplications, and is slow.
- If we replace it with a two-step transformation, we can do it
- faster. First we go from (x,y) to (u,v), then to (x',y'). The
- transformation equations are:
-
- u = x
- v = x sin(phi) + y cos(phi)
-
- x' = u sec(phi) - v tan(phi)
- y' = v
-
- If you substitute the first pair into the second pair of
- equations, you retrieve the original transformation, so it works.
-
-
- Note that when phi=90 degrees, the secant and tangent terms are
- infinite, so the transformation doesn't work. Hence rotations by
- 90 and 270 degrees must be handled as special cases. This isn't
- a problem. Also if phi is close to 90 or 270 degrees, there will
- be accuracy problems, and in that case it is better to rotate the
- bitmap by 90 (or 270) degrees first, and then rotate by 90-phi
- (or 270-phi) degrees.
-
- Algorithm:
-
- sinphi = sin(phi)
- cosphi = cos(phi)
- secphi = 1.0/cosphi
- tanphi = sinphi/cosphi
-
- for x = 1 to n
- xs[x] = x * sinphi
- us[x] = x * secphi
- ys[x] = x * cosphi
- vt[x] = x * tanphi
- end for
-
- for x = 1 to n
- u = x
- xsinphi = xs[x]
- for y = 1 to n
- v = xsinphi + yc[y]
-
- array2[u,v] = array[x,y]
- end for
- end for
-
- for v = 1 to n
- yp = v
- vtanphi = vt[v]
- for u = 1 to n
- xp = us[u] - vtanphi
-
- array[xp,yp] = array2[u,v]
- end for
- end for
-
-
- I hope that works. I just threw it together on the fly.
-
-
- Alejo Hausner (hausner@qucis.queensu.ca)
-
- --
- Alejo Hausner. (hausner@qucis.queensu.ca)
-
-