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

Midterm exam 1 review Sp2006: Flip the other way

Questions, comments, concerns, answers, concerns about answers?
(Back to Midterm Exam 1 Review Sp2006)


Apparently, I did this the complete opposite way as out TA, but I'm pretty sure this is it:
  /**
   *Method to flip an image Top-to-bottom**/
  public Picture flip() {
    
    Pixel currPixel;
    Picture target = new Picture(this.getWidth(),this.getHeight());
    
    for (int srcx = 0, trgx = 0; srcx < getWidth(); srcx++, trgx++)
    {
      for (int srcy = 0, trgy = getHeight()-1; srcy < (int)(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());
      }
    };
    return target;
  }

Hmm. That's actually kinda hard to follow...
EDIT: And I think it's wrong! Thanks for pointing this out somebody!
I've created a code below that does what they wanted (not really a flip at all!).

I'm not exactly sure what this question is asking for. Are we flipping the picture upside down, or mirroring the top half to the bottom half?

I read it as a mirror across the horizontal axis. It says we see a top on the top and a top on the bottom, but flipped.
So that's the procedure I went for (mirror top half to bottom half).

I think mirroring on a blank canvas would require two sets of for loops. One to copy the top half of the picture and one to flip the top half and copy it to the bottom. I think mirroring would be easier in a void method if we weren't copying the pixels onto a blank canvas like on the example.

Wow. Whoever pointed that out, you're dead on. My mistake! The example is a complete flip, but they ask us to perform a mirror!

  /**
   *Method to flip an image Top-to-bottom**/
  // The name is deceiving, it actually mirrors.
  public Picture flip() {
    
    Pixel currPixel;
    Picture target = new Picture(this.getWidth(),this.getHeight());
    
    //Put the top on the bottom of the new canvas, flipped.
    for (int srcx = 0, trgx = 0; srcx < getWidth(); srcx++, trgx++)
    {
      for (int srcy = 0, trgy = getHeight()-1; srcy < (int)(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());
      }
    };
    
    //Now, put the top on the top.
    for (int srcx = 0, trgx = 0; srcx < getWidth(); srcx++, trgx++)
    {
      for (int srcy = 0, trgy = 0; srcy <= (int)(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());
      }
    };

    return target;
  }

Thanks, somebody, for helping me out there!!!

To make it simpler, you could combine the two for loops like so:
for (int srcx = 0, trgx = 0; srcx < getWidth(); srcx++, trgx++)
    {
      for (int srcy = 0, trgy = getHeight()-1; srcy < (int) (getHeight()/2); srcy++, trgy--)
      {
        // get the current pixel 
        currPixel = this.getPixel(srcx,srcy);
        
        /*copy the color of currPixel into bottom half of target*/
        target.getPixel(trgx,trgy).setColor(currPixel.getColor());
        
        /*copy the color of currPixel into top half of target*/
        target.getPixel(srcx,srcy).setColor(currPixel.getColor());
      }
    };



Awesome–Now that's a nice clean solution!

I'm confused. What does each for loop do? And how did the bottom half end up looking like the top half AND flipped?

If you're confused, trace it through. Draw a picture with only four pixels, then trace it through to see where the pixel colors' go. Mark Guzdial



Link to this Page