package com.udacity.gamedev.drawthecantorgasket;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
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
    private final static int RECURSIONS = 5;

    @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);
        Rectangle bounds = findLargestSquare();

        // TODO: Begin a filled shapeRenderer batch
        shapeRenderer.begin(ShapeType.Filled);

        // TODO: Draw a white square matching the bounds
        shapeRenderer.setColor(Color.WHITE);
        shapeRenderer.rect(bounds.x, bounds.y, bounds.width, bounds.height);

        // TODO: Set the working color to black, and call punchCantorGasket with the bounds
        shapeRenderer.setColor(Color.BLACK);
        punchCantorGasket(bounds.x, bounds.y, bounds.width, RECURSIONS);

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

    private void punchCantorGasket(float x, float y, float size, int recursions){
        // TODO: Base case, if recursions = 0, return
        if (recursions == 0) {
            return;
        }
        // TODO: Draw a black square in the middle third
        shapeRenderer.rect(x + size / 3, y + size / 3, size / 3, size / 3);

        // TODO: Call punchCantorGasket on all the other thirds
        float newSize = size/3;

        punchCantorGasket(x, y, newSize, recursions - 1);
        punchCantorGasket(x + newSize, y, newSize, recursions - 1);
        punchCantorGasket(x + 2 * newSize, y, newSize, recursions-1);
        punchCantorGasket(x, y + newSize, newSize, recursions-1);
        punchCantorGasket(x + 2 * newSize, y + newSize, newSize, recursions - 1);
        punchCantorGasket(x, y + 2 * newSize, newSize, recursions - 1);
        punchCantorGasket(x + newSize, y + 2 * newSize, newSize, recursions-1);
        punchCantorGasket(x + 2 * newSize, y + 2 * newSize, newSize, recursions - 1);
    }

    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;
    }
}

@@ -679,41 +679,8 @@
  %7B%0A%0A
-    ShapeRenderer shapeRenderer;%0A
     
@@ -779,16 +779,49 @@
 ONS = 5;
+%0A    ShapeRenderer shapeRenderer;
 %0A%0A    @O
@@ -850,17 +850,16 @@
 d create
- 
 () %7B%0A   
@@ -943,17 +943,16 @@
 d render
- 
 () %7B%0A   
@@ -1726,16 +1726,17 @@
 ursions)
+ 
 %7B%0A      
@@ -2070,17 +2070,19 @@
 e = size
-/
+ / 
 3;%0A%0A    
@@ -2260,33 +2260,35 @@
 Size, recursions
--
+ - 
 1);%0A        punc
@@ -2328,33 +2328,35 @@
 Size, recursions
--
+ - 
 1);%0A        punc
@@ -2572,17 +2572,19 @@
 cursions
--
+ - 
 1);%0A    
@@ -2660,24 +2660,343 @@
 rsions - 1);
+%0A%0A        // Here's a more compact way to perform the recursive calls.%0A        // Thanks to GitHub user khafan! https://github.com/khafan%0A%0A//        for (int i = 0; i %3C 9; i++) %7B%0A//            if (i == 4) continue;%0A//            punchCantorGasket(i%253 * size/3 + x, i/3 * size/3 + y, size/3, recursions - 1);%0A//        %7D
 %0A    %7D%0A%0A    
@@ -3032,16 +3032,17 @@
 Square()
+ 
 %7B%0A      
@@ -3234,16 +3234,17 @@
 nHeight)
+ 
 %7B%0A      
@@ -3295,17 +3295,19 @@
 nHeight)
-/
+ / 
 2;%0A     
@@ -3539,17 +3539,19 @@
 enWidth)
-/
+ / 
 2;%0A     

Add extra Cantor Gasket approach