
import java.awt.*;
import java.io.*;

public class BezierSegment
  implements Drawable
{
  public String name;
  public Dot p[];
  public Color color;

  public BezierSegment( String name,
                          Dot p0, Dot p1, Dot p2, Dot p3, Color color )
  {
    this.name = name;
    p = new Dot[4];
    p[0] = p0;
    p[1] = p1;
    p[2] = p2;
    p[3] = p3;
    this.color = color;
  }

  public void paint( Graphics g )
  {
    if( color != null )
      g.setColor( color );
    int i, n;
    double t, t_;
    double step;
    double a, b, c, d;
    int x, y, lastx, lasty, x0, y0, x1, y1, x2, y2, x3, y3;
    x0 = p[0].getX(); y0 = p[0].getY();
    x1 = p[1].getX(); y1 = p[1].getY();
    x2 = p[2].getX(); y2 = p[2].getY();
    x3 = p[3].getX(); y3 = p[3].getY();
    lastx = x0; lasty = y0;
    n = 20;
    step = 1.0 / n;
    for( i = 1; i <= n; i++ )
    {
      t = i * step;
      t_ = 1.0 - t;
      a = t_ * t_ * t_;
      b = 3 * t_ * t_ * t;
      c = 3 * t_ * t * t;
      d = t * t * t;
      x = (int) ( a * x0 + b * x1 + c * x2 + d * x3 );
      y = (int) ( a * y0 + b * y1 + c * y2 + d * y3 );
      g.drawLine( lastx, lasty, x, y );
      lastx = x; lasty = y;
    }
  }

  public void printPS( PrintStream O, int w, int h )
  {
    O.print( "  " );
    // super.printPS( O, w, h );
    int i;
    for( i = 0; i < 4; i++ ) {
      O.print( p[i].getX() );
      O.print( " " );
      O.print( h - p[i].getY() );
      if( i == 0 )
        O.print( " moveto " );
      else
        O.print( " " );
    }
    O.println( "curveto" );
  }
  public void printPS( PrintStream O, int w, int h, Dot fromPt )
  {
    O.print( "  " );
    // super.printPS( O, w, h );
    int i;
    Dot pt = p[0];
    for( i = 1; i < 4; i++ ) {
      if( fromPt == p[0] )
        pt = p[i];
      else
        pt = p[3-i];
      O.print( pt.getX() );
      O.print( " " );
      O.print( h - pt.getY() );
      O.print( " " );
    }
    O.println( "curveto" );
  }
  public int getX()
  {
    return 0;
  }
  public int getY()
  {
    return 0;
  }
}
