aboutsummaryrefslogtreecommitdiff
path: root/arm9/source/devices/math.c
blob: 33aeb63dc4bb70c5ccbad9a25c8c0f7583d4cee4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "math.h"


// Angle conversion constants.
const double fromRadians = 10430.378350470453;     // 65536 / 2π
const double toRadians   = 0.00009587379924285257; // 2π / 65536


// Reset the math device.
void math_reset(MathDevice *math) {
    math->x = 0;
    math->y = 0;
    math->r = 0;
    math->t = 0;
    math_clear_cartesian(math);
    math_clear_polar(math);
}

// Clear all values calculated from cartesian coordinates.
void math_clear_cartesian(MathDevice *math) {
    math->r_cached    = false;
    math->t_cached    = false;
    math->prod_cached = false;
    math->quot_cached = false;
    math->rem_cached  = false;
}

// Clear all values calculated from polar coordinates.
void math_clear_polar(MathDevice *math) {
    math->x_cached    = false;
    math->y_cached    = false;
}

// Calculate the x coordinate of the polar point.
s16 math_get_x(MathDevice *math) {
    if (!math->x_cached) {
        double x = cos(math->t * toRadians) * math->r;
        if (-32768.0 <= x && x <= 32768.0) {
            math->x_read = x;
        } else {
            math->x_read = 0;
        }
        math->x_cached = true;
    }
    return math->x_read;
}

// Calculate the y coordinate of the polar point.
s16 math_get_y(MathDevice *math) {
    if (!math->y_cached) {
        double y = sin(math->t * toRadians) * math->r;
        if (-32768.0 <= y && y <= 32767.0) {
            math->y_read = y;
        } else {
            math->y_read = 0;
        }
        math->y_cached = true;
    }
    return math->y_read;
}

// Calculate the r coordinate of the cartesian point.
u16 math_get_r(MathDevice *math) {
    if (!math->r_cached) {
        u32 x2 = math->x * math->x;
        u32 y2 = math->y * math->y;
        math->r_read = sqrt32(x2 + y2);
        math->r_cached = true;
    }
    return math->r_read;
}

// Calculate the t coordinate of the cartesian point.
u16 math_get_t(MathDevice *math) {
    if (!math->t_cached) {
        if (math->x || math->y) {
            math->t_read = atan2(math->y, math->x) * fromRadians;
        } else {
            math->t_read = 0;
        }
        math->t_cached = true;
    }
    return math->t_read;
}

// Calculate the product of x by y.
u32 math_get_prod(MathDevice *math) {
    if (!math->prod_cached) {
        math->prod_read = (u16)math->x * (u16)math->y;
        math->prod_cached = true;
    }
    return math->prod_read;
}

// Calculate the quotient of x by y.
u16 math_get_quot(MathDevice *math) {
    if (!math->quot_cached) {
        if (math->y) {
            math->quot_read = div32((u16)math->x, (u16)math->y);
        } else {
            math->quot_read = 0;
        }
        math->quot_cached = true;
    }
    return math->quot_read;
}

// Calculate the remainder of x by y.
u16 math_get_rem(MathDevice *math) {
    if (!math->rem_cached) {
        if (math->y) {
            math->rem_read = mod32((u16)math->x, (u16)math->y);
        } else {
            math->rem_read = 0;
        }
        math->rem_cached = true;
    }
    return math->rem_read;
}