반응형
Generation Look-up Table
Sub Function
Atan2 Fuction
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; }
반응형
'Programming > MFC-C++' 카테고리의 다른 글
Wave PCM Data Drawing with C++ (0) | 2015.06.09 |
---|---|
ShellExecute 파일 실행, 프로세스 종료 될 때까지 기다림 (0) | 2015.05.29 |
텍스트 파일(Text File) 인코딩 포멧 알아내기 (0) | 2015.05.18 |
Visual Studio 스타일 & Tip (0) | 2015.05.16 |
Thread Pool C++ 11 (0) | 2015.05.14 |