Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm exam 1 review Fall2006: Flip the other way

Back to Midterm Exam 1 Review Fall2006

Questions? Answers? Comments?


//Correct Code at the Bottom of the page



//Almost there, but not quite yet. The code here I believe flips the picture upside down instead of reflecting it across the horizontal axis.

/Error: this should not happen
Method to mirror an image along the horizontal axis/
public Picture flipUp()
{

Pixel currPixel;
Picture target = new Picture(this.getWidth(),this.getHeight());

//This Loop copies directly the top half onto the target picture
for (int srcx = 0, trgx = 0; srcx < getWidth(); srcx++, trgx++)
{
for (int srcy = 0, trgy = 0; srcy < getHeight()/2; srcy++, trgy++)
{
// get the current pixel
currPixel = this.getPixel(srcx,srcy);

/ copy the color of currPixel into target
/
target.getPixel(trgx,trgy).setColor(currPixel.getColor());
}
};


//This Loop creates the bottom half horizontally mirrored to the top half
for (int srcx = 0, trgx = 0; srcx < getWidth(); srcx++, trgx++)
{
for (int srcy = getHeight()/2, trgy = getHeight()/2; srcy > 0; srcy–, trgy++)
{
// get the current pixel
currPixel = this.getPixel(srcx,srcy);

/ copy the color of currPixel into target
/
target.getPixel(trgx,trgy).setColor(currPixel.getColor());
}
};
return target;
}
//REMOVED Cormack

Ummm, correct me again if i am wrong, but i think the code doesnt do what it is supposed to do? i think there is some logic mistake here. Or maybe i just cant understand it, i mean the second part, where it will flip the mirror, it seems a lil bit ambiguous.
i make a different version here, prof, could u check and comment on it? it will be greatly appreciated. Here it is:


public Picture abc()
{
Pixel abc;
Picture target (this.getWidth(), this.getHeight());

for (int y = 0, y this.getHeight()/2, y++)
{
for (x=0, x this.getWidth(), x++)
{
abc = this.getPixel();
target.getPixel(x,y).setColor(abc.getColor());
target.getPixel(x,this.getheight+1-y).setColor(abc.getColor());

}
}
}


//Chris Ang

The code you just put up has a lot of errors. in Java, you need to use ; instead of , in for statements.

Try putting my code into your Picture class, then opening myPicture and replacing flip w/ flipUp.

You will see in the collage that the code draws the top half of the picture normally, and then it mirrors the top half of the picture onto the bottom half, i.e. mirroring the image over the horizontal axis. If it's supposed to mirror the entire picture, the only difference is that you would make the target picture twice as tall, then set the code to copy the entire picture, then copy it again, but starting at the bottom of the picture and working your way up the srcx/srcy while moving down the trgx/trgy placekeepers.

REMOVED

Also you can put your code in <code></code> tags to keep the formatting (so comments don't messup on the coweb) - Joel

but on the second part of your code, you start it from half way thru, dont we have to start it from the pixel (1,1)?
Chris A

(0,0) is the starting position. If you start it from (0,0) then it will write over the code where it copies the top half of the picture. I ran the code on a picture to make sure it works, but I'm not sure how to post a picture on coweb otherwise I'd post it so I could show better what this looks like.

REMOVED

Okay this code right here takes the original image, makes the target picture double the height of the original, and copies the original picture onto the top half of the target image. It then starts at that point and copies the mirror image to the bottom half of the target image (note the srcy = getHeight()-1 because if it starts at the height then technically it's already out of bounds).

Run it through MyPicture.java. just change swan.flip() to swan.horizontalMirror() and comment all the rest of the mumbo jumbo that's added to the end of it.

REMOVED Cormack

    /**
   * Method to mirror an image along the horizontal axis
   **/
  public Picture horizontalMirror()
  {
    
    Pixel currPixel;
    Picture target = new Picture(this.getWidth(),this.getHeight()*2);
   
    //This Loop copies directly the top half onto the tartet picture
    for (int srcx = 0, trgx = 0; srcx < getWidth(); srcx++, trgx++)
    {
      for (int srcy = 0, trgy = 0; srcy < getHeight(); srcy++, trgy++)
      {
        // get the current pixel 
        currPixel = this.getPixel(srcx,srcy);
        
        /* copy the color of currPixel into target
         */
        target.getPixel(trgx,trgy).setColor(currPixel.getColor());
      }
    };
    
    
    //This Loop creates the bottom half horizontally mirrored to the top half
    for (int srcx = 0, trgx = 0; srcx < getWidth(); srcx++, trgx++)
    {
      for (int srcy = getHeight()-1, trgy = getHeight(); srcy > 0; srcy--, trgy++)
      {
        // get the current pixel 
        currPixel = this.getPixel(srcx,srcy);
        
        /* copy the color of currPixel into target
         */
        target.getPixel(trgx,trgy).setColor(currPixel.getColor());
      }
    };
    return target;
  }


hey, that is not what i thought the question ask. Anyway, this is what my interpretation of the question is:
Free Image Hosting at www.ImageShack.us
Sorry for my bad code just now. I never compile it, so there is a lot of mistakes and regarding to the punctuation, i use C++ a lot, so forgive me! ;) here is my fixed code, which really works
public Picture test1()
{
	Pixel abc;
	//the reason i put +2 in the height part is to compensate the offset, if i put +1 or nothing, it wont work. 
	Picture target = new Picture(this.getWidth(), this.getHeight()+2);
 
	for (int y = 0; y < this.getHeight()/2 + 1; y++)
	{
		for (int x=0; x < this.getWidth(); x++)
		{
			abc = this.getPixel(x,y);
			target.getPixel(x,y).setColor(abc.getColor());
			target.getPixel(x,(this.getHeight() + 1 - y)).setColor(abc.getColor());
		}
	}
	return target;
}


What i really do is to copy one pixel and put it in two point, maybe you can also implement it in your code(doesnt matter if you are flipping it down), so u only need two loops instead of four.

//Chris Ang

I see where you're coming from now. I guess what we're not deciding on now is aesthetics and general coding preference. Looks to me like both of ours worked :).

Good luck on the test today.

REMOVED



Uploaded Image: swanmirror.JPG

Link to this Page