Subversion Repositories AndroidProjects

Rev

Rev 787 | 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 Vector3
    {
        public float x;
        public float y;
        public float z;

        public Vector3()
        {
        }

        public Vector3(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 bool 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);
        }

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

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