import java.awt.Color;

public class Vertex3D {
    public float x, y, z, w;           // coordinate of vertex
    public float nx, ny, nz;        // vertex normal
    public boolean hasNormal;
    public int counter = 0;
    public int argb;
    public int[] memberList = new int[100]; //holds list of
                                         //indices into triList that
                                         //indicate which tris its a vertex of

    public Vertex3D()
    {
        x = y = z = 0;
        nx = ny = nz = 0;
        w=1;
        hasNormal = false;
    }
    
    public Vertex3D(Vertex3D v) {
        this.copy(v);
    }


    public Vertex3D(float xval, float yval, float zval)
    {
        x = xval;
        y = yval;
        z = zval;
        w = 1;
        nx = ny = nz = 0;
        hasNormal = false;
    }
    
    
    public void copy(Vertex3D v)
    {
        x = v.x; 
        y = v.y;
        z = v.z;
        w = v.w;
        nx = v.nx;
        ny = v.ny;
        nz = v.nz;
        hasNormal = v.hasNormal;

    }
    
    public void addTri(int index) {
        //add index into trilist to memberlist
        if (counter<100) {
            memberList[counter]=index;
            counter++;
        }
    }

    public void setNormal(float xval, float yval, float zval) {
        float l = (float) (1 / Math.sqrt(xval*xval + yval*yval + zval*zval));
        xval *= l;
        yval *= l;
        zval *= l;
        nx = xval;
        ny = yval;
        nz = zval;
        hasNormal = true;
    }
    
    public void averageNormals(Triangle triList[]) {
        float xsum=0;
        float ysum=0;
        float zsum=0;
        for (int i=0;i<counter;i++) {
            Triangle tmp=triList[memberList[i]];
            xsum+=tmp.nx;
            ysum+=tmp.ny;
            zsum+=tmp.nz;
        }
        setNormal((xsum/counter),(ysum/counter),(zsum/counter));
    }

    public void normalize( )
    {
        if (w != 1) {
            w = 1 / w;
            x *= w;
            y *= w;
            z *= w;
            w = 1;
        }
    }
    
    public String toString()
    {
        return new String(" ["+x+", "+y+", "+z+", "+w+"]");
    }
}
