#include "calc.h"

LCARD	delta_dist ;

/*  サインテーブル  */
long int sin_tbl[256] = {
	     0,   1608,   3215,   4821,   6423,   8022,   9616,  11204, 
	 12785,  14359,  15923,  17479,  19024,  20557,  22078,  23586, 
	 25079,  26557,  28020,  29465,  30893,  32302,  33692,  35061, 
	 36409,  37736,  39039,  40319,  41575,  42806,  44011,  45189, 

	 46340,  47464,  48558,  49624,  50659,  51665,  52638,  53581, 
	 54491,  55368,  56212,  57021,  57797,  58538,  59243,  59913, 
	 60547,  61144,  61705,  62228,  62714,  63162,  63571,  63943, 
	 64276,  64571,  64826,  65043,  65220,  65358,  65457,  65516, 

	 65536,  65516,  65457,  65358,  65220,  65043,  64826,  64571, 
	 64276,  63943,  63571,  63162,  62714,  62228,  61705,  61144, 
	 60547,  59913,  59243,  58538,  57797,  57022,  56212,  55368, 
	 54491,  53581,  52639,  51665,  50660,  49624,  48559,  47464, 

	 46340,  45189,  44011,  42806,  41575,  40319,  39039,  37736, 
	 36409,  35061,  33692,  32302,  30893,  29465,  28020,  26558, 
	 25079,  23586,  22078,  20557,  19024,  17479,  15924,  14359, 
	 12785,  11204,   9616,   8022,   6423,   4821,   3215,   1608, 

	     0,  -1609,  -3216,  -4821,  -6424,  -8023,  -9616, -11204, 
	-12786, -14359, -15924, -17480, -19024, -20558, -22079, -23586, 
	-25080, -26558, -28021, -29466, -30894, -32303, -33693, -35062, 
	-36410, -37737, -39040, -40320, -41576, -42807, -44012, -45190, 

	-46340, -47465, -48559, -49625, -50660, -51665, -52639, -53582, 
	-54492, -55369, -56212, -57022, -57798, -58539, -59244, -59914, 
	-60548, -61145, -61705, -62229, -62714, -63162, -63572, -63944, 
	-64277, -64572, -64827, -65044, -65221, -65359, -65458, -65517, 

	-65536, -65517, -65458, -65359, -65221, -65044, -64827, -64572, 
	-64277, -63944, -63573, -63163, -62715, -62229, -61706, -61145, 
	-60548, -59914, -59244, -58539, -57798, -57023, -56213, -55369, 
	-54492, -53582, -52640, -51666, -50661, -49625, -48560, -47465, 

	-46340, -45190, -44012, -42807, -41576, -40321, -39040, -37737, 
	-36411, -35062, -33693, -32303, -30894, -29467, -28021, -26559, 
	-25080, -23587, -22079, -20558, -19025, -17480, -15925, -14360, 
	-12786, -11205,  -9617,  -8023,  -6424,  -4822,  -3217,  -1609
};

/*  アークタンジェントテーブル  */
unsigned int atn_tbl[32] = {
	   805,  2414,  4026,  5643,  7268,  8901, 10545, 12202,
	 13874, 15564, 17273, 19006, 20762, 22546, 24360, 26209,
	 28093, 30018, 31987, 34002, 36071, 38195, 40382, 42637,
	 44963, 47369, 49864, 52452, 55144, 57951, 60880, 63947,
};

int delta_ang( int dx, int dy )
{
	register int ang ;
	LCARD	ratio, x, y ;
	int		s ;

	delta_dist = 0 ;
	if( dx == 0 && dy == 0 ) return 0 ;

	x = ABS( dx ), y = ABS( dy );
	ratio = ( x > y )? ( y<<16 )/x : ( x<<16 )/y ;

	for( ang = 0 ; ang <= 31 && atn_tbl[ang] <= ratio ; ang++ );

	switch( (( 0 <= dx )<<1 )+( 0 <= dy ) ){
	case 0: ang = ( x > y )? 192 + ang :   0 - ang ; break ; /*  左上  */
	case 1: ang = ( x > y )? 192 - ang : 128 + ang ; break ; /*  左下  */
	case 2: ang = ( x > y )?  64 - ang :   0 + ang ; break ; /*  右上  */
	case 3: ang = ( x > y )?  64 + ang : 128 - ang ; break ; /*  右下  */
	}
	ang &= 255, x <<= 16, y <<= 16 ;
	if( y < x ){
		if( ( s = SIN(ang) )< 0 ) s = -s ; 
		delta_dist = ( x / s )+( 8 <= ( x % s )*10 / s );/*  七捨八入  */
	}else{
		if( ( s = COS(ang) )< 0 ) s = -s ; 
		delta_dist = ( y / s )+( 8 <= ( y % s )*10 / s );
	}
	return ang ;
}
