package sample.svg.contributed;
import static util.SVGUtil.*;

public class Nagae1 {
    private static final double CENTER_X = 148.5;
    private static final double CENTER_Y = 105.0;

    // 中心座標 cx cy
    // 何倍で表示するか r
    // sinの係数 sc
    // cosの係数 cc
    // sinのθの係数 stc
    // cosのθの係数 ctc
    // 荒さ(255の約数であることを推奨) d
    public static void polar(double cx, double cy, double r, double sc, double cc, int stc, int ctc, int d) {
        int i;
        int unit = (0xFF / d) + 1;
        int len = 6*unit;
        // 初期値
        double lx = cx + (1.0 + cc)*r;
        double ly = cy;
        int cr = 0xFF;
        int cg = 0x00;
        int cb = 0x00;

        for (i = 1; i <= len; i++) {
            // 極方程式
            double a = i * 2*PI / len;
            double tr = 1.0 + sc*Math.sin(a*stc) + cc*Math.cos(a*ctc);
            double nx = cx + tr*Math.cos(a)*r;
            double ny = cy - tr*Math.sin(a)*r;
            // 色
            stroke(rgb255(cr, cg, cb));
            // 描画
            line(lx, ly, nx, ny);
            // 座標更新
            lx = nx;
            ly = ny;
            // 色更新
            if (i % unit == 0) continue;
            switch (i / unit) {
            case 0: cg += d; break;
            case 1: cr -= d; break;
            case 2: cb += d; break;
            case 3: cg -= d; break;
            case 4: cr += d; break;
            case 5: cb -= d; break;
            }
        }
    }

    // EntryPoint
    public static void main(String[] args) {
        start();
        rulers();

        polar(CENTER_X, CENTER_Y, 40, 0.5, 0.3, 6, 24, 17);

        finish();
        return;
    }
}
