
// ColorRug.java   (was called PersianRig.java)
// eschwartzman
// Robert Sedgewick and Kevin Wayne
// Modified by Andrew Davison, Nov. 2022

// https://github.com/eschwartzman/Art/blob/master/PersianRug.java

/* Draw a rug

   Usage:
     > compile ColorRug.java
     > run ColorRug
*/

import java.awt.Color;
import java.util.*;


public class ColorRug
{
  private static final Color[] palette = {
    StdDraw.BLUE, StdDraw.CYAN, StdDraw.PRINCETON_ORANGE,
    StdDraw.BOOK_LIGHT_BLUE, StdDraw.GREEN, StdDraw.LIGHT_GRAY, 
    StdDraw.MAGENTA, StdDraw.ORANGE, StdDraw.PINK,
    StdDraw.RED, StdDraw.WHITE, StdDraw.YELLOW
  };


  public static void main(String args[])
  {
    StdDraw.setPenColor(palette[0]);
    StdDraw.line(0, 0, 1, 0);
    StdDraw.line(1, 0, 1, 1);
    StdDraw.line(1, 1, 0, 1);
    StdDraw.line(0, 1, 0, 0);

    // Lower left = (0,0); Size = 1; color indicies
    int[] cols = randInts(4, palette.length);
    colorRug(0.0, 0.0, 1, cols[0], cols[1], cols[2], cols[3]);
    System.out.println("Drawing finished");
  }


  private static int randPal()
  {  return (int)(Math.random()*palette.length);  }


  private static int[] randInts(int n, int max)
  // generate n different integers between 0 and max
  {  
    if (n > max) {
      System.out.println("Maximum too small, setting it to n");
      max = n;
    }

    ArrayList<Integer> digits = new ArrayList<>();
    for (int i=0; i < max; i++)
       digits.add(i);
    Collections.shuffle(digits);

    int[] cols = new int[n];
    for(int i=0; i < n; i++)
      cols[i] = digits.get(i);
    return cols;
  }  // end of randInts()


  private static int randInt(int min, int max)
  {  return (int)(Math.random()*((max-min)))+min;  }



  private static void colorRug(double x, double y, double size, 
                   int north, int east, int south, int west)
  /* (x,y): lower left coordinate of square
     size: width (and height) of square
     north: color index of the north side of square
     east: color index of the east side of square
     south: color index of the south side of square
     west: color index of the west side of square
  */
  {
    if (size < 0.0035) // base case
      return;

    /* random formula here--if you change them, you end 
       up with another color design
    */
    int colorIdx = (north + east + south + west)/4;
    int xIdx = ((east+west)/2 + randInt(-1,1))% palette.length;
    int yIdx = ((north+south)/2 + randInt(1,-1))% palette.length;

    StdDraw.setPenColor(palette[colorIdx]);
    StdDraw.line(x+size/2, y, x+size/2, y+size);
    StdDraw.line(x, y+size/2, x+size, y+size/2);

    colorRug(x, y+size/2, size/2, 
                       north, xIdx, yIdx, west); // top left
    colorRug(x+size/2, y+size/2, size/2, 
                       north, east, yIdx, xIdx); // top right
    colorRug(x, y, size/2, 
                       yIdx, xIdx, south, west); // bottom left
    colorRug(x+size/2, y, size/2, 
                       yIdx, east, south, xIdx); // bottom right
  } // end of colorRug()


} // end of ColorRug class

