Exemple #1
0
        public static bool CreateCircumscribed(Vector2 v0, Vector2 v1, Vector2 v2, out Circle2 circle)
        {
            Vector2 vector  = v1 - v0;
            Vector2 vector2 = v2 - v0;

            float[,] array = new float[2, 2];
            array[0, 0]    = vector.x;
            array[0, 1]    = vector.y;
            array[1, 0]    = vector2.x;
            array[1, 1]    = vector2.y;
            float[,] a     = array;
            float[] b = new float[]
            {
                0.5f * vector.sqrMagnitude,
                0.5f * vector2.sqrMagnitude
            };
            Vector2 b2;

            if (LinearSystem.Solve2(a, b, out b2, 1E-05f))
            {
                circle.Center = v0 + b2;
                circle.Radius = b2.magnitude;
                return(true);
            }
            circle = default(Circle2);
            return(false);
        }
Exemple #2
0
        public static bool CreateInscribed(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, out Sphere3 sphere)
        {
            Vector3 vector  = v1 - v0;
            Vector3 vector2 = v2 - v0;
            Vector3 vector3 = v3 - v0;
            Vector3 value   = v2 - v1;
            Vector3 vector4 = v3 - v1;
            Vector3 vector5 = vector4.Cross(value);
            Vector3 vector6 = vector2.Cross(vector3);
            Vector3 vector7 = vector3.Cross(vector);
            Vector3 vector8 = vector.Cross(vector2);

            if (Mathf.Abs(Vector3ex.Normalize(ref vector5, 1E-05f)) < 1E-05f)
            {
                sphere = default(Sphere3);
                return(false);
            }
            if (Mathf.Abs(Vector3ex.Normalize(ref vector6, 1E-05f)) < 1E-05f)
            {
                sphere = default(Sphere3);
                return(false);
            }
            if (Mathf.Abs(Vector3ex.Normalize(ref vector7, 1E-05f)) < 1E-05f)
            {
                sphere = default(Sphere3);
                return(false);
            }
            if (Mathf.Abs(Vector3ex.Normalize(ref vector8, 1E-05f)) < 1E-05f)
            {
                sphere = default(Sphere3);
                return(false);
            }
            float[,] array = new float[3, 3];
            array[0, 0]    = vector6.x - vector5.x;
            array[0, 1]    = vector6.y - vector5.y;
            array[0, 2]    = vector6.z - vector5.z;
            array[1, 0]    = vector7.x - vector5.x;
            array[1, 1]    = vector7.y - vector5.y;
            array[1, 2]    = vector7.z - vector5.z;
            array[2, 0]    = vector8.x - vector5.x;
            array[2, 1]    = vector8.y - vector5.y;
            array[2, 2]    = vector8.z - vector5.z;
            float[,] a     = array;
            float[] b = new float[]
            {
                0f,
                0f,
                -vector8.Dot(vector3)
            };
            Vector3 vector9;

            if (LinearSystem.Solve3(a, b, out vector9, 1E-05f))
            {
                sphere.Center = v3 + vector9;
                sphere.Radius = Mathf.Abs(vector5.Dot(vector9));
                return(true);
            }
            sphere = default(Sphere3);
            return(false);
        }
Exemple #3
0
        public static bool CreateCircumscribed(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, out Sphere3 sphere)
        {
            Vector3 vector  = v1 - v0;
            Vector3 vector2 = v2 - v0;
            Vector3 vector3 = v3 - v0;

            float[,] array = new float[3, 3];
            array[0, 0]    = vector.x;
            array[0, 1]    = vector.y;
            array[0, 2]    = vector.z;
            array[1, 0]    = vector2.x;
            array[1, 1]    = vector2.y;
            array[1, 2]    = vector2.z;
            array[2, 0]    = vector3.x;
            array[2, 1]    = vector3.y;
            array[2, 2]    = vector3.z;
            float[,] a     = array;
            float[] b = new float[]
            {
                0.5f * vector.sqrMagnitude,
                0.5f * vector2.sqrMagnitude,
                0.5f * vector3.sqrMagnitude
            };
            Vector3 b2;

            if (LinearSystem.Solve3(a, b, out b2, 1E-05f))
            {
                sphere.Center = v0 + b2;
                sphere.Radius = b2.magnitude;
                return(true);
            }
            sphere = default(Sphere3);
            return(false);
        }
Exemple #4
0
        internal static bool HeightPlaneFit3(IList <Vector3> points, out float a, out float b, out float c)
        {
            float num   = 0f;
            float num2  = 0f;
            float num3  = 0f;
            float num4  = 0f;
            float num5  = 0f;
            float num6  = 0f;
            float num7  = 0f;
            float num8  = 0f;
            int   count = points.Count;

            for (int i = 0; i < count; i++)
            {
                num  += points[i].x;
                num2 += points[i].y;
                num3 += points[i].z;
                num4 += points[i].x * points[i].x;
                num5 += points[i].x * points[i].y;
                num6 += points[i].x * points[i].z;
                num7 += points[i].y * points[i].y;
                num8 += points[i].y * points[i].z;
            }
            float[,] array = new float[3, 3];
            array[0, 0]    = num4;
            array[0, 1]    = num5;
            array[0, 2]    = num;
            array[1, 0]    = num5;
            array[1, 1]    = num7;
            array[1, 2]    = num2;
            array[2, 0]    = num;
            array[2, 1]    = num2;
            array[2, 2]    = (float)count;
            float[,] a2    = array;
            float[] b2 = new float[]
            {
                num6,
                num8,
                num3
            };
            float[] array2;
            bool    flag = LinearSystem.Solve3(a2, b2, out array2, 1E-05f);

            if (flag)
            {
                a = array2[0];
                b = array2[1];
                c = array2[2];
            }
            else
            {
                a = 3.40282347E+38f;
                b = 3.40282347E+38f;
                c = 3.40282347E+38f;
            }
            return(flag);
        }
Exemple #5
0
        public static bool Solve2(float[,] A, float[] B, out Vector2 X, float zeroTolerance = 1E-05f)
        {
            float[] array;
            bool    flag = LinearSystem.Solve2(A, B, out array, zeroTolerance);

            if (flag)
            {
                X.x = array[0];
                X.y = array[1];
            }
            else
            {
                X = Vector2ex.Zero;
            }
            return(flag);
        }
Exemple #6
0
        internal static bool HeightLineFit2(IList <Vector2> points, out float a, out float b)
        {
            float num   = 0f;
            float num2  = 0f;
            float num3  = 0f;
            float num4  = 0f;
            int   count = points.Count;

            for (int i = 0; i < count; i++)
            {
                num  += points[i].x;
                num2 += points[i].y;
                num3 += points[i].x * points[i].x;
                num4 += points[i].x * points[i].y;
            }
            float[,] array = new float[2, 2];
            array[0, 0]    = num3;
            array[0, 1]    = num;
            array[1, 0]    = num;
            array[1, 1]    = (float)count;
            float[,] a2    = array;
            float[] b2 = new float[]
            {
                num4,
                num2
            };
            float[] array2;
            bool    flag = LinearSystem.Solve2(a2, b2, out array2, 1E-05f);

            if (flag)
            {
                a = array2[0];
                b = array2[1];
            }
            else
            {
                a = 3.40282347E+38f;
                b = 3.40282347E+38f;
            }
            return(flag);
        }
Exemple #7
0
        public static bool Inverse(float[,] A, out float[,] invA)
        {
            if (A.GetLength(0) != A.GetLength(1))
            {
                invA = null;
                return(false);
            }
            int length = A.GetLength(0);

            invA = new float[length, length];
            Buffer.BlockCopy(A, 0, invA, 0, A.Length * 4);
            int[]  array  = new int[length];
            int[]  array2 = new int[length];
            bool[] array3 = new bool[length];
            int    num    = 0;
            int    num2   = 0;

            for (int i = 0; i < length; i++)
            {
                float num3 = 0f;
                for (int j = 0; j < length; j++)
                {
                    if (!array3[j])
                    {
                        for (int k = 0; k < length; k++)
                        {
                            if (!array3[k])
                            {
                                float num4 = Mathf.Abs(invA[j, k]);
                                if (num4 > num3)
                                {
                                    num3 = num4;
                                    num  = j;
                                    num2 = k;
                                }
                            }
                        }
                    }
                }
                if (num3 == 0f)
                {
                    invA = null;
                    return(false);
                }
                array3[num2] = true;
                if (num != num2)
                {
                    LinearSystem.SwapRows(invA, num, num2, length);
                }
                array2[i] = num;
                array[i]  = num2;
                float num5 = 1f / invA[num2, num2];
                invA[num2, num2] = 1f;
                for (int k = 0; k < length; k++)
                {
                    invA[num2, k] *= num5;
                }
                for (int j = 0; j < length; j++)
                {
                    if (j != num2)
                    {
                        float num6 = invA[j, num2];
                        invA[j, num2] = 0f;
                        for (int k = 0; k < length; k++)
                        {
                            invA[j, k] -= invA[num2, k] * num6;
                        }
                    }
                }
            }
            for (int j = length - 1; j >= 0; j--)
            {
                if (array2[j] != array[j])
                {
                    for (int k = 0; k < length; k++)
                    {
                        float num6 = invA[k, array2[j]];
                        invA[k, array2[j]] = invA[k, array[j]];
                        invA[k, array[j]]  = num6;
                    }
                }
            }
            return(true);
        }
Exemple #8
0
        public static bool Solve(float[,] A, float[] B, out float[] X)
        {
            if (A.GetLength(0) != A.GetLength(1) || A.GetLength(0) != B.Length)
            {
                X = null;
                return(false);
            }
            int length = A.GetLength(1);

            float[,] array = new float[A.GetLength(0), A.GetLength(1)];
            Buffer.BlockCopy(A, 0, array, 0, A.Length * 4);
            X = new float[length];
            Buffer.BlockCopy(B, 0, X, 0, length * 4);
            int[]  array2 = new int[length];
            int[]  array3 = new int[length];
            bool[] array4 = new bool[length];
            int    num    = 0;
            int    num2   = 0;

            for (int i = 0; i < length; i++)
            {
                float num3 = 0f;
                for (int j = 0; j < length; j++)
                {
                    if (!array4[j])
                    {
                        for (int k = 0; k < length; k++)
                        {
                            if (!array4[k])
                            {
                                float num4 = Mathf.Abs(array[j, k]);
                                if (num4 > num3)
                                {
                                    num3 = num4;
                                    num  = j;
                                    num2 = k;
                                }
                            }
                        }
                    }
                }
                if (num3 == 0f)
                {
                    X = null;
                    return(false);
                }
                array4[num2] = true;
                if (num != num2)
                {
                    LinearSystem.SwapRows(array, num, num2, length);
                    float num5 = X[num];
                    X[num]  = X[num2];
                    X[num2] = num5;
                }
                array3[i] = num;
                array2[i] = num2;
                float num6 = 1f / array[num2, num2];
                array[num2, num2] = 1f;
                for (int k = 0; k < length; k++)
                {
                    array[num2, k] *= num6;
                }
                X[num2] *= num6;
                for (int j = 0; j < length; j++)
                {
                    if (j != num2)
                    {
                        float num5 = array[j, num2];
                        array[j, num2] = 0f;
                        for (int k = 0; k < length; k++)
                        {
                            array[j, k] -= array[num2, k] * num5;
                        }
                        X[j] -= X[num2] * num5;
                    }
                }
            }
            return(true);
        }