Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
trigonometry.c File Reference

Macros

#define ARCTAN_APPROX(y, x)   (-y * (47 * y - 182 * x) / 3 / x / x)
 

Functions

int16_t getSin1024 (int16_t degree)
 
int16_t getCos1024 (int16_t degree)
 
int32_t getTan1024 (int16_t degree)
 
int32_t cordicAtan2 (int32_t x, int32_t y)
 CORDIC approximation of arctan.
 
int16_t getAtan2 (int32_t y, int32_t x)
 Calculate the angle of the line from the origin to (x, y), in degrees.
 

Variables

const int16_t sin1024 [91]
 
const uint16_t tan1024 [91]
 

Macro Definition Documentation

◆ ARCTAN_APPROX

#define ARCTAN_APPROX ( y,
x )   (-y * (47 * y - 182 * x) / 3 / x / x)

Approximates arctan(y / x) to within 1 degree, but only when 0 <= (y / x) <= 1 Modified from https://math.stackexchange.com/a/1098585 to fit the format of your numbers.

Function Documentation

◆ getSin1024()

int16_t getSin1024 ( int16_t degree)

Integer sine function

Parameters
degreeThe degree, between 0 and 359
Returns
The sine of the degree, between -1024 and 1024

◆ getCos1024()

int16_t getCos1024 ( int16_t degree)

Integer cosine function

Parameters
degreeThe degree, between 0 and 359
Returns
The cosine of the degree, between -1024 and 1024

◆ getTan1024()

int32_t getTan1024 ( int16_t degree)

Integer tangent function

Parameters
degreeThe degree, between 0 and 359
Returns
The tangent of the degree, scaled by 1024

◆ cordicAtan2()

int32_t cordicAtan2 ( int32_t x,
int32_t y )

CORDIC approximation of arctan.

Parameters
xThe x component
yThe y component
Returns
The approximation of atan(y/x), in degrees

◆ getAtan2()

int16_t getAtan2 ( int32_t y,
int32_t x )

Calculate the angle of the line from the origin to (x, y), in degrees.

If both x and y are 0, atan2 is undefined and will return 0.

Parameters
yThe Y coordinate of the point to calculate the angle to
xThe X coordinate of the point to calculate the angle to
Returns
int16_t The angle degree, between 0 and 359.

Variable Documentation

◆ sin1024

const int16_t sin1024[91]
Initial value:
= {
0, 18, 36, 54, 71, 89, 107, 125, 143, 160, 178, 195, 213, 230, 248, 265, 282, 299, 316,
333, 350, 367, 384, 400, 416, 433, 449, 465, 481, 496, 512, 527, 543, 558, 573, 587, 602, 616,
630, 644, 658, 672, 685, 698, 711, 724, 737, 749, 761, 773, 784, 796, 807, 818, 828, 839, 849,
859, 868, 878, 887, 896, 904, 912, 920, 928, 935, 943, 949, 956, 962, 968, 974, 979, 984, 989,
994, 998, 1002, 1005, 1008, 1011, 1014, 1016, 1018, 1020, 1022, 1023, 1023, 1024, 1024,
}

A table of the first 91 values of (1024 * sin(x)). Only 91 values are necessary because getSin1024() and getCos1024() handle symmetry to calculate the full wave

◆ tan1024

const uint16_t tan1024[91]
Initial value:
= {
0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 181, 199, 218, 236, 255, 274,
294, 313, 333, 353, 373, 393, 414, 435, 456, 477, 499, 522, 544, 568, 591, 615,
640, 665, 691, 717, 744, 772, 800, 829, 859, 890, 922, 955, 989, 1024, 1060, 1098,
1137, 1178, 1220, 1265, 1311, 1359, 1409, 1462, 1518, 1577, 1639, 1704, 1774, 1847, 1926, 2010,
2100, 2196, 2300, 2412, 2534, 2668, 2813, 2974, 3152, 3349, 3571, 3822, 4107, 4435, 4818, 5268,
5807, 6465, 7286, 8340, 9743, 11704, 14644, 19539, 29324, 58665, 65535,
}

A table of the first 91 values of (1024 * tan(x)). Only 91 values are necessary because getTan1024() handles symmetry to calculate the full wave