Subversion Repositories AndroidProjects

Rev

Rev 1423 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BauzoidNET.math
{
    public class Vector2
    {
        public float X
        {
            get { return x; }
            set { x = value; }
        }

        public float Y
        {
            get { return y; }
            set { y = value; }
        }

        public float x;
        public float y;

        public override string ToString()
        {
            return "Vector2";
        }


        public Vector2()
        {
        }

        public Vector2(Vector2 o)
        {
            set(o.x, o.y);
        }

        public Vector2(float _x, float _y)
        {
            set(_x, _y);
        }

        public void set(float _x, float _y)
        {
            x = _x;
            y = _y;
        }

        public void setFrom(Vector2 other)
        {
            x = other.x;
            y = other.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 bool 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)));
        }

        /** Return a copy of this Vector2. */
        public Vector2 copy()
        {
            return new Vector2(x, y);
        }

        /// <summary>
        /// Return the largest component of the Vector3.
        /// </summary>
        public float max()
        {
            return Math.Max(x, y);
        }

        /// <summary>
        /// Return the smallest component of the Vector3.
        /// </summary>
        public float min()
        {
            return Math.Min(x, y);
        }
    }
}