0

我已经有一种方法可以将图片旋转 90 度,但是我需要编写一个单独的方法来将其旋转 180 度,然后再编写一个方法来旋转 270 度。我以为我可以通过重复 90 度来做到这一点,但我是新手,我真的不知道。

我的 90 度代码:

    public Picture rotateRight90()
  {
   Picture rotated = new Picture (getHeight(), getWidth());

   for (int x = 0, roty = 0; x < getWidth(); x++, roty++)
   {
    for ( int y = 0, rotx = getHeight() - 1; y < getHeight(); y++, rotx--)
    {
       Pixel orig = getPixel(x,y);
       Pixel rotPix = rotated.getPixel (rotx, roty);
       rotPix.setColor(orig.getColor());
    }
   }
   return rotated;
  }

我尝试旋转 180 代码:

     public Picture rotate180()
 {
   Picture rotated = new Picture (getHeight(), getWidth());

   for (int z = 0; z < 2; z++)
   {
     for (int x = 0, roty = 0; x < getWidth(); x++, roty++)
   {
    for ( int y = 0, rotx = getHeight() - 1; y < getHeight(); y++, rotx--)
    {
       Pixel orig = getPixel(x,y);
       Pixel rotPix = rotated.getPixel (rotx, roty);
       rotPix.setColor(orig.getColor());
    }
   }
   }
   return rotated;
  }
4

1 回答 1

0

这个是180的

  public Picture rotateRight180()
{
  Picture rotated = new Picture (getWidth(), getHeight());
 
  for (int x = 0, rotx = getWidth() - 1; x < getWidth(); x++, rotx--)
  {
    for (int y = 0, roty = getHeight() - 1; y < getHeight (); y++, roty--)
   {
     
      Pixel orig = getPixel (x,y);
      Pixel rotPix = rotated.getPixel (rotx, roty);
      rotPix.setColor(orig.getColor());
   
   }
  }
  return rotated;
 }
于 2021-11-23T21:30:14.480 回答