package com.roxiga.aquamotion3d;

public class Vector3D
{
	public float _x;
	public float _y;
	public float _z;
	
	public Vector3D(float x,float y,float z)
	{
		_x = x;
		_y = y;
		_z = z;
	}
	
	public float length()
    {
		return (float)(Math.sqrt(_x*_x + _y*_y + _z*_z));
    }

	public Vector3D subtract(Vector3D v)
	{
		Vector3D v2 = new Vector3D(0,0,0);
		
		v2._x = _x - v._x;
		v2._y = _y - v._y;
		v2._z = _z - v._z;

		return v2;
	}
	
	static public Vector3D Subtract(Vector3D dest, Vector3D eye)
	{
		float x = dest._x - eye._x;
		float y = dest._y - eye._y;
		float z = dest._z - eye._z;
				
		return new Vector3D(x,y,z);
	}
	
	public void normalize()
	{
		float l = this.length();
		if ( l == 0 )
		{
			_x = 0;
			_y = 0;
			_z = 1;
		}
		else
		{
			_x /= l;
			_y /= l;
			_z /= l;
		}
	}

	public float dotProduct(Vector3D v)
    {
 		return (v._x * _x + v._y * _y + v._z * _z);
    }

	public Vector3D crossProduct(Vector3D v)
	{
		float x = (_y*v._z) - (v._y*_z);
		float y = (v._x*_z) - (_x*v._z);
		float z = (_x*v._y) - (v._x*_y);
		return (new Vector3D(x, y, z));
	}
	
	
}
