Thread: Rotation

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


    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


  2. #2
    Registered User
    Join Date
    Nov 2003
    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
    Portland, Or
    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)

    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
    Hutton, Preston
    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
    Luxembourg, Europe
    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:

    Attachment 7159

    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
    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
    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, 03:50 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM