# Thread: Rotating 2d array image

1. Member
Join Date
Mar 2012
Location
Sweden
Posts
88
Rep Power
0

## Rotating 2d array image

So I store my images in pixel arrays and I want to rotate them any amount and I don't know how to accomplish that. I know how to rotate the image 90 degrees but that isn't hard. I'm more interested in 45,30 or even 286. So is there a way to make this happen or is this some really complicated thing I need to do.

FYI, I'm not that great at tan, cos,sin and atan and all that there is, but I'm open to learn about it to get better.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Rotating 2d array image

The trig functions and idea of "similar triangles" on which they rest have worked well for several millenia; fan or not I don't think you'll easily be able to come up with something better!

When you rotate by some arbitrary angle a rather awkward problem emerges. Each pixel in the source image will rotate around so that it falls on the intersection where four pixels in the destination image meet. The same observation works in reverse: each pixel in the destination image will be made up of "contributions" from four different pixels in the source image. Figuring out which pixels and the relative contribution of each will require trigonometry. Once you have the Maths sorted out you have then to implement it carefully and deal with the messy details like the edge pixels.

Personally I wouldn't bother. Java has an Image class which does a better job of representing images than a general purpose 2D array. And the Java 2D API provides ways of applying transformations to images that are fast and accurate.

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Rotating 2d array image

You can rotate the graphics context by casting the Graphics context g in the paintComponent method to Graphics2D. You can then use the rotate method. However, the image is rotated about the origin so you would need to translate the origin to somewhere else, say the center of the panel and then draw the image starting at x = half the image height and to the left of center y = half the image width (using negative coordinates). This would provide a pivot point in the center of the image.

But then, even using antialiasing to smooth out the edges causes some distortion at various angles through 2*PI radians. Trying out the Java 2D API as pbrockway2 suggested to see what works best for you is the best bet.

Regards,
Jim
Last edited by jim829; 03-29-2013 at 02:20 AM.

4. Member
Join Date
Mar 2012
Location
Sweden
Posts
88
Rep Power
0

## Re: Rotating 2d array image

Originally Posted by pbrockway2
Java has an Image class which does a better job of representing images than a general purpose 2D array.
Okay, but is there something already built in that can somehow rotate a array of numbers? Because all my graphics code is done by int arrays and it would be a pain to change everything. Can I change the pixel array to a image and then turn it back to an array when it has been rotated?

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Rotating 2d array image

What exactly does your image array look like? To rotate images about an origin (px, py) I use the following: For a given x and y. rotating counter-clockwise for positive angle in radians, the new coordinate will be x2 and y2.

double x2 = px + (x - px) * cos(angle) - (y - py) * sin(angle);
double y2 = py + (x - px) * sin(angle) + (y - py) * cos(angle);

Regards,
Jim

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Rotating 2d array image

Can I change the pixel array to a image and then turn it back to an array when it has been rotated?
That's the approach I'd follow.

I'm not exactly sure how I would deal with the rounding error that gets introduced when Jim's formulae in #5 are applied to the int indices of your array. If you use int values for the x's and y's it might well be the case that two different source pixels (x,y) and (x',y') map to the same target pixel (x2,y2). Even worse, some (x2,y2) slots in your target array might not get filled by any (x,y) source elements. (I'm not saying you *couldn't* deal with this problem or the associated one mentioned in #1 that each (x2,y2) really comes from a weighted average of four (x,y) values - just that I'm lazy and wouldn't bother.)

7. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Rotating 2d array image

Using the Java 2D capabilities would be the preferred method. I first thought you would have to convert to an image raster. But it looks like you may be able to use setRGB of the BufferedImage class for setting individual or groups of pixels.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•