import java.awt.*;
import java.lang.*;

public class Vertex3D  implements Drawable   {
  
  public final static int FRACBITS = 12 ;
  private float coord[];
  protected int size = 4;
  private int color;
  protected int vertexNo;

  ///////////////////////// CONSTRUCTORS ///////////////////
  
   /**
    initialize vertexNo, coord[], color through set...() functions
   */
  public Vertex3D( ) {
    coord = new float[size];
    color = -16777216; // 0xff000000 HAS NOT BEEN SET
    vertexNo = -1; //HAS NOT BEEN SET
  }

  /**
    initialize components
   */
  public Vertex3D( float x, float y, float z, float w) {
    coord = new float[size];
    coord[0]=x; coord[1]=y; coord[2]=z; coord[3]=w;
    color = -16777216;
    vertexNo = -1;//HAS NOT BEEN SET
  }

  /**
    initialize components
   */
  public Vertex3D( float x, float y, float z, float w, int clr) {
    coord = new float[size];
    coord[0]=x; coord[1]=y; coord[2]=z; coord[3]=w;
    color = clr;
    vertexNo = -1;//HAS NOT BEEN SET
  }

  /**
    initialize components
   */
  public Vertex3D( int vNo, float x, float y, float z, float w, int clr) {
    coord = new float[size];
    coord[0]=x; coord[1]=y; coord[2]=z; coord[3]=w;
    color = clr;
    vertexNo = vNo;
  }


  ////////////////////////// METHODS //////////////////// 

  public final void loadOrigin( ) {
      for( int i=0; i< (size-1); i++){
	  coord[i] = 0.0f;
      }
      coord[size-1] = 1.0f;
      return;
  }
    
    public final float getCoord( int i) {
	return coord[i];
    }

    public final void setCoord( int i, float val) {
	coord[i] = val;
    }

    public final int getColor() {
	return color;
    }

    public final void setColor( int clr) {
      color = clr;
    }

  //CHANGE THIS LATER
    public final void setColor( Vertex3D v0, Vertex3D v1) {
      color = (int)( (v0.getColor() + v1.getColor() )/2.0f);
    }

//     protected void lineEqn(float eqn[], Vertex3D v0, Vertex3D v1){
//       float x0 = v0.getCoord(0);   float y0 = v0.getCoord(1);
//       int clr0 = v0.getColor();
//       float x1 = v1.getCoord(0);   float y1 = v1.getCoord(1);
//       int clr1 = v1.getColor(); 
//       if( isNotZero(x0-x1) ) {
// 	eqn[1] =  
//      }

//     protected void lineEqn(float eqn[], int p0, int p1, ){
//       float x0 = v0.getCoord(0);   float y0 = v0.getCoord(1);
//       int clr0 = v0.getColor();
//       float x1 = v1.getCoord(0);   float y1 = v1.getCoord(1);
//       int clr1 = v1.getColor(); 
//       if( isNotZero(x0-x1) ) {
// 	eqn[1] =  
//      }

    public void setVertexNo( int someNo  ) {
       vertexNo = someNo;
  } 
  
    public int getVertexNo() {
        return vertexNo;
  } 

  
    public void draw( Raster r){
        //System.out.println("DRAWING VERTEX");
        //System.out.println(" (int)(coord[0]+0.5)="+(int)(coord[0]+0.5)+" (int)(coord[1]+0.5)="+ (int)(coord[1]+0.5)+" color=" + color);   
        r.setPixel( color, (int)(coord[0]+0.5), (int)(coord[1]+0.5));
  }
  
    public  void drawLine( Raster r ){;}
    public  void drawLine( Raster r, float[] alpha, float[] red,  float[] green,  float[] blue) {;} 
    public void draw( Raster r, Buffer buf){;}

  protected boolean isNotZero( float f ) {
    if ( Math.abs(f) >= (1>>FRACBITS) ) return true; else return false;
  }
  protected boolean isPosZero( float f ) {
    if ( (f>0)&&( Math.abs(f) < (1>>FRACBITS)) ) return true;
    else return false;
  }
  protected boolean isNegZero( float f ) {
    if ( (f<0)&&( Math.abs(f) < (1>>FRACBITS)) ) return true;
    else return false;
  }


//     //ACCOUNT FOR W=0 IN THE FUNCTIONS BELOW, IF THESE FUNCTIONS ARE NECESSARY
//   public void subtract( Vertex3D v, Vertex3D res ) {
//     for( int i=0; i<3; i++ ) {
//       res.setCoord( i, (this.coord[i]/this.coord[3])-(v.getCoord(i)/v.getCoord(i))  );
//     }
//     res.setCoord(3,1);
//   }

//   public void add( Vertex3D v, Vertex3D res ) {
//     if( (isNotZero(this.coord[3])) &&  (isNotZero(v.getCoord(3))) ) {
//       for( int i=0; i<3; i++ ) {
// 	res.setCoord( i, (this.coord[i]/this.coord[3])+(v.getCoord(i)/v.getCoord(i))  );
//       }
//       res.setCoord(3,1);
//     } else {
//       System.err.println("ERROR: Vertex3D.add(): problem with one or both w coordinate=0" );
//     }
//   }

//   public void scalarMult( float k ) {
//     if( isNotZero(coord[3]) ) {
//       for( int i=0; i<3; i++ ) {
// 	coord[i] *= k/coord[3];
//       }
//       coord[3] = 1;
//     }
//     if( isPosZero(coord[3]) ) {
// 	for( int i=0; i<3; i++ ) {
// 	  coord[i] *= k;
//       }
//       coord[3] = 0;
//     }  
//     if( isNegZero(coord[3]) ) {
//       for( int i=0; i<3; i++ ) {
// 	coord[i] *= -k;
//       }
//       coord[3] = 0;
//     }  
//   }


  public void print() {
      System.out.println(" x= " + coord[0]+ " y= " + coord[1] +  " z= " + coord[2] +  " w= " + coord[3]+  " color= " + color );  
  }


} //VERTEX3D

