// 朝顔の葉1を描くクラス(leaf1 をクラスにしたもの)
// パラメータは, 根元の座標 (X,Y), 大きさ scale, 原点の回りの回転角度 theta

import java.applet.Applet;
import java.awt.*;

public class leaf1c extends Applet {
    public static void paint(Graphics g, int X, int Y, double scale, int color, double theta) {
        // int X = -400, Y = -100;  // 根元の座標
        // double scale = 200;      // 大きさ
        // int color = 0x008800;
        // double theta = 30;       // 2次元の回転角度
        int x_start,y_start,x_end,y_end;
        double xx,yy,dxx,xxx,yyy;

        for (xx=0; xx<0.95; xx+=0.0001) {
            graphics.SetColor(g,color);
            yy = f(xx);
            // 上側の縁
                x_start = (int)(graphics.RotateX(xx,yy,theta)*scale);
                y_start = (int)(graphics.RotateY(xx,yy,theta)*scale);
            // 下側の縁
                x_end = (int)(graphics.RotateX(xx,-yy,theta)*scale);
                y_end = (int)(graphics.RotateY(xx,-yy,theta)*scale);
	    graphics.DrawLine(g,X+x_start,Y+y_start,X+x_end,Y+y_end); 
        }

        graphics.SetColor(g,0xcccccc);
        // 中央の葉脈
        x_start = 0;
        y_start = 0;
        x_end = (int)(graphics.RotateX(0.95,0,theta)*scale);
        y_end = (int)(graphics.RotateY(0.95,0,theta)*scale);
        graphics.DrawLine(g,X+x_start,Y+y_start,X+x_end,Y+y_end);       

        // 葉脈 (3本)
        dxx = 0.15;
        for (xx=0.25; xx<0.5; xx+=dxx) {
            xxx = xx+dxx;
            yyy = f(xxx);
            x_start = (int)(graphics.RotateX(xx,0,theta)*scale);
            y_start = (int)(graphics.RotateY(xx,0,theta)*scale);
            x_end = (int)(graphics.RotateX(xxx,yyy,theta)*scale);
            y_end = (int)(graphics.RotateY(xxx,yyy,theta)*scale);
            graphics.DrawLine(g,X+x_start,Y+y_start,X+x_end,Y+y_end);
            x_end = (int)(graphics.RotateX(xxx,-yyy,theta)*scale);
            y_end = (int)(graphics.RotateY(xxx,-yyy,theta)*scale);
            graphics.DrawLine(g,X+x_start,Y+y_start,X+x_end,Y+y_end);
        }
    }

    static double f(double x) {
        double a = 1.75;  // 葉の幅
        double b = 0.0;   // ギザギザの深さ
        int n = 0;        // ギザギザの個数

        return a*x*(1-x)*(1-x)*(1+b*g(x,n));
    }

    static double g(double x, double n) {
        return n*x-Math.floor(n*x);
    }
}