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

• 11-11-2012, 09:26 AM
Track13
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()?
• 11-11-2012, 09:32 AM
JosAH
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
• 11-11-2012, 09:49 AM
Track13
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...
• 11-11-2012, 10:25 AM
JosAH
Re: Help to rotate a pic 180 degrees (upside down)
Well, compare my first transformation with your transformation ...

kind regards,

Jos
• 11-13-2012, 03:45 AM
Track13
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.
• 11-13-2012, 08:53 AM
JosAH
Re: Help to rotate a pic 180 degrees (upside down)
Quote:

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:

Code:

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

Jos
• 11-16-2012, 04:02 AM
Track13
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);
• 11-16-2012, 05:27 AM