Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.Bauzoid.math;

public class Vector2
{
       
        public float x;
        public float y;
       
        public Vector2()
        {              
        }
       
        public Vector2(float _x, float _y)
        {
                x = _x;
                y = _y;
        }
       
        public void normalize()
        {
                float len = length();
                if (len != 0.0f)
                {
                        x /= len;
                        y /= len;
                }
        }
       
        public float length()
        {
                float squared = squaredLength();
                return (float)Math.sqrt(squared);
        }
       
        public void setLength(float newLength)
        {
                normalize();
                x *= newLength;
                y *= newLength;
        }
       
        public boolean isZero()
        {
                return ((x == 0.0f) && (y == 0.0f));
        }
       
        public float squaredLength()
        {
                return (x*x + y*y);
        }
       
        public void addVector(Vector2 v)
        {
                x += v.x;
                y += v.y;
        }
       
        public void subtractVector(Vector2 v)
        {
                x -= v.x;
                y -= v.y;
        }
       
        public float getAngle()
        {
                float angle = (float)Math.atan2(x, y);
                return MathUtil.stayInDegrees0to360(MathUtil.radToDeg(angle));         
        }
       
        public float dotProduct(Vector2 v)
        {
                return dotProduct(this, v);                            
        }
       
        static public float dotProduct(Vector2 a, Vector2 b)
        {
                return (a.x*b.x + a.y*b.y);
        }
       
        static public float distance(Vector2 a, Vector2 b)
        {
                Vector2 c = new Vector2(b.x - a.x, b.y - a.y);
                return c.length();
        }
       
        static public Vector2 fromRotation(float angle, float length)
        {
                Vector2 result = fromRotation(angle);
                result.setLength(length);
                return result;
        }
       
        static public Vector2 fromRotation(float angle)
        {
                return new Vector2((float)Math.cos(MathUtil.degToRad(angle)), (float)Math.sin(MathUtil.degToRad(angle)));              
        }
}