using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MPStudio
{
    public class Vec3Tool : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
            var res = SolveLinearEquation2x2(3, 4, -2, 2, 1, 2);
            print(res);
            /*
            print("hhh");
            var a = new Vector3(5, 2, -3);
            var b = new Vector3(8, 1, -4);
            print(a.magnitude+":"+Len(a));
            print(Vector3.Dot(a,b));
            print(Dot(a,b));
            */


            /*
            var a = new Vector3(5, -2, 0);
            var b = new Vector3(1, 2, 3);
            var v1 = Cross(a, b);
            var v2 = Unit(v1);
            print(v2);
            print(Angel(a,b));
            print(Angel2(a,b));
            */
            /*
            Matrix4x4 a1 = new Matrix4x4(
                new Vector4(1, 0, 0, 0), // 第1列
                new Vector4(0, 1, 0, 0), // 第2列
                new Vector4(0, 0, 1, 0), // 第3列
                new Vector4(0, 0, 0, 1)  // 第4列
            );
            Matrix4x4 a2 = new Matrix4x4(
                new Vector4(1, 2, 3, 4), // 第1列
                new Vector4(0, 1, 0, 0), // 第2列
                new Vector4(0, 0, 1, 0), // 第3列
                new Vector4(0, 0, 0, 1)  // 第4列
            );
            Matrix4x4 a3 = new Matrix4x4(
                new Vector4(1, 2, 3, 4), // 第1列
                new Vector4(5, 6, 7, 8), // 第2列
                new Vector4(11, 22,33, 44), // 第3列
                new Vector4(55, 66, 77, 88)  // 第4列
            );
            print(a2.Equals(a1));
            print(MatrixAdd(a1,a2));
            print(MatrixMul(a2,5));
            print(MatrixTranspose(a3));
            print(a3);
            */
            /*
            Matrix4x4 m1 = InitMatrix(
                new Vector4(1, 1, 1, 1),
                new Vector4(0, 0, 0, 0),
                new Vector4(2, 2, 2, 2),
                new Vector4(3, 3, 3, 3)
            );

            print(MatrixTranspose(m1));
            print(m1*m1);
            print(m1*(new Vector4(2,3,2,1)));//结果返回一个Vec4
            */

            Quaternion a = Quaternion.identity;
            Quaternion b = Quaternion.identity;


            //print(Quaternion.);
        }

        public static float FixedDeltaTime()
        {
            return 1f / Application.targetFrameRate;
        }

        public static float ThrowMoveS(float v0, float t)
        {
            return v0 * t - 4.9f * t * t;
        }

        public static float ThrowMoveSpeedY(float len, float jiaodu)
        {

            return Mathf.Sin(jiaodu * (Mathf.PI / 180f)) * len;
        }

        public static float ThrowMoveSpeedX(float len, float jiaodu)
        {
            return Mathf.Cos(jiaodu * (Mathf.PI / 180f)) * len;
        }

        //s曲线
        static Vector3 SmoothStep()
        {
            return Vector3.back;
        }

        //叉乘求面积
        static float TriArea(Vector3 a, Vector3 b)
        {
            return Vector3.Cross(a, b).magnitude / 0.5f;
        }

        //直线标准向量和距离
        static Vector2 LineVec2(Vector2 p1, Vector2 p2, out float d)
        {
            float m = (p2.y - p1.y) / (p2.x - p1.x);
            float tmp = Mathf.Sqrt(m * m + 1);
            Vector2 res = new Vector2(m / tmp, -1 / tmp);
            d = (m * p1.x - p1.y) / tmp;
            return res;
        }

        static Matrix4x4 TRS(Vector3 a, Quaternion b, Vector3 c)
        {
            return Matrix4x4.TRS(a, b, c);
        }

        static Matrix4x4 InitMatrix(Vector4 v1, Vector4 v2, Vector4 v3, Vector4 v4)
        {
            Matrix4x4 mat = new Matrix4x4();
            mat.SetRow(0, v1);
            mat.SetRow(1, v2);
            mat.SetRow(2, v3);
            mat.SetRow(3, v4);
            return mat;
        }

        //转置
        static Matrix4x4 MatrixTranspose(Matrix4x4 m)
        {
            return m.transpose;
        }

        static bool MEqual(UnityEngine.Matrix4x4 a, UnityEngine.Matrix4x4 b)
        {
            return a.Equals(b);
        }

        public static Matrix4x4 MatrixMul(Matrix4x4 matrix, float scalar)
        {
            Matrix4x4 result = new Matrix4x4();
            for (int i = 0; i < 16; i++) result[i] = matrix[i] * scalar;
            return result;
        }

        public static Matrix4x4 MatrixAdd(Matrix4x4 a, Matrix4x4 b)
        {
            Matrix4x4 res = new Matrix4x4();

            for (int i = 0; i < 16; i++) res[i] = a[i] + b[i];
            return res;
        }


        //极坐标，半径和弧度
        static Vector2 Polar(Vector2 v)
        {
            float r = v.magnitude;
            float angle = Mathf.Atan2(v.y, v.x);
            return new Vector2(r, angle);
        }

        //极坐标转换成笛卡尔坐标
        public static Vector2 PolarToCartesian(float radius, float angleRadians)
        {
            return new Vector2(
                radius * Mathf.Cos(angleRadians),
                radius * Mathf.Sin(angleRadians)
            );
        }

        static Vector3 Law(Vector3 a, Vector3 b)

        {

            var v1 = Cross(a, b);
            var v2 = Unit(v1);
            return v2;

        }

        static float Angel(Vector3 a, Vector3 b)
        {
            float dVal = Dot(a, b);
            float mVal = Len(a) * Len(b);
            float cVal = dVal / mVal;
            return Mathf.Acos(cVal) * (180 / Mathf.PI);
        }

        static float Angel2(Vector3 a, Vector3 b)

        {
            float dVal = Len(Cross(a, b));
            float mVal = Len(a) * Len(b);
            float cVal = dVal / mVal;
            return Mathf.Asin(cVal) * (180 / Mathf.PI);
        }


        static Vector3 Unit(Vector3 a)
        {

            float len = Len(a);
            float x = a.x / len;
            float y = a.y / len;
            float z = a.z / len;
            return new Vector3(x, y, z);

        }

        static float Len(Vector3 v)
        {
            float t = Mathf.Pow(v.x, 2) + Mathf.Pow(v.y, 2) + Mathf.Pow(v.z, 2);
            return Mathf.Sqrt(t);
        }

        static float Dot(Vector3 a, Vector3 b)
        {
            return a.x * b.x + a.y * b.y + a.z * b.z;
        }

        static Vector3 Cross(Vector3 v1, Vector3 v2)
        {
            float a1 = v1.x;
            float a2 = v1.y;
            float a3 = v1.z;

            float b1 = v2.x;
            float b2 = v2.y;
            float b3 = v2.z;

            float x = a2 * b3 - a3 * b2;
            float y = a3 * b1 - a1 * b3;
            float z = a1 * b2 - a2 * b1;
            return new Vector3(x, y, z);
        }


        //直线与直线的交点,abc表示Ax+By+C=0
        public static Vector2? SolveLinearEquation2x2(
            float a1, float b1, float c1,
            float a2, float b2, float c2)
        {
            // 计算行列式
            float det = a1 * b2 - a2 * b1;

            // 行列式为0表示无解或无穷多解
            if (Mathf.Abs(det) < Mathf.Epsilon)
            {
                Debug.LogWarning("方程组无解或有无穷多解");
                return null;
            }

            // 使用克莱姆法则求解
            float x = (c1 * b2 - c2 * b1) / det;
            float y = (a1 * c2 - a2 * c1) / det;

            return new Vector2(x, y);
        }
































    }
}