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

PreQuiz 1-Fall 2005

Below is the code to scale an image to an input size. For example, if I wanted to scale the picture myPicture down by 1/2 in each dimension, I would execute myPicture.scale(0.5). Notice that scale() does return a copy of the original picture.

/**
   * Method to scale the picture by a factor, and return the result
   * @param scale factor to scale by (1.0 stays the same, 0.5 decreases each side by 0.5, 2.0 doubles each side)
   * @return the scaled picture
   */
  public Picture scale(double factor)
  {
    Pixel sourcePixel, targetPixel;
    Picture canvas = new Picture((int) (factor*this.getWidth())+1,
                                 (int) (factor*this.getHeight())+1);
    // loop through the columns
    for (double sourceX = 0, targetX=0;
         sourceX < this.getWidth();
         sourceX+=(1/factor), targetX++)
    {
      // loop through the rows
      for (double sourceY=0, targetY=0;
           sourceY < this.getHeight();
           sourceY+=(1/factor), targetY++)
      {
        sourcePixel = this.getPixel((int) sourceX,(int) sourceY);
        targetPixel = canvas.getPixel((int) targetX, (int) targetY);
        targetPixel.setColor(sourcePixel.getColor());
      }
    }
    return canvas;
  }


Write the method topQuarter() which returns a new picture that is part of the original. It returns a new picture that is only the upper left 1/4 of the picture. So myPicture.topQuarter() would return a new Picture that is a piece of myPicture. If the original myPicture were 640 pixels across and 480 pixels top to bottom, the returned picture would go from (0,0) to (320,240).

Comments? Questions?



hmmmmmmmm...

That's absolutely right! Good job! :-) Mark Guzdial


 public Picture topQuarter()
{
 Pixel sourcePixel, targetPixel;
 Picture canvas = new Picture((int)(this.getWidth())+1,(int)(this.getHeight())+1);
 
 for( int sourceX=0,targetX=0; sourceX < this.getWidth()/2; sourceX++, targetX++)
 {
  for( int sourceY=0,targetY=0; sourceY < this.getHeight()/2; sourceY++, targetY++)
  {
   sourcePixel = this.getPixel((int)sourceX, (int) sourceY);
   targetPixel = canvas.getPixel((int) targetX, (int) targetY);
   targetPixel.setColor(sourcePixel.getColor());
   }
  }
 return canvas;
}


yes? Removed at KS request
Nice! Mark Guzdial

did that code work? I've tried several different revisions, but that code gave me an error that said, "Picture, filename null height 481 width 641". I don't know how to make it work.
Student166
Oh, I don't really care about one-off. Mark Guzdial

Yeah, I tried it with several different pictures and it gave me 1/4 of the picture every time. are you using a normal picture or a picture that is blank, i don't know if that would make a difference. i dont know, try using a different picture maybe because like i said, it worked several times for me. Removed at KS request

The only thing is, don't we want the canvas, or the new picture, to only be the 1/4 size (ie not be a screen the size of the original picture with just picture in the top corner)? I did basically the same thing as you, but my third line reads:
    Picture canvas = new Picture((int) (this.getWidth()/2)+1,(int) (this.getHeight()/2)+1);

Am I correct in thinking that, or does it not matter?
Summer REMOVED
That does matter – good thinking, Summer! Mark Guzdial

I don't think you need the +1 when you half the height and width only because size wouldn't end up being 0 ever.

Eli

When I put in the pre-quiz method, it doesn't actually return a picture. It just tells me "Picture, filename c:/CS 1316/mediasources/dog-blue2.jpg height 84 width 104" or something like that depending on the picture. So, essientially its just returning the demensions of the picture times two. When I try Krista's code, it returns the demesions plus one. Here's what Perry and I worked out.

 public Picture topQuarterTwo()
 {
   Pixel sourcePixel, targetPixel;
   Picture canvas = new Picture((int)(this.getWidth()/2)+1, (int)(this.getHeight()/2)+1);
   
   for (double sourceX = 0, targetX=0; sourceX< this.getWidth()/2; sourceX = targetX++)
   {
     for (double sourceY = 0, targetY=0; sourceY< this.getHeight()/2; sourceY = targetY++)
     {
       sourcePixel=this.getPixel((int)sourceX,(int)sourceY);
       targetPixel=canvas.getPixel((int)targetX, (int)targetY);
       targetPixel.setColor(sourcePixel.getColor());
      }
    }
  return canvas;
 }

This returns the demsions halved, which would be the top 1/4 of the picture, so I hope that's what was wanted. Otherwise, I'm very very lost.
Student159
Student160
Excellent! Mark Guzdial

i think you guys are right and i misunderstood what we were supposed to do. I really think we are supposed to return just 1/4 of the picture without the white canvas. good luck on the quiz tomorrow.
Removed at KS request

We're all probably making it seem harder than it actually is. Student159



Link to this Page