Results 1 to 8 of 8
  1. #1
    Track13 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Help to rotate a pic 180 degrees (upside down)

    I just need help on how to rotate a picture 180 degrees.

    public void copyRotation(Picture pict)
    {
    Pixel sourcePixel = null;
    Pixel targetPixel = null;

    for(int sourceX =0; sourceX < pict.getWidth(); sourceX++)
    {
    for(int sourceY =0; sourceY < pict.getHeight(); sourceY++)
    {
    sourcePixel = getPixel(sourceX, sourceY);
    targetPixel = this.getPixel(sourceY, getHeight() - 1 - sourceX);
    targetPixel.setColor(sourcePixel.getColor());
    }
    }
    } <--- My method prints the pic out to the left (90 degrees)
    Do I have to do something towards the getWidth() or getHeight()?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,517
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help to rotate a pic 180 degrees (upside down)

    Let w and h be the width and height of the picture; the following transformation rotates it over 180 degrees:

    (x', y') = (w-1-x, h-1-y)

    If you want to flip (mirror) it over an horizontal axis, the transformation is:

    (x', y') = (x, h-1-y)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Track13 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Re: Help to rotate a pic 180 degrees (upside down)

    I don't want to mirror the picture, just simply have the picture upside down.
    I added in the equation and my picture came out all jumble...

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,517
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help to rotate a pic 180 degrees (upside down)

    Well, compare my first transformation with your transformation ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Track13 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Re: Help to rotate a pic 180 degrees (upside down)

    public void copyRotation(Picture pict)
    {
    Pixel sourcePixel = null;
    Pixel targetPixel = null;

    for(int sourceX =0; sourceX < pict.getWidth(); sourceX++)
    {
    for(int sourceY =0; sourceY < pict.getHeight(); sourceY++)
    {
    sourcePixel = getPixel(sourceX, sourceY);
    targetPixel = this.getPixel(getHeight() - 1 - sourceY, getWidth() - 1 - sourceX);
    targetPixel.setColor(sourcePixel.getColor());
    }
    }
    }

    My picture is still coming out jumble.
    I compared my transformation w/ your transformation but I am not understanding why the picture is
    coming out crazy.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,517
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help to rotate a pic 180 degrees (upside down)

    Quote Originally Posted by Track13 View Post
    My picture is still coming out jumble.
    I compared my transformation w/ your transformation but I am not understanding why the picture is
    coming out crazy.
    Shouldn't those two lines be:

    Java Code:
            sourcePixel = getPixel(sourceX, sourceY);
            targetPixel = this.getPixel(pict.getWidth() - 1 - sourceX, pict.getHeight() - 1 - sourceY);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Track13 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Re: Help to rotate a pic 180 degrees (upside down)

    Thank you ! My rotation works but now my rotated pic is mirrored vertically. I know its because of my method mirrorPic but how is that possible if mirrorPic is its on method alone an i called it once for Picture flower2?

    public void mirrorPic()
    {
    int width = this.getWidth();
    int mirrorPoint = width / 2;
    Pixel leftPixel = null;
    Pixel rightPixel = null;

    for(int y =0; y < getHeight(); y++)
    {
    for(int x =0; x < mirrorPoint; x++)
    {
    leftPixel = getPixel(x, y);
    rightPixel = this.getPixel(width - 1 - x,y);
    rightPixel.setColor(leftPixel.getColor());
    }
    }
    }

    public static Picture Rotation(Picture pict)
    {
    Pixel sourcePixel = null;
    Pixel targetPixel = null;
    Picture rotate = new Picture(pict.getHeight(), pict.getWidth());

    for(int sourceX =0; sourceX < pict.getWidth(); sourceX++)
    {
    for(int sourceY =0; sourceY < pict.getHeight(); sourceY++)
    {
    sourcePixel = pict.getPixel(sourceX, sourceY);
    targetPixel = rotate.getPixel(sourceY, pict.getWidth() - 1 - sourceX);
    targetPixel.setColor(sourcePixel.getColor());
    }
    }
    return rotate;
    }

    This is in the main method ---> Picture flower2 = flower;
    flower2.mirrorPic();
    background.copyPic(flower2, 120, 120);

    Picture flower3 = flower;
    flower3 = Rotation(flower3);
    Picture flower4;
    flower4 = Rotation(flower3);
    background.copyPic(flower4, 230, 230);

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Help to rotate a pic 180 degrees (upside down)

    Code tags!

Similar Threads

  1. Math.atan --- To Degrees Help
    By GregoryNeal in forum New To Java
    Replies: 1
    Last Post: 03-07-2012, 06:54 AM
  2. Upside Down Triangle
    By Interista in forum New To Java
    Replies: 13
    Last Post: 12-02-2011, 04:09 PM
  3. How to Make Upside Down Triangle Filled In?
    By Settz in forum New To Java
    Replies: 7
    Last Post: 11-21-2011, 04:42 AM
  4. Upside down Triangle pattern
    By JayP in forum New To Java
    Replies: 9
    Last Post: 06-06-2011, 05:46 AM
  5. BufferedImage flips the chart upside down
    By sg3232 in forum Java 2D
    Replies: 0
    Last Post: 02-10-2011, 08:46 PM

Posting Permissions

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