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 argb;                // color of vertex

    public Vertex3D()
    {
        x = y = z = 0;
        w = 1;
        //argb = 0;
        //hasNormal = false;
    }

    public Vertex3D(float xval, float yval, float zval)
    {
        x = xval;
        y = yval;
        z = zval;
        w = 1;
        //argb = 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 normalize( )
    {
        if (w != 1) {
            w = 1 / w;
            x *= w;
            y *= w;
            z *= w;
            w = 1;
        }
    }

    public Vertex3D sub(Vertex3D vert) {
        // returns this - vert
        Vertex3D v_out = new Vertex3D();
        v_out.x = this.x - vert.x;
        v_out.y = this.y - vert.y;
        v_out.z = this.z - vert.z;
        return v_out;
    }

    public float dot(Vertex3D vert) {
        // returns this * vert
        return (this.x*vert.x + this.y*vert.y + this.z*vert.z);
    }

    public Vertex3D cross(Vertex3D vert) {
        // returns this x vert = [ a[2] b[3] - a[3] b[2], a[3] b[1] - a[1] b[3], a[1] b[2] - a[2] b[1] ]
        Vertex3D v_out = new Vertex3D();
        v_out.x = this.y*vert.z - this.z*vert.y;
        v_out.y = this.z*vert.x - this.x*vert.z;
        v_out.z = this.x*vert.y - this.y*vert.x;
        return v_out;
    }

    public String toString() {
        ///////////////////////////////////////////////////////////////
        // Pretty prints current Point3D to a string
        ///////////////////////////////////////////////////////////////
        String str;
        int i;
        String x_str = (new Float(x)).toString();
        String y_str = (new Float(y)).toString();
        String z_str = (new Float(z)).toString();
        String w_str = (new Float(w)).toString();

        int col_max = x_str.length();
        int num = y_str.length();
        if ( num > col_max )
            col_max = num;
        num = z_str.length();
        if ( num > col_max )
            col_max = num;

        str = "+-";
        for (i = 1; i<=col_max; i++) {
            str += " ";
        }
        str += "-+\n";

        str += "| ";
        for (i = x_str.length(); i < col_max; i++) {
          str += " ";
        }
        str += x_str;
        str += " |\n";

        str += "| ";
        for (i = y_str.length(); i < col_max; i++) {
          str += " ";
        }
        str += y_str;
        str += " |\n";

        str += "| ";
        for (i = z_str.length(); i < col_max; i++) {
          str += " ";
        }
        str += z_str;
        str += " |\n";

        str += "| ";
        for (i = w_str.length(); i < col_max; i++) {
          str += " ";
        }
        str += w_str;
        str += " |\n";

        str += "+-";
        for (i = 1; i<=col_max; i++) {
            str += " ";
        }
        str += "-+";

        return str;
    }

    public String[] toString2() {
        ///////////////////////////////////////////////////////////////
        // Pretty prints current Point3D to an array
        // of strings, a string for each row of the matrix
        ///////////////////////////////////////////////////////////////
        String str[] = new String[6];
        int i;
        String x_str = (new Float(x)).toString();
        String y_str = (new Float(y)).toString();
        String z_str = (new Float(z)).toString();
        String w_str = (new Float(w)).toString();

        int col_max = x_str.length();
        int num = y_str.length();
        if ( num > col_max )
            col_max = num;
        num = z_str.length();
        if ( num > col_max )
            col_max = num;

        str[0] = "+-";
        for (i = 1; i<=col_max; i++) {
            str[0] += " ";
        }
        str[0] += "-+";
        str[5] = str[0];

        str[1] = "| ";
        for (i = x_str.length(); i < col_max; i++) {
          str[1] += " ";
        }
        str[1] += x_str;
        str[1] += " |";

        str[2] = "| ";
        for (i = y_str.length(); i < col_max; i++) {
          str[2] += " ";
        }
        str[2] += y_str;
        str[2] += " |";

        str[3] = "| ";
        for (i = z_str.length(); i < col_max; i++) {
          str[3] += " ";
        }
        str[3] += z_str;
        str[3] += " |";

        str[4] = "| ";
        for (i = w_str.length(); i < col_max; i++) {
          str[4] += " ";
        }
        str[4] += w_str;
        str[4] += " |";

        return str;
    }
}
