简体   繁体   中英

How to mirror an image horizontally?

I am a high school student. I want to mirror an image horizontally. I have figured out the method to mirror it vertically but I have no idea how to start this task.

I copy and pasted the code from the previous method and was looking for something to edit but have had no luck so far.

public void mirrorHorizontal()
  {
      Pixel[][] pixels = this.getPixels2D();
      Pixel leftPixel = null;
      Pixel rightPixel = null;
      int width = pixels[0].length;
      for (int row = 0; row < pixels.length; row++)
      {
         for (int col = 0; col < width / 2; col++)
         {
            leftPixel = pixels[row][col];
            rightPixel = pixels[row][width - 1 - col];
            leftPixel.setColor(rightPixel.getColor());
         }
      }
  }

How can I change this code to mirror the image horizontally?

Does this do it?

public void mirrorHorizontal()
      {
          Pixel[][] pixels = this.getPixels2D();
          Pixel topPixel = null;
          Pixel bottomPixel = null;

      int height = pixels.length;
      int width = pixels[0].length;

      for (int row = 0; row < height / 2; row++)
      {
         for (int col = 0; col < width; col++)
         {
            topPixel = pixels[row][col];
            bottomPixel = pixels[height - 1 - row][width];
            topPixel.setColor(bottomPixel.getColor());
         }
      }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM