-
Notifications
You must be signed in to change notification settings - Fork 0
/
degutil.c
105 lines (95 loc) · 1.86 KB
/
degutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* $Id: degutil.c 264 2005-11-06 17:43:44Z mrbrown $
*
* Copyright 2003 Kenta Cho. All rights reserved.
*/
/**
* Changing the cordinate into the angle.
*
* @version $Revision: 264 $
*/
#include <math.h>
#include "degutil.h"
static int tantbl[TAN_TABLE_SIZE+2];
int sctbl[SC_TABLE_SIZE+SC_TABLE_SIZE/4];
void initDegutil() {
int i, d = 0;
double od = 6.28/DIV;
for ( i=0 ; i<TAN_TABLE_SIZE ; i++ ) {
while ( (int)(sin(d*od)/cos(d*od)*TAN_TABLE_SIZE)<i ) d++;
tantbl[i] = d;
}
tantbl[TAN_TABLE_SIZE] = tantbl[TAN_TABLE_SIZE+1] = 128;
for ( i=0 ; i<SC_TABLE_SIZE+SC_TABLE_SIZE/4 ; i++ ) {
sctbl[i] = (int)(sin(i*(6.28/SC_TABLE_SIZE))*256);
}
}
int getDeg(int x, int y) {
int tx, ty;
int f, od, tn;
if ( x==0 && y==0 ) {
return(512);
}
if ( x < 0 ) {
tx = -x;
if ( y < 0 ) {
ty = -y;
if ( tx > ty ) {
f = 1;
od = DIV*3/4; tn = ty*TAN_TABLE_SIZE/tx;
} else {
f = -1;
od = DIV; tn = tx*TAN_TABLE_SIZE/ty;
}
} else {
ty = y;
if ( tx > ty ) {
f = -1;
od = DIV*3/4; tn=ty*TAN_TABLE_SIZE/tx;
} else {
f=1;
od = DIV/2; tn=tx*TAN_TABLE_SIZE/ty;
}
}
} else {
tx = x;
if ( y < 0 ) {
ty = -y;
if ( tx > ty ) {
f = -1;
od = DIV/4; tn = ty*TAN_TABLE_SIZE/tx;
} else {
f = 1;
od = 0; tn = tx*TAN_TABLE_SIZE/ty;
}
} else {
ty = y;
if ( tx > ty ) {
f = 1;
od = DIV/4; tn = ty*TAN_TABLE_SIZE/tx;
} else {
f = -1;
od = DIV/2; tn = tx*TAN_TABLE_SIZE/ty;
}
}
}
return((od+tantbl[tn]*f)&(DIV-1));
}
int getDistance(int x, int y) {
if ( x < 0 ) x = -x;
if ( y < 0 ) y = -y;
if ( x > y ) {
return x + (y>>1);
} else {
return y + (x>>1);
}
}
float getDistanceFloat(float x, float y) {
if ( x < 0 ) x = -x;
if ( y < 0 ) y = -y;
if ( x > y ) {
return x + (y/2);
} else {
return y + (x/2);
}
}