import static util.SVGUtil.*;

public class Ehagaki {
    private final static int RECT_WIDTH  = 10;
    private final static int RECT_HEIGHT = 10;
    private final static int SPACE       =  8;
    private final static int X_OFFSET    = 15;
    private final static int Y_OFFSET    = 30;

    /*
      main
    */
    public static void main(String[] args) {
        double color;
        double x;
        double y;
        double w;
        double h;
        double s;
        double ox;
        double oy;
        int i;

        start();

        x = X_OFFSET;
        y = Y_OFFSET;
        ox = X_OFFSET;
        oy = Y_OFFSET;
        for(i = 0, color = 0, s = 100, w = RECT_WIDTH, h = RECT_HEIGHT; i < 10; ++i) {
            color = draw_rects(x, y, color, w, h, s, ox, oy);
            x = randomInRange(X_OFFSET, pageWidth() - X_OFFSET - 20);
            ox = x;
            y = randomInRange(Y_OFFSET, pageHeight() - Y_OFFSET - 20);
            oy = y;
            s *= 0.8;
            w *= 0.9;
            h *= 0.9;
        }
        fill(rgb255(40, 80, 40));
        textFont("Verdana", 6);
        text("Kagawa University", 0, 8);
        text("OpenCampus", pageWidth() - 60, pageHeight() - 8);

        finish();
        return;
    }

    private static double draw_rect(double color, final double x, final double y, final double w, final double h, final double s) {
        if(color * 5 > 100) color = 0;
        fill(hsb100(color * 5, s, 100));
        rect(x, y, w, h);
        return color;
    }

    private static double draw_rects(double x, double y, double color,
                                     final double w, final double h, final double s, final double x_offset, final double y_offset) {

        for(; y + h < pageHeight() - h - y_offset; y += h + SPACE, ++color) {
            color = draw_rect(color, x, y, w, h, s);
        }
        for(; x + w < pageWidth() - w - x_offset; x += w + SPACE, ++color) {
            color = draw_rect(color, x, y, w, h, s);
        }
        for(; y >= y_offset + h; y -= (h + SPACE), ++color) {
            color = draw_rect(color, x, y, w, h, s);
        }
        for(; x >= x_offset + w; x -= (w + SPACE), ++color) {
            color = draw_rect(color, x, y, w, h, s);
        }

        return color;
    }
}
