wings/src/maths.c

48 lines
609 B
C
Raw Normal View History

2017-04-20 12:44:09 +02:00
#include "maths.h"
2017-04-21 22:32:40 +02:00
float fcos(unsigned char x)
{
2017-04-26 11:25:48 +02:00
char n = 1;
2017-04-21 22:32:40 +02:00
float cos[4] = {1.0, 0.707106, 0.0, -0.707106};
2017-04-22 21:02:24 +02:00
while(x >= 4)
{
2017-04-26 11:25:48 +02:00
x -= 4;
n = -n;
2017-04-22 21:02:24 +02:00
}
2017-04-26 11:25:48 +02:00
return (n*cos[x]);
2017-04-21 22:32:40 +02:00
}
float fsin(unsigned char x)
{
return fcos(x - 2);
}
2017-04-20 14:37:02 +02:00
#define MAX_TURN 6
2017-04-20 12:44:09 +02:00
float cos(float x)
{
2017-04-20 14:37:02 +02:00
unsigned int a = 8 * MAX_TURN - 6;
unsigned int b = 2 * MAX_TURN * (2 * MAX_TURN - 1); // 2n(2n-1)
2017-04-20 12:44:09 +02:00
float s = 1;
2017-04-20 14:37:02 +02:00
unsigned short i;
2017-04-20 12:44:09 +02:00
2017-04-20 14:37:02 +02:00
x = x * x; // x²
2017-04-20 12:44:09 +02:00
for(i = 0; i < MAX_TURN; i++)
{
s = 1 - x * s / b;
b -= a;
a -= 8;
}
return s;
}
float sin(float x)
{
return cos(x - pi_2);
}