package com.udacity.gamedev.drawthecantorgasket;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;

/*

TODO: Start here

The Cantor gasket is a fractal where we start with a white square. We devide that square up into a 3x3 grid of smaller squares, then remove the middle square. Finally, we repeat the process on each of the remaining 8 squares.

 */

public class DrawTheCantorGasket extends ApplicationAdapter {

    ShapeRenderer shapeRenderer;
    // TODO: Set a constant for how many recursions to draw. 5 is a good place to start

    @Override
    public void create () {
        shapeRenderer = new ShapeRenderer();
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Finds a good place to draw our fractal
        // Rectangle has members x,y for the lower left corner, and width and height
        Rectangle bounds = findLargestSquare();

        // TODO: Begin a filled shapeRenderer batch

        // TODO: Draw a white square matching the bounds

        // TODO: Set the working color to black, and call punchCantorGasket with the bounds

        // TODO: End the batch
        shapeRenderer.end();
    }


    private void punchCantorGasket(float x, float y, float size, int recursions){
        // TODO: Base case, if recursions = 0, return

        // TODO: Draw a black square in the middle square

        // TODO: Call punchCantorGasket on all 8 other squares

    }

    private Rectangle findLargestSquare(){
        Rectangle largestSquare = new Rectangle();
        float screenWidth = Gdx.graphics.getWidth();
        float screenHeight = Gdx.graphics.getHeight();

        if (screenWidth > screenHeight){
            largestSquare.x = (screenWidth - screenHeight)/2;
            largestSquare.y = 0;
            largestSquare.width = screenHeight;
            largestSquare.height = screenHeight;
        } else {
            largestSquare.x = 0;
            largestSquare.y = (screenHeight - screenWidth)/2;
            largestSquare.width = screenWidth;
            largestSquare.height = screenWidth;
        }
        return largestSquare;
    }
}

@@ -252,16 +252,40 @@
 angle;%0A%0A
+import java.awt.Color;%0A%0A
 /*%0A%0ATODO
@@ -713,16 +713,44 @@
 to start
+%0A    final int recursions=5;
 %0A%0A    @O
@@ -1211,486 +1211,1575 @@
 tch%0A
-%0A        // TODO: Draw a white square matching the bounds%0A%0A        // TODO: Set the working color to black, and call punchCantorGasket with the bounds%0A%0A        // TODO: End the batch%0A        shapeRenderer.end();%0A    %7D%0A%0A%0A    private void punchCantorGasket(float x, float y, float size, int recursions)%7B%0A        // TODO: Base case, if recursions = 0, return%0A%0A        // TODO: Draw a black square in the middle square%0A%0A        // TODO: Call punchCantorGasket on all 8 other squares
+        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);%0A%0A        // TODO: Draw a white square matching the bounds%0A        shapeRenderer.setColor(com.badlogic.gdx.graphics.Color.WHITE);%0A        shapeRenderer.rect(bounds.x,bounds.y,bounds.height,bounds.width);%0A%0A        // TODO: Set the working color to black, and call punchCantorGasket with the bounds%0A        shapeRenderer.setColor(com.badlogic.gdx.graphics.Color.BLACK);%0A        punchCantorGasket(bounds.x,bounds.y,bounds.height,recursions);%0A%0A        // TODO: End the batch%0A        shapeRenderer.end();%0A    %7D%0A%0A%0A    private void punchCantorGasket(float x, float y, float size, int recursions)%7B%0A        // TODO: Base case, if recursions = 0, return%0A        if(recursions==0)%0A        %7B%0A            return;%0A        %7D%0A%0A        // TODO: Draw a black square in the middle square%0A        shapeRenderer.rect(x+size/3,y+size/3,size/3,size/3);%0A%0A        // TODO: Call punchCantorGasket on all 8 other squares%0A        float newSize = size/3;%0A        punchCantorGasket(x, y, newSize, recursions - 1);%0A        punchCantorGasket(x + newSize, y, newSize, recursions - 1);%0A        punchCantorGasket(x + 2 * newSize, y, newSize, recursions-1);%0A        punchCantorGasket(x, y + newSize, newSize, recursions-1);%0A        punchCantorGasket(x + 2 * newSize, y + newSize, newSize, recursions - 1);%0A        punchCantorGasket(x, y + 2 * newSize, newSize, recursions - 1);%0A        punchCantorGasket(x + newSize, y + 2 * newSize, newSize, recursions-1);%0A        punchCantorGasket(x + 2 * newSize, y + 2 * newSize, newSize, recursions -1);%0A
 %0A%0A  

resolve the cantor gasket excercise