package gl;

public class Vertex extends Object
{
    public double x;
    public double y;
    public double z;

    // ==== Constructors ====================================================

    public Vertex()
    {
	this( 0.0, 0.0, 1.0 );
    }

    public Vertex( Vertex v )
    {
	this( v.x, v.y, v.z );
    }

    public Vertex( double x, double y )
    {
	this( x, y, 1.0 );
    }

    public Vertex( double x, double y, double z )
     {
        this.x = x;
        this.y = y;
        this.z = z;
     }

    // ==== Methods =========================================================

    public double radius()
    {
	return Math.sqrt( x * x + y * y );
    }
    
    //-----------------------------------------------------------------------
    
    public Vertex normalize()
    {
	double s = this.radius();
	x /= s;
	y /= s;
	return this;
    }

    //-----------------------------------------------------------------------
    
    public Vertex plus( Vertex v )
    {
	return new Vertex( x + v.x, y + v.y );
    }

    //-----------------------------------------------------------------------
    
    public Vertex plus( double dx, double dy )
    {
	return new Vertex( x + dx, y + dy );
    }

    //-----------------------------------------------------------------------
    
    public Vertex minus( Vertex v )
    {
	return new Vertex( x - v.x, y - v.y );
    }

    //-----------------------------------------------------------------------
    
    public Vertex minus( double dx, double dy )
    {
	return new Vertex( x - dx, y - dy );
    }

    //-----------------------------------------------------------------------

    public String toString()
    {
	return "Vertex[" + String.valueOf( x ) + "," +
			   String.valueOf( y ) + "," +
	  		   String.valueOf( z ) + "]";
    }
}
