Rotation

This is a discussion on Rotation within the Windows Programming forums, part of the Platform Specific Boards category; Does anyone know how i would go about rotating an image 90 degrees clockwise and anticlockwise as i am finding ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Rotation

    Does anyone know how i would go about rotating an image 90 degrees clockwise and anticlockwise as i am finding it hard to find information on it

    i am using borland c++ builder

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    trigonometry
    lets say you have a pixel at x , y. where (0, 0) is top left. (w, h) is width height. (a,b) = (x-w/2, h/2 - y)
    where m is the length from (0, 0) defined at center of image to pixel
    x' = mcos(k+90) = -sin(k)m = -b
    y' = msin(k+90) = mcos(k) = a

    apply transformation

    x'' = x' + w/2 = w/2 - b = w/2 - [h/2 - y] = w/2 + y - h/2
    y'' = h/2 - y' = h/2 - a = h/2 - [x/2 - w/2] = h/2 + w/2 - x/2

    your final points x'' and y'' are the new coordinates. For better effect you might want to do a similar thing except assume you have a point go backward to find what pixel should go there. This way it will eliminate "white spots", and look more continious.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Um, or just take the container holding the individual pixels and put them into a new container where the width == height of the original, and height == width of the original.

    Say you have a 3 by 4 pixel image (very simple)

    Code:
    1  2  3
    4  5  6
    7  8  9
    10 11 12
    So pixel 1 would go in the bottom left pixel slot 2 would be just above that. And so on and so forth. Perhaps if I get bored I will show an example with an array.

  4. #4
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah i think i understand what you mean - i am unsure how i would implement this tho - can you give me a few hints on how to cover come this?

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Basically, if you have an pixel with coordinates x,y and you want to get the pixel coordinates after an rotation of alpha degrees, you multiply the coordinates in an 3 dimensional space by the following matrix:

    Name:  matrices_2.gif
Views: 125
Size:  1.7 KB

    What you have to do is create an destination image with the same dimensions as your original image (or different dimensions if you calculate the bounding box of the 4 corner pixels rotated by that matrix) and then for each pixel of the destination image, you apply the inverted rotation matrix to find the source pixel.

    The code would look like this:

    1. Calculate the inverse of the rotation matrix:
    2. Create an empty image with the same dimensions as your original image
    3.
    Code:
    for (i = 0; i < width; i++)
    {
        for (j = 0; j < height; j++)
        {
            pixel(i,j) = InverseRotation(i,j);
        }
    }
    Since it's a 3x3 matrix, you need to transform your 2D coordinations x,y into a 3D coordinate system by fixing the 3rd dimensions z to 1. (x,y) -> (x,y,1).

    But seeing what you did with the last thread I tried to help you, instead of trying to understand how things really work, you try to find a function somewhere that does the job for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  2. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. one final problem with rotation
    By DavidP in forum Game Programming
    Replies: 3
    Last Post: 11-19-2003, 02:50 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21