// 2次ベジエ曲線

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

public class bezier1a extends Applet {
    public void paint(Graphics g) {
        double[] Ax = {0,1,2};
        double[] Ay = {0,2,1};
        double[] C = new double[2];
        int X=0, Y=0; 
        int x,y;
        double scale=100,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) {
            P(Ax,Ay,C,t);
            x = (int)(C[0]*scale);
            y = (int)(C[1]*scale);
            graphics.DrawPoint(g,X+x,Y+y);
        }
    }

    void P(double A[], double B[], double C[], double t) {
        C[0] =(1-t)*(1-t)*A[0]+2*t*(1-t)*A[1]+t*t*A[2];
        C[1] =(1-t)*(1-t)*B[0]+2*t*(1-t)*B[1]+t*t*B[2];
    }
}