예제 #1
0
        //
        // Does a point p lie to the left, to the right, or on a vector going from a to b
        //
        //https://gamedev.stackexchange.com/questions/71328/how-can-i-add-and-subtract-convex-polygons
        public static float GetPointInRelationToVectorValue(MyVector2 a, MyVector2 b, MyVector2 p)
        {
            float x1 = a.x - p.x;
            float x2 = a.y - p.y;
            float y1 = b.x - p.x;
            float y2 = b.y - p.y;

            float determinant = MathUtility.Det2(x1, x2, y1, y2);

            return(determinant);
        }
예제 #2
0
 //Alternative 2 in 2d space [radians]
 public static float AngleFromToCCW(MyVector2 from, MyVector2 to)
 {
     //The determinant is similar to the dot product
     //The dot product is always 0 no matter in which direction the perpendicular vector is pointing
     //But the determinant is -1 or 1 depending on which way the perpendicular vector is pointing (up or down)
     //AngleBetween goes from 0 to 180 so we can now determine if we need to compensate to get 360 degrees
     if (MathUtility.Det2(from, to) > 0f)
     {
         return(AngleBetween(from, to));
     }
     else
     {
         return((Mathf.PI * 2f) - AngleBetween(from, to));
     }
 }
예제 #3
0
        //
        // Calculate the center of circle in 2d space given three coordinates - Simple version
        //
        //From the book "Geometric Tools for Computer Graphics"
        public static MyVector2 CalculateCircleCenter(MyVector2 a, MyVector2 b, MyVector2 c)
        {
            //Make sure the triangle a-b-c is counterclockwise
            if (!IsTriangleOrientedClockwise(a, b, c))
            {
                //Swap two vertices to change orientation
                (a, b) = (b, a);

                //Debug.Log("Swapped vertices");
            }


            //The area of the triangle
            float X_1 = b.x - a.x;
            float X_2 = c.x - a.x;
            float Y_1 = b.y - a.y;
            float Y_2 = c.y - a.y;

            float A = 0.5f * MathUtility.Det2(X_1, Y_1, X_2, Y_2);

            //Debug.Log(A);


            //The center coordinates:
            //float L_10 = MyVector2.Magnitude(b - a);
            //float L_20 = MyVector2.Magnitude(c - a);

            //float L_10_square = L_10 * L_10;
            //float L_20_square = L_20 * L_20;

            float L_10_square = MyVector2.SqrMagnitude(b - a);
            float L_20_square = MyVector2.SqrMagnitude(c - a);

            float one_divided_by_4_A = 1f / (4f * A);

            float x = a.x + one_divided_by_4_A * ((Y_2 * L_10_square) - (Y_1 * L_20_square));
            float y = a.y + one_divided_by_4_A * ((X_1 * L_20_square) - (X_2 * L_10_square));

            MyVector2 center = new MyVector2(x, y);

            return(center);
        }
        //In 2d space [radians]
        //If you want to calculate the angle from vector a to b both originating from c, from is a-c and to is b-c
        public static float AngleFromToCCW(MyVector2 from, MyVector2 to, bool shouldNormalize = false)
        {
            from = MyVector2.Normalize(from);
            to   = MyVector2.Normalize(to);

            float angleRad = AngleBetween(from, to, shouldNormalize = false);

            //The determinant is similar to the dot product
            //The dot product is always 0 no matter in which direction the perpendicular vector is pointing
            //But the determinant is -1 or 1 depending on which way the perpendicular vector is pointing (up or down)
            //AngleBetween goes from 0 to 180 so we can now determine if we need to compensate to get 360 degrees
            if (MathUtility.Det2(from, to) > 0f)
            {
                return(angleRad);
            }
            else
            {
                return((Mathf.PI * 2f) - angleRad);
            }
        }