import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: Pavel Kunyavskiy
 * Date: 19/10/13
 * Time: 20:47
 */
public class KochGenerator implements PolygonGenerator {

    final int[] dx = {1, 1, 0, -1, -1, -1, 0, 1};
    final int[] dy = {0, 1, 1, 1, 0, -1, -1, -1};

    int ptr;
    int n;

    void gen(int[] x,int[] y, int level,int dir){
         if (ptr == n) return;
         if (level == 1) {
             x[ptr] = x[ptr-1] + dx[dir] * 100;
             y[ptr] = y[ptr-1] + dy[dir] * 100;
             ptr++;
             return;
         }
        gen(x, y, level-1, dir);
        gen(x, y, level-1, (dir + 1) % 8);
        gen(x, y, level-1, (dir + 7) % 8);
        gen(x, y, level-1, dir);
    }

    public void generate(int[] x, int[] y, int maxCx, int maxCy, Random rnd) {
        n = x.length;
        assert x.length == y.length;
        ptr = 1;
        x[0] = y[0] = 0;
        gen(x, y, 30, 0);
        y[ptr-1] *= 2; // this one for avoid intersections
        CrossReverseGenerator.fixSegmentIntersection(x, y);
    }

    public String Name(){
        return "Koch snowflake";
    }

    public String shortName() {
        return "koch";
    }

}
