mirror of
https://github.com/zeldaret/oot.git
synced 2025-07-04 06:54:33 +00:00
Lightweight trigonometry doc (#1356)
* Doc units of trig functions * "Very simple" yet I made a mistake * sins returns in [-0x7FFF,0x7FFF] as the [-1,1] range * Also `sys_math_atan.c` * Remove `@param`s without descriptions * Add note on Math_Atan2S/F arguments being unlike atan2 * "from (1,0) to (x,y)" -> "from vector ..." * arg names -> `angle` * Improve `@return` comment on atans
This commit is contained in:
parent
0b38f6e678
commit
0283493db8
10 changed files with 104 additions and 40 deletions
|
@ -3,9 +3,13 @@
|
|||
|
||||
s32 gUseAtanContFrac;
|
||||
|
||||
f32 Math_FTanF(f32 x) {
|
||||
f32 sin = sinf(x);
|
||||
f32 cos = cosf(x);
|
||||
/**
|
||||
* @param angle radians
|
||||
* @return tan(angle)
|
||||
*/
|
||||
f32 Math_FTanF(f32 angle) {
|
||||
f32 sin = sinf(angle);
|
||||
f32 cos = cosf(angle);
|
||||
|
||||
return sin / cos;
|
||||
}
|
||||
|
@ -42,7 +46,7 @@ f32 Math_FAtanTaylorQF(f32 x) {
|
|||
const f32* c = coeffs;
|
||||
f32 term;
|
||||
|
||||
while (1) {
|
||||
while (true) {
|
||||
term = *c++ * exp;
|
||||
if (poly + term == poly) {
|
||||
break;
|
||||
|
@ -124,6 +128,9 @@ f32 Math_FAtanContFracF(f32 x) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return arctan(x) in radians, in (-pi/2,pi/2) range
|
||||
*/
|
||||
f32 Math_FAtanF(f32 x) {
|
||||
if (!gUseAtanContFrac) {
|
||||
return Math_FAtanTaylorF(x);
|
||||
|
@ -132,6 +139,9 @@ f32 Math_FAtanF(f32 x) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return angle to (x,y) from vector (1,0) around (0,0) in radians, in (-pi,pi] range
|
||||
*/
|
||||
f32 Math_FAtan2F(f32 y, f32 x) {
|
||||
if (x == 0.0f) {
|
||||
if (y == 0.0f) {
|
||||
|
@ -152,10 +162,16 @@ f32 Math_FAtan2F(f32 y, f32 x) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return arcsin(x) in radians, in [-pi/2,pi/2] range
|
||||
*/
|
||||
f32 Math_FAsinF(f32 x) {
|
||||
return Math_FAtan2F(x, sqrtf(1.0f - SQ(x)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return arccos(x) in radians, in [0,pi] range
|
||||
*/
|
||||
f32 Math_FAcosF(f32 x) {
|
||||
return M_PI / 2 - Math_FAsinF(x);
|
||||
}
|
||||
|
|
|
@ -38,10 +38,18 @@ f32 Math_PowF(f32 base, s32 exp) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param angle radians
|
||||
* @return sin(angle)
|
||||
*/
|
||||
f32 Math_SinF(f32 angle) {
|
||||
return sins((s16)(angle * (0x7FFF / M_PI))) * SHT_MINV;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param angle radians
|
||||
* @return cos(angle)
|
||||
*/
|
||||
f32 Math_CosF(f32 angle) {
|
||||
return coss((s16)(angle * (0x7FFF / M_PI))) * SHT_MINV;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "global.h"
|
||||
|
||||
static u16 sATan2Tbl[] = {
|
||||
static u16 sAtan2Tbl[] = {
|
||||
0x0000, 0x000A, 0x0014, 0x001F, 0x0029, 0x0033, 0x003D, 0x0047, 0x0051, 0x005C, 0x0066, 0x0070, 0x007A, 0x0084,
|
||||
0x008F, 0x0099, 0x00A3, 0x00AD, 0x00B7, 0x00C2, 0x00CC, 0x00D6, 0x00E0, 0x00EA, 0x00F4, 0x00FF, 0x0109, 0x0113,
|
||||
0x011D, 0x0127, 0x0131, 0x013C, 0x0146, 0x0150, 0x015A, 0x0164, 0x016F, 0x0179, 0x0183, 0x018D, 0x0197, 0x01A1,
|
||||
|
@ -77,24 +77,34 @@ static u16 sATan2Tbl[] = {
|
|||
0x1FF6, 0x1FFB, 0x2000,
|
||||
};
|
||||
|
||||
u16 Math_GetAtan2Tbl(f32 x, f32 y) {
|
||||
/**
|
||||
* @param y must be >= 0 and <= x
|
||||
* @param x must be >= 0
|
||||
* @return arctan(y/x) as binang, in [0,0x2000] range
|
||||
*/
|
||||
u16 Math_GetAtan2Tbl(f32 y, f32 x) {
|
||||
u16 ret;
|
||||
|
||||
if (y == 0.0f) {
|
||||
ret = sATan2Tbl[0];
|
||||
if (x == 0.0f) {
|
||||
ret = sAtan2Tbl[0];
|
||||
} else {
|
||||
s32 tblIdx = ((x / y) * 1024.0f) + 0.5f;
|
||||
s32 tblIdx = ((y / x) * 1024.0f) + 0.5f;
|
||||
|
||||
if (tblIdx >= ARRAY_COUNT(sATan2Tbl)) {
|
||||
ret = sATan2Tbl[0];
|
||||
if (tblIdx >= ARRAY_COUNT(sAtan2Tbl)) {
|
||||
ret = sAtan2Tbl[0];
|
||||
} else {
|
||||
ret = sATan2Tbl[tblIdx];
|
||||
ret = sAtan2Tbl[tblIdx];
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return angle to (x,y) from vector (1,0) around (0,0) as binang, in [-0x8000,0x7FFF] range
|
||||
*
|
||||
* @note The arguments are (x,y), which is different from atan2's (y,x)
|
||||
*/
|
||||
s16 Math_Atan2S(f32 x, f32 y) {
|
||||
s32 ret;
|
||||
|
||||
|
@ -107,7 +117,7 @@ s16 Math_Atan2S(f32 x, f32 y) {
|
|||
}
|
||||
} else {
|
||||
if (-x < y) {
|
||||
ret = Math_GetAtan2Tbl(-x, y) + 0x4000;
|
||||
ret = 0x4000 + Math_GetAtan2Tbl(-x, y);
|
||||
} else {
|
||||
ret = 0x8000 - Math_GetAtan2Tbl(y, -x);
|
||||
}
|
||||
|
@ -115,21 +125,27 @@ s16 Math_Atan2S(f32 x, f32 y) {
|
|||
} else {
|
||||
if (x < 0.0f) {
|
||||
if (-y <= -x) {
|
||||
ret = Math_GetAtan2Tbl(-y, -x) + 0x8000;
|
||||
ret = 0x8000 + Math_GetAtan2Tbl(-y, -x);
|
||||
} else {
|
||||
ret = 0xC000 - Math_GetAtan2Tbl(-x, -y);
|
||||
}
|
||||
} else {
|
||||
if (x < -y) {
|
||||
ret = Math_GetAtan2Tbl(x, -y) + 0xC000;
|
||||
ret = 0xC000 + Math_GetAtan2Tbl(x, -y);
|
||||
} else {
|
||||
ret = -Math_GetAtan2Tbl(-y, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
|
||||
return (s16)ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return angle to (x,y) from vector (1,0) around (0,0) in radians, in [-pi,pi) range
|
||||
*
|
||||
* @note The arguments are (x,y), which is different from atan2's (y,x)
|
||||
*/
|
||||
f32 Math_Atan2F(f32 x, f32 y) {
|
||||
return BINANG_TO_RAD(Math_Atan2S(x, y));
|
||||
}
|
||||
|
|
|
@ -23,10 +23,18 @@ void Lib_MemSet(u8* dest, size_t len, u8 val) {
|
|||
// clang-format on
|
||||
}
|
||||
|
||||
/**
|
||||
* @param angle binang
|
||||
* @return cos(angle)
|
||||
*/
|
||||
f32 Math_CosS(s16 angle) {
|
||||
return coss(angle) * SHT_MINV;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param angle binang
|
||||
* @return sin(angle)
|
||||
*/
|
||||
f32 Math_SinS(s16 angle) {
|
||||
return sins(angle) * SHT_MINV;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue