// 2次ベジエ曲線

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

public class bezier1 extends Applet {
    double[] Ax = {0,1,2};
    double[] Ay = {0,2,1};

    public void paint(Graphics g) {
        int scale = 100;
        int X = 0, Y = 0; 
        int x,y;
        double t;
 
        // 制御点
        graphics.SetColor(g,0x000000);
        graphics.FillCircle(g,(int)(Ax[0]*scale),(int)(Ay[0]*scale),4);
        graphics.FillCircle(g,(int)(Ax[1]*scale),(int)(Ay[1]*scale),4);
        graphics.FillCircle(g,(int)(Ax[2]*scale),(int)(Ay[2]*scale),4);
        graphics.DrawString(g,"P0",(int)(Ax[0]*scale)-25,(int)(Ay[0]*scale));
        graphics.DrawString(g,"P1",(int)(Ax[1]*scale),(int)(Ay[1]*scale)+15);
        graphics.DrawString(g,"P2",(int)(Ax[2]*scale)+15,(int)(Ay[2]*scale));

        // 接線
        graphics.SetColor(g,0xff00);
        graphics.DrawLine(g,(int)(Ax[0]*scale),(int)(Ay[0]*scale),
                            (int)(Ax[1]*scale),(int)(Ay[1]*scale));
        graphics.DrawLine(g,(int)(Ax[1]*scale),(int)(Ay[1]*scale),
                            (int)(Ax[2]*scale),(int)(Ay[2]*scale));

        // ベジエ曲線
        graphics.SetColor(g,0x0000ff);
        for (t=0; t<=1; t+=0.001) {
            x = (int)(Px(t)*scale);
            y = (int)(Py(t)*scale);
            graphics.DrawPoint(g,X+x,Y+y);
        }
    }

    double Px(double t) {
        return (1-t)*(1-t)*Ax[0]+2*t*(1-t)*Ax[1]+t*t*Ax[2];
    }

    double Py(double t) {
        return (1-t)*(1-t)*Ay[0]+2*t*(1-t)*Ay[1]+t*t*Ay[2];
    }
}