Results 1 to 7 of 7
  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default 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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default 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 01:20 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Rotating 2d array image

    Quote Originally Posted by pbrockway2 View Post
    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. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Rotating image with an Applet
    By meesterpickles in forum Java 2D
    Replies: 1
    Last Post: 03-11-2012, 05:22 AM
  2. Help with Image Rotating?
    By Leospaceman in forum Java 2D
    Replies: 2
    Last Post: 05-31-2011, 02:48 PM
  3. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 AM
  4. Rotating an image
    By lackofcolor in forum Java 2D
    Replies: 3
    Last Post: 02-27-2009, 11:54 PM
  5. Rotating Image?
    By sciguy77 in forum Java Applets
    Replies: 9
    Last Post: 02-17-2009, 01:47 AM

Posting Permissions

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