Programming/MFC-C++

atan2 Look-up Table 만들기

빠릿베짱이 2015. 5. 18. 19:03
반응형
Generation Look-up Table
void AtanInit()
{
	const float AtanTableDelta = 1.0f / (float)360;
	for (int i = 0; i < 360; ++i) {
		AtanTable[i] = atanf((float)i * AtanTableDelta)*1000;
	}
}

Sub Function
static float AtanLookup2(int y, int x) 
{
	assert(y > 0.0f);
	assert(x > 0.0f);
	assert(y < x);
	//360은 룩업 테이블 수
	const int index = y*360/x;
	return AtanTable[index];    
}

Atan2 Fuction
int Atan2(int y, int x) 
{
	// Handle x == 0 or x == -0
	// (See atan2(3) for specification of sign-bit handling.)
	if (x == 0) {
		if (y > 0) {
			return INT_PI_2;//3.141/2
		}
		else if (y < 0) {
			return -INT_PI_2;
		}
		else if (x < 0) {
			return y < 0 ? -INT_PI : INT_PI;
		}
		else {
			return y < 0? -0 : 0;
		}
	}

	// Handle y == 0, x != 0
	if (y == 0) {
		return (x > 0)? 0 : INT_PI;
	}

	// Handle y == x
	if (y == x) {
		return (x > 0.0f)? INT_PI_4 : -INT_PI_42;
	}

	// Handle y == -x
	if (y == -x) {
		return (x > 0.0f)? -INT_PI_4 : INT_PI_42;
	}

	// For other cases, determine quadrant and do appropriate lookup and calculation
	bool right = (x > 0.0f);
	bool top = (y > 0.0f);
	if (right && top) {
		// First quadrant
		if (y < x) {
			return AtanLookup2(y, x);
		}
		else {
			return INT_PI_2 - AtanLookup2(x, y);
		}
	}
	else if (!right && top) {
		// Second quadrant
		const int posx = ABS(x);
		if (y < posx) {
			return INT_PI - AtanLookup2(y, posx);
		}
		else {
			return INT_PI_2 + AtanLookup2(posx, y);
		}
	}
	else if (!right && !top) {
		// Third quadrant
		const int posx = ABS(x);
		const int posy = ABS(y);
		if (posy < posx) {
			return -INT_PI + AtanLookup2(posy, posx);
		}
		else {
			return -INT_PI_2 - AtanLookup2(posx, posy);
		}
	}
	else { // right && !top
		// Fourth quadrant
		const int posy = ABS(y);
		if (posy < x) {
			return -AtanLookup2(posy, x);
		}
		else {
			return -INT_PI_2 + AtanLookup2(x, posy);
		}
	}

	return 0.0f;
}


반응형