public TestVectorFunctions()
        {
            float angleDegrees = 45f;

            Console.WriteLine("Deg2Rad ok ?:" + (Vec2.Deg2Rad(angleDegrees) == 0.25f * Mathf.PI));

            float angleRadiand = 0.25f * Mathf.PI;

            Console.WriteLine("Rad2Deg ok ?:" + (Vec2.Rad2Deg(angleRadiand) == 45f));

            Vec2 setNewAngleDegrees = new Vec2(10, 0);
            Vec2 expected           = new Vec2(7.071068f, 7.071068f);

            setNewAngleDegrees.SetAngleDegrees(45);
            Console.WriteLine("SetAngleDegrees ok ?:" + (setNewAngleDegrees.x == expected.x && setNewAngleDegrees.y == expected.y));

            Vec2 setNewAngleRadians = new Vec2(10, 0);
            Vec2 expectedRad        = new Vec2(7.071068f, 7.071068f);

            setNewAngleRadians.SetAngleRadians(Mathf.PI * 0.25f);
            Console.WriteLine("SetAngleRadians ok ?:" + (setNewAngleRadians.x == expectedRad.x && setNewAngleRadians.y == expectedRad.y));

            Vec2 getAndleDegrees = new Vec2(10, 10);

            Console.WriteLine("getAndleDegrees ok ?:" + (getAndleDegrees.GetAngleDegrees() == 45f));


            Vec2 getAndleRadians = new Vec2(10, 10);

            Console.WriteLine("getAndleRadians ok ?:" + (getAndleRadians.GetAngleRadians() == Vec2.Deg2Rad(45f)));


            Vec2 originalRoatateDeg = new Vec2(10, 0);
            Vec2 endRoatateDeg      = new Vec2(7.071068f, 7.071068f);

            originalRoatateDeg.RotateDegrees(45f);
            Console.WriteLine("RotateDegrees ok ?:" + (originalRoatateDeg.x == endRoatateDeg.x && originalRoatateDeg.y == endRoatateDeg.y));

            Vec2 originalRoatateRad = new Vec2(10, 0);
            Vec2 endRoatateRad      = new Vec2(7.071068f, 7.071068f);

            originalRoatateRad.RotateRadians(Mathf.PI * 0.25f);
            Console.WriteLine("RotateRadians ok ?:" + (originalRoatateRad.x == endRoatateRad.x && originalRoatateRad.y == endRoatateRad.y));

            Vec2 originalRoatateOverDeg = new Vec2(10, 0);
            Vec2 pointDeg          = new Vec2(5, 0);
            Vec2 endRoatateOverDeg = new Vec2(8.535534f, 3.535534f);

            originalRoatateOverDeg.RotateAroundDegrees(45f, pointDeg);
            Console.WriteLine("RotateOverDegrees ok ?:" + (originalRoatateOverDeg.x == endRoatateOverDeg.x && originalRoatateOverDeg.y == endRoatateOverDeg.y));

            Vec2 originalRoatateOverRad = new Vec2(10, 0);
            Vec2 pointRad          = new Vec2(5, 0);
            Vec2 endRoatateOverRad = new Vec2(8.535534f, 3.535534f);

            originalRoatateOverRad.RotateAroundRadians(Mathf.PI * 0.25f, pointDeg);
            Console.WriteLine("RotateOverRadians ok ?:" + (originalRoatateOverRad.x == endRoatateOverRad.x && originalRoatateOverRad.y == endRoatateOverRad.y));
        }
Ejemplo n.º 2
0
        public static void UnitTest()
        {
            Vec2  testVec   = new Vec2(2, 4);
            Vec2  testVec2  = new Vec2(3, 4);
            float tempFloat = 0;

            Vec2 temp = testVec + testVec2;

            Console.WriteLine("Addition correct?: " + (Math.Abs(temp.x - 5) < 0.01f && Math.Abs(temp.y - 8) < 0.01f));

            temp = testVec - testVec2;
            Console.WriteLine("Substraction correct?: " + (Math.Abs(temp.x + 1) < 0.01f && Math.Abs(temp.y) < 0.01f));

            temp = 2 * testVec;
            Console.WriteLine("Multiplication on left correct?: " + (Math.Abs(temp.x - 4) < 0.01f && Math.Abs(temp.y - 8) < 0.01f));

            temp = testVec * 2;
            Console.WriteLine("Multiplication on right correct?: " + (Math.Abs(temp.x - 4) < 0.01f && Math.Abs(temp.y - 8) < 0.01f));

            tempFloat = testVec2.Length();
            Console.WriteLine("Length correct?: " + (Math.Abs(tempFloat - 5) < 0.01f));

            testVec2.Normalize();
            Console.WriteLine("Normalize correct?: " + (Math.Abs(testVec2.Length() - 1) < 0.01f && Math.Abs(testVec2.x - 0.6f) < 0.01f && Math.Abs(testVec2.y - 0.8f) < 0.01f));

            testVec2 = new Vec2(3, 4);

            temp = testVec2.Normalized();
            Console.WriteLine("Normalized correct?: " + (Math.Abs(temp.Length() - 1) < 0.01f && Math.Abs(temp.x - 0.6f) < 0.01f && Math.Abs(temp.y - 0.8f) < 0.01f));

            testVec.SetXY(15, 2);
            Console.WriteLine("SetXY correct?: " + (Math.Abs(testVec.x - 15) < 0.01f && Math.Abs(testVec.y - 2) < 0.01f));

            Console.WriteLine("Deg2Rad correct?: " + (Math.Abs(Deg2Rad(180) - Math.PI) < 0.01f));

            Console.WriteLine("Rad2Deg correct?: " + (Math.Abs(Rad2Deg((float)Math.PI) - 180) < 0.01f));

            testVec = GetUnitVectorDeg(180);
            Console.WriteLine("GetUnitVectorDeg correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f));

            testVec = GetUnitVectorRad((float)Math.PI);
            Console.WriteLine("GetUnitVectorRad correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f));

            Console.WriteLine("RandomUnitVector correct?: " + (Math.Abs(RandomUnitVector().Length() - 1) < 0.01f));

            testVec = RandomUnitVector();
            testVec.SetAngleDegrees(270);
            Console.WriteLine("SetAngleDegrees correct?: " + (Math.Abs(testVec.y + 1) < 0.01f && Math.Abs(testVec.x) < 0.01f));

            Console.WriteLine("GetAngleDegrees correct?: " + (Math.Abs(testVec.GetAngleDegrees() - 270) < 0.01f));

            testVec.SetAngleRadians((float)Math.PI);
            Console.WriteLine("SetAngleRadians correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f));

            Console.WriteLine("GetAngleRadians correct?: " + (Math.Abs(testVec.GetAngleRadians() - Math.PI) < 0.01f));

            testVec.RotateDegrees(90);
            Console.WriteLine("RotateDegrees correct?: " + (Math.Abs(testVec.GetAngleDegrees() - 270) < 0.01f));

            testVec = RandomUnitVector();
            testVec.SetAngleDegrees(180);

            testVec.RotateRadians((float)Math.PI / 2);
            Console.WriteLine("RotateRadians correct?: " + (Math.Abs(testVec.GetAngleRadians() - (3 * Math.PI) / 2) < 0.01f));

            testVec  = new Vec2(2, 4);
            testVec2 = new Vec2(3, 4);

            Console.WriteLine("Dot correct?: " + ((testVec.Dot(testVec2) - 22) < 0.01f));

            Vec2 testVecNormal = testVec2.Normal();

            Console.WriteLine("Normal correct?: " + (Math.Abs(testVecNormal.x + 0.8f) < 0.01f && Math.Abs(testVecNormal.y - 0.6f) < 0.01f && Math.Abs(testVecNormal.Length() - 1) < 0.01f));

            testVec.Reflect(1, testVec2);
            Console.WriteLine("Reflect correct?: " + (Math.Abs(testVec.x - 3.28f) < 0.01f && Math.Abs(testVec.y - 3.04f) < 0.01f));
        }