Page 1 of 1

PostPosted: Wed Mar 03, 2004 9:22 am
by dan_priv
:) Hi all

There's a function in PocketC allowing you to rotate images for 90/180 degrees.

The question now is: Is it possible to rotate an image by a certain value (eg. 5 degrees)?

If there's no such thing, does anybody know about a method to do that?

Help is very much apreciated...

Thanks in advance, with best regards

PostPosted: Thu Mar 04, 2004 12:17 am
by wolfgang
Rotating by an arbitrary angle is much more complex than 90 and 180 degree, because the - square! - pixel-grid does not match anymore. You'll also need to change (increase) the size of the rectangular sprite to hold the rotated version. You need to determine for each point in the new (rotated) sprite where it "comes from" in the old (original) sprite. If you want to get even more sophisticated, you should weigh the value from adjacent points so the picture becomes smoother and avoids raggedness due to aliasing (even though that works better for photos and at higher resolution than for painted, low-res sprites). Basic transformation:

For all points in the new sprite:

r_new = sqrt(x_new^2 + y_new^2)
phi_new = acos (x_new / r_new) (beware of the special case r_new=0, and also you'll need to treat each quadrant on its own because acos() is not unique across the full circle)

Calculate the original location (rho is the rotation angle):
phi_old = phi_new - rho
r_old = r_new
x_old = r_old * cos (phi_old)
y_old = r_old * sin (phi_old)

Get the color of pixel (x_old, y_old) in the old sprite. This is the color for pixel (x_new, y_new) in the rotated sprite.

Assume the old sprite is x_max wide and y_max high.

r_max = sqrt(x_max^2 + y_max^2)

To be on the save side, so the rotated picture always fits, make the new sprite r_max wide and r_max high.

All those formulas assume the origin (0,0) is in the center of the sprite!

Hope that gets you going!


Composing music on the PocketPC! -

PostPosted: Thu Mar 04, 2004 12:56 pm
by dan_priv
Thank's wolfgang!

This seems to be the solution!

Thanks you very much for your time and tip!!! When I get lucky coding this, I'll ow you a free copy of my Program.

With best regards


PostPosted: Thu Mar 04, 2004 6:00 pm
by wolfgang
Hi again,

I forgot to mention a few details:

The angle rho is the angle measured from the horizontal axis (x-axis).

I'm not sure acos() is available in PocketC, but I am positive atan() is. The easiest way then is to use:


(again caution with x_new=0 OR EVEN JUST CLOSE TO ZERO, and treat every quadrant separately).

To avoid piling up roundoff/aliasing errors dur to subsequent rotations (a big problem especially with the coarse resolution of a Palm screen), it is best to keep the original (unrotated) sprite in memory and calculate all rotated ones directly from this one, instead of calculating rotation 1 from the original, then rotation 2 from rotation 1, etc.


Composing music on the PocketPC! -