using System;
//using System.Collections.Generic;

//namespace McGuffinsOfflineNetworkWorkshop {
    public class MVector2 {
        public float x, y;
        public MVector2() { x = y = 0; }
        public MVector2(float X, float Y) { x = X; y = Y; }
        public void copy(MVector2 other) {
            x = other.x;
            y = other.y;
        }
        public float magnitudeSquared() {
            return x * x + y * y;
        }
        public float magnitude() {
            return (float)Math.Sqrt(x * x + y * y);
        }
        static public MVector2 average(MVector2 a, MVector2 b) {
            return new MVector2((a.x + b.x) * 0.5f, (a.y + b.y) * 0.5f);
        }
        public static MVector2 operator -(MVector2 a, MVector2 b) {
            return new MVector2(a.x - b.x, a.y - b.y);
        }
        public static MVector2 operator +(MVector2 a, MVector2 b) {
            return new MVector2(a.x + b.x, a.y + b.y);
        }
        public static MVector2 operator *(MVector2 a, float k) {
            return new MVector2(a.x * k, a.y * k);
        }
        // returns the dot-product of the given vectors
        static public float operator *( MVector2 a, MVector2 b ) {
            return a.x*b.x + a.y*b.y;
        }

        // Computes the angle of rotation from v1 to v2 around the origin.
        // The angle returned is in the interval [-pi,pi],
        // where a positive angle corresponds to a counterclockwise rotation
        // (assuming x+ right, y+ up).
        public static float computeSignedAngle( MVector2 v1, MVector2 v2 ) {
            float productOfLengths = v1.magnitude() * v2.magnitude();
            if ( productOfLengths <= 0 )
                return 0;
            // Compute the z component of the cross product of v1 and v2
            // (Note that the x and y components of the cross product are zero,
            // because the z components of a and b are both zero)
            double crossProduct_z = v1.x*v2.y - v1.y*v2.x;

            double sineOfAngle = Math.Abs(crossProduct_z) / productOfLengths;

            // Due to numerical inaccuracies, the sine we computed
            // may be slightly more than 1.
            // Calling arcsin on such a value could be bad, so we don't.
            double angle = ( sineOfAngle >= 1 ) ? Math.PI/2 : Math.Asin( sineOfAngle );

            // Compute the dot product of v1 and v2
            float dotProduct = v1 * v2;

            if ( dotProduct < 0 )
                angle = Math.PI - angle;

            if ( crossProduct_z < 0 )
                angle = - angle;

            return (float)angle;
        }
    }
//}

