FastTrig
Fast trigonometry from tables, for games and demos (raycasting, rotations, fake 3D) on computers without an FPU: angles are integers with 1024 steps per full circle, the results are fixed point numbers. No floating point is used, so it is fast on a 68000 as well.
int angle = FastTrig.FromDegrees(30); // 85 (1024 steps = 360 degrees)int x = (radius * FastTrig.Cos(angle)) >> 14; // Sin and Cos: 1.14 fixed point, 16384 = 1.0int y = (radius * FastTrig.Sin(angle)) >> 14;int dir = FastTrig.Atan2(dy, dx); // the angle of a vector, 0..1023Every int is an angle: it is taken modulo 1024 (with a mask), so negative angles and sums past a full turn work.
FastTrig.Tan, FastTrig.Cot, FastTrig.Sec (1/cos) and FastTrig.Csc (1/sin) are 16.16 fixed point (65536 =
1.0); at their poles (tan at 90 degrees, …) they return 2147483647 or -2147483647. Sec and Csc give the step
lengths of a raycaster’s grid walk (DDA). The tables are generated by stdlib/tools/fasttrig.py.
Constants
Section titled “Constants”| Name | Description |
|---|---|
Steps |
The number of angle steps in a full circle (360 degrees). |
Mask |
angle & Mask is the angle in 0..1023. |
Quarter |
The angle of 90 degrees. |
Half |
The angle of 180 degrees. |
SinOne |
1.0 in the results of FastTrig.Sin and FastTrig.Cos (1.14 fixed point). |
FixedOne |
1.0 in the results of FastTrig.Tan, FastTrig.Cot, FastTrig.Sec and FastTrig.Csc (16.16 fixed point). |
const int Steps = 1024The number of angle steps in a full circle (360 degrees).
Since 0.15
const int Mask = 1023angle & Mask is the angle in 0..1023.
Since 0.15
Quarter
Section titled “Quarter”const int Quarter = 256The angle of 90 degrees.
Since 0.15
const int Half = 512The angle of 180 degrees.
Since 0.15
SinOne
Section titled “SinOne”const int SinOne = 163841.0 in the results of FastTrig.Sin and FastTrig.Cos (1.14 fixed point).
Since 0.15
FixedOne
Section titled “FixedOne”const int FixedOne = 655361.0 in the results of FastTrig.Tan, FastTrig.Cot, FastTrig.Sec and FastTrig.Csc (16.16 fixed point).
Since 0.15
Functions
Section titled “Functions”| Name | Description |
|---|---|
Sin |
The sine of angle (1024 steps per circle) in 1.14 fixed point: -16384 to 16384. |
Cos |
The cosine of angle (1024 steps per circle) in 1.14 fixed point: -16384 to 16384. |
Tan |
The tangent of angle in 16.16 fixed point. |
Cot |
The cotangent (1/tan) of angle in 16.16 fixed point. |
Sec |
The secant (1/cos) of angle in 16.16 fixed point: the length of the step of a ray to the next vertical grid line. |
Csc |
The cosecant (1/sin) of angle in 16.16 fixed point: the length of the step of a ray to the next horizontal grid line. |
Atan2 |
The angle of the vector (x, y) in steps, 0 to 1023, exact to about half a step. |
FromDegrees |
An angle in degrees converted to steps (rounded): FromDegrees(90) is 256. |
ToDegrees |
An angle in steps converted to degrees, 0 to 359 (rounded). |
int16 Sin(int angle)The sine of angle (1024 steps per circle) in 1.14 fixed point: -16384 to 16384.
Since 0.15
int16 Cos(int angle)The cosine of angle (1024 steps per circle) in 1.14 fixed point: -16384 to 16384.
Since 0.15
int Tan(int angle)The tangent of angle in 16.16 fixed point.
Returns 2147483647 or -2147483647 at 90 and 270 degrees.
Since 0.15
int Cot(int angle)The cotangent (1/tan) of angle in 16.16 fixed point.
Returns 2147483647 or -2147483647 at 0 and 180 degrees.
Since 0.15
int Sec(int angle)The secant (1/cos) of angle in 16.16 fixed point: the length of the step of a ray to the next vertical grid line.
Returns 2147483647 or -2147483647 at 90 and 270 degrees.
Since 0.15
int Csc(int angle)The cosecant (1/sin) of angle in 16.16 fixed point: the length of the step of a ray to the next horizontal grid
line.
Returns 2147483647 or -2147483647 at 0 and 180 degrees.
Since 0.15
int Atan2(int y, int x)The angle of the vector (x, y) in steps, 0 to 1023, exact to about half a step.
Returns 0 for the zero vector.
Since 0.15
FromDegrees
Section titled “FromDegrees”int FromDegrees(int degrees)An angle in degrees converted to steps (rounded): FromDegrees(90) is 256.
Since 0.15
ToDegrees
Section titled “ToDegrees”int ToDegrees(int angle)An angle in steps converted to degrees, 0 to 359 (rounded).
Since 0.15