Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.math;

public class Vector3
{
       
        public float x;
        public float y;
        public float z;
       
        public Vector3()
        {
        }
       
        public Vector3(float _x, float _y, float _z)
        {
                set(_x, _y, _z);
        }
       
        public void set(float _x, float _y, float _z)
        {
                x = _x;
                y = _y;
                z = _z;
        }
       
        public void normalize()
        {
                float len = length();
                if (len != 0.0f)
                {
                        x /= len;
                        y /= len;
                        z /= len;
                }
        }
       
        public float length()
        {
                float squared = squaredLength();
                return (float)Math.sqrt(squared);
        }
       
        public void setLength(float newLength)
        {
                normalize();
                x *= newLength;
                y *= newLength;
                z *= newLength;
        }
       
        public boolean isZero()
        {
                return ((x == 0.0f) && (y == 0.0f) && (z == 0.0f));
        }
       
        public float squaredLength()
        {
                return (x*x + y*y + z*z);
        }
       
        public void addVector(Vector3 v)
        {
                x += v.x;
                y += v.y;
                z += v.z;
        }
       
        public void subtractVector(Vector3 v)
        {
                x -= v.x;
                y -= v.y;
                z -= v.z;
        }
       
        public float dotProduct(Vector3 v)
        {
                return dotProduct(this, v);                            
        }
       
        static public float dotProduct(Vector3 a, Vector3 b)
        {
                return (a.x*b.x + a.y*b.y + a.z*b.z);
        }
       
        public Vector3 crossProduct(Vector3 v)
        {
                return crossProduct(this, v);          
        }
       
        static public Vector3 crossProduct(Vector3 a, Vector3 b)
        {
                return new Vector3(
                                (a.y * b.z) - (a.z * b.y),
                                (a.z * b.x) - (a.x * b.z),
                                (a.x * b.y) - (a.y * b.x));
        }
       
        static public float distance(Vector3 a, Vector3 b)
        {
                Vector3 c = new Vector3(b.x - a.x, b.y - a.y, b.z - a.z);
                return c.length();
        }
       
        static public Vector3 add(Vector3 a, Vector3 b)
        {
                return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
        }
       
        static public Vector3 subtract(Vector3 a, Vector3 b)
        {
                return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
        }
       
        static public float length(float x, float y, float z)
        {
                return (float)Math.sqrt(x*x + y*y + z*z);
        }
       
        /** Return a copy of this Vector3. */
        public Vector3 copy()
        {
                return new Vector3(x, y, z);
        }
}