
// BulgingSquares.java
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

/* An optical illusion

   Usage:
     > compile BulgingSquares.java
     > run BulgingSquares
*/


import java.awt.*;

public class BulgingSquares
{

  public static void main(String[] args)
  {
    int n = 15; 
    StdDraw.setXscale(0, n); 
    StdDraw.setYscale(0, n); 

    for (int i = 0;  i < n;  i++) 
      for (int j = 0; j < n; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.BLACK);
        else 
          StdDraw.setPenColor(StdDraw.RED);
        StdDraw.filledSquare(i + 0.5, j + 0.5, 0.5);
      }

    for (int i = (n/2); i < n; i++) 
      for (int j = n/2; j < n; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.RED);
        else 
          StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledSquare(i + 0.75, j + 0.25, 0.1);
      }

    for (int i = (n/2); i < n; i++) 
      for (int j = n/2; j < n; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.RED);
        else 
           StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledSquare(i + 0.25, j + 0.75, 0.1);
      }

    for (int i = 0; i < n/2; i++) 
       for (int j = 0; j < n/2; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.RED);
        else 
          StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledSquare(i + 0.25, j + 0.75, 0.1);
      }

    for (int i = 0; i < n/2; i++) 
       for (int j = 0; j < n/2; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.RED);
        else 
          StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledSquare(i + 0.75, j + 0.25, 0.1);
      }

    for (int i = n/2; i < n; i++) 
       for (int j = 0; j < n/2; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.RED);
        else 
          StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledSquare(i + 0.75, j + 0.75, 0.1);
      }

    for (int i = n/2; i < n; i++) 
      for (int j = 0; j < n/2; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.RED);
        else 
          StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledSquare(i + 0.25, j + 0.25, 0.1);
      }

    for (int i = 0; i < n/2; i++) 
      for (int j = n/2; j < n; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.RED);
        else 
          StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledSquare(i + 0.75, j + 0.75, 0.1);
      }

    for (int i = 0; i < n/2; i++) 
      for (int j = n/2; j < n; j++) {
        if ((i + j) % 2 != 0) 
          StdDraw.setPenColor(StdDraw.RED);
        else 
          StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledSquare(i + 0.25, j + 0.25, 0.1);
      }
  } /* main */


}  // end of BulgingSquares class
