import java.io.*;
import java.util.Random;
import java.util.StringTokenizer;



/**
 * Created with IntelliJ IDEA.
 * User: Pavel Kunyavskiy
 * Date: 19/10/13
 * Time: 21:25
 */
public class SpiralGenerator implements PolygonGenerator {

    public void generate(int[] x, int[] y, int maxCx, int maxCy, Random rnd) {
        int N = x.length;
        int rounds = rnd.nextInt(N/10)+2;
        int n = x.length;
        assert x.length == y.length;

        double fullRadius = maxCx / (rounds + 2.0);
        double start = fullRadius * 0.5;
        double shift = fullRadius * 0.25;
        double radiusDelta = (fullRadius * rounds) / N;
        double angleDelta = (2.0 * Math.PI * rounds * 2.0) / N;

        int idShift = rnd.nextInt(n);
        int ptr = 0;

        for (int i = 0; i < n/2; i++){
            x[(ptr + idShift) % n] = (int)((start + radiusDelta * i) * Math.cos(angleDelta * i));
            y[(ptr + idShift) % n] = (int)((start + radiusDelta * i) * Math.sin(angleDelta * i));
            ptr++;
        }
        for (int i = n/2-1; i >= 0; i--){
            x[(ptr + idShift) % n] = (int)((start + radiusDelta * i + shift) * Math.cos(angleDelta * i));
            y[(ptr + idShift) % n] = (int)((start + radiusDelta * i + shift) * Math.sin(angleDelta * i));
            ptr++;
        }

        CrossReverseGenerator.fixSegmentIntersection(x, y);
    }

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

    public String Name(){
        return "Two ways spiral";
    }
}
