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

Detailed Description

Design Philosophy

Floating point or double precision trigonometry is slow. Look up tables are fast! These functions do a good job approximating sine, cosine, and tangent.

The arguments are in degrees, not radians, and they return (1024 * the trigonometric function).

Usage

Call getSin1024(), getCos1024(), or getTan1024() with a degree between 0 and 359 inclusive. If the input degree is out of bounds, the result will be indeterminate.

The sin1024 and tan1024 arrays contain the first 91 elements of the respective functions and can be used directly if you're careful.

Example

printf("%d\n", getSin1024(180));
int16_t getSin1024(int16_t degree)
Definition trigonometry.c:56

Go to the source code of this file.

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]
 

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]
extern

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]
extern

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