Beispiel #1
0
    // Arc Tangent (inverse tangent)
    // Uses GBA implementation: http://www.coranac.com/documents/arctangent/
    public static FInt Atan(FInt dx, FInt dy)
    {
        if (dx.Value() == 0)
        {
            if (dy.Value() >= 0)
            {
                return new FInt(0.5) * PI;
            }
            else
            {
                return new FInt(1.5) * PI;
            }
        }
        if (dy.Value() == 0)
        {
            if (dx.Value() > 0)
            {
                return 0L;
            }
            else
            {
                return PI;
            }
        }

        if (dx.Value() < 0)
        {
            if (dy.Value() < 0)
            {
                return PI + Atan(-dx, -dy);
            }
            else
            {
                return PI - Atan(-dx, dy);
            }
        }
        else
        {
            if (dy.Value() < 0)
            {
                return 2 * PI - Atan(dx, -dy);
            }
        }

        bool swapAxes = false;
        if (dy > dx)
        {
            FInt tmp = dy;
            dy = dx;
            dx = tmp;
            swapAxes = true;
        }

        long t = (dy.Value() << FLOATING_BITS) / dx.Value();
        long t2 = -(t * t) >> FLOATING_BITS;
        long t4 = (t2 * t2) >> FLOATING_BITS;
        long t6 = (t4 * t2) >> FLOATING_BITS;
        long t8 = (t4 * t4) >> FLOATING_BITS;
        long t10 = (t6 * t4) >> FLOATING_BITS;
        long t12 = (t6 * t6) >> FLOATING_BITS;
        long t14 = (t8 * t6) >> FLOATING_BITS;
        FInt x = new FInt(
            ((65536L * t) >> FLOATING_BITS) +
            ((((21845L * t) >> FLOATING_BITS) * t2) >> FLOATING_BITS) +
            ((((13107L * t) >> FLOATING_BITS) * t4) >> FLOATING_BITS) +
            ((((9102L * t) >> FLOATING_BITS) * t6) >> FLOATING_BITS) +
            ((((6317L * t) >> FLOATING_BITS) * t8) >> FLOATING_BITS) +
            ((((3664L * t) >> FLOATING_BITS) * t10) >> FLOATING_BITS) +
            ((((1432L * t) >> FLOATING_BITS) * t12) >> FLOATING_BITS) +
            ((((266L * t) >> FLOATING_BITS) * t14) >> FLOATING_BITS));

        if (swapAxes)
        {
            return PI / 2 - x;
        }
        return x;
    }
Beispiel #2
0
    public static FInt Sqrt(FInt a)
    {
        if (a.Value() == 0L)
        {
            return a;
        }
        FInt x = 1L;
        for (int i = 0; i < 64; i++)
        {
            x = (x + a / x) / 2;
        }

        return x;
    }
Beispiel #3
0
 public static FInt Max(FInt a, FInt b)
 {
     long x = a.Value();
     long y = b.Value();
     return (x >= y) ? a : b;
 }
Beispiel #4
0
 public static FInt Min(FInt a, FInt b)
 {
     long x = a.Value();
     long y = b.Value();
     return (x >= y) ? b : a;
 }
Beispiel #5
0
 public FInt(FInt x)
 {
     value = x.Value();
     floatValue = (float)(value / FSHIFT);
 }