# Thread: Help to rotate a pic 180 degrees (upside down)

1. Member
Join Date
Nov 2012
Posts
11
Rep Power
0

## 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. ## 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

3. Member
Join Date
Nov 2012
Posts
11
Rep Power
0

## 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. ## Re: Help to rotate a pic 180 degrees (upside down)

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

kind regards,

Jos

5. Member
Join Date
Nov 2012
Posts
11
Rep Power
0

## 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. ## Re: Help to rotate a pic 180 degrees (upside down)

Originally Posted by Track13
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

7. Member
Join Date
Nov 2012
Posts
11
Rep Power
0

## 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. ## Re: Help to rotate a pic 180 degrees (upside down)

Code tags!

#### Posting Permissions

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