Effective implementation of algorithms (Master Thesis)
Effective and error-free implementation of algorithms
|
00001 #ifndef H_GEOMETRY_TWO_D_ANGLE 00002 #define H_GEOMETRY_TWO_D_ANGLE 00003 00007 #include "signum.h" 00008 #include "point.h" 00009 00010 namespace geometry { 00011 namespace two_d { 00012 00019 enum Quadrant { 00021 CENTER = 0, 00023 TOP_RIGHT = 1, 00025 TOP_LEFT = 2, 00027 BOTTOM_LEFT = 3, 00029 BOTTOM_RIGHT = 4, 00030 }; 00031 00032 template <typename T> 00033 Quadrant getQuadrant(const Point<T> point) { 00034 int a = 1 + signum(point.x()); 00035 int b = 1 + signum(point.y()); 00036 00037 // check if signum is correct 00038 assert(0 <= a && a <= 2); 00039 assert(0 <= b && b <= 2); 00040 00041 Quadrant data[3][3] = { 00042 {BOTTOM_LEFT, BOTTOM_LEFT, TOP_LEFT}, 00043 {BOTTOM_RIGHT, CENTER, TOP_LEFT}, 00044 {BOTTOM_RIGHT, TOP_RIGHT, TOP_RIGHT} 00045 }; 00046 00047 return data[a][b]; 00048 } 00049 00055 template <typename T> 00056 bool angleLess(const Point<T> point1, const Point<T> point2) { 00057 Quadrant q1 = getQuadrant(point1); 00058 Quadrant q2 = getQuadrant(point2); 00059 if (q1 != q2) { 00060 return q1 < q2; 00061 } 00062 // same quadrant, we can decide by cross product. 00063 return point1.cross(point2) > 0; 00064 } 00065 00066 } // namespace two_d 00067 } // namespace geometry 00068 00069 #endif