00001
00024 #ifndef _TUPLE_H_
00025 #define _TUPLE_H_
00026
00027 #include <cstdio>
00028 #include <cmath>
00029
00030 class Tuple {
00031 double _t[2];
00032 public:
00033 Tuple(double a, double b) {
00034 _t[0] = a;
00035 _t[1] = b;
00036 }
00037
00038 Tuple() {};
00039 ~Tuple() {};
00040
00041 bool operator==(const Tuple& rhs) const {
00042 for (int x = 0; x < 2; ++x) {
00043 if (_t[x] != rhs._t[x]) return false;
00044 }
00045 return true;
00046 }
00047
00048 bool operator!=(const Tuple& rhs) const {
00049 return !(*this == rhs);
00050 }
00051
00052 bool operator<(const Tuple& rhs) const {
00053 for (int i = 0; i < 2; ++i) {
00054 if (_t[i] < rhs._t[i]) return true;
00055 else if (_t[i] > rhs._t[i]) return false;
00056 }
00057 return false;
00058 }
00059
00060 bool operator>(const Tuple& rhs) const {
00061 for (int i = 0; i < 2; ++i) {
00062 if (_t[i] > rhs._t[i]) return true;
00063 else if (_t[i] < rhs._t[i]) return false;
00064 }
00065 return false;
00066 }
00067
00068 Tuple operator+(const Tuple& rhs) const {
00069 return Tuple(_t[0]+rhs._t[0], _t[1]+rhs._t[1]);
00070 }
00071
00072 Tuple operator-(const Tuple& rhs) const {
00073 return Tuple(_t[0]-rhs._t[0], _t[1]-rhs._t[1]);
00074 }
00075
00076 Tuple operator*(double d) const {
00077 return Tuple(_t[0]*d, _t[1]*d);
00078 }
00079
00080 double operator*(const Tuple& rhs) const {
00081 return _t[0]*rhs._t[0] + _t[1]*rhs._t[1];
00082 }
00083
00084 double operator[](int i) const {
00085 return _t[i];
00086 };
00087
00088 int cmp(const Tuple& x) const {
00089 if (*this == x)
00090 return 0;
00091 if (*this > x)
00092 return 1;
00093 return -1;
00094 }
00095
00096 double distance_squared(const Tuple& p) const {
00097 double sum = 0.0;
00098 for (int i = 0; i < 2; ++i) {
00099 double d = _t[i] - p._t[i];
00100 sum += d * d;
00101 }
00102 return sum;
00103 }
00104
00105 double distance(const Tuple& p) const {
00106 return sqrt(distance_squared(p));
00107 }
00108
00109 double angle(const Tuple& a, const Tuple& b) const {
00110 Tuple vb = a - *this;
00111 Tuple vc = b - *this;
00112 double dp = vb*vc;
00113 double c = dp / sqrt(distance_squared(a) * distance_squared(b));
00114 return (180/M_PI) * acos(c);
00115 }
00116
00117 void angleCheck(const Tuple& a, const Tuple& b, bool& ob, bool& sm, double M) const {
00118 Tuple vb = a - *this;
00119 Tuple vc = b - *this;
00120 double dp = vb*vc;
00121
00122 if (dp < 0) {
00123 ob = true;
00124 return;
00125 }
00126
00127 double c = dp / sqrt(distance_squared(a) * distance_squared(b));
00128 if (c > cos(M*M_PI/180)) {
00129 sm = true;
00130 return;
00131 }
00132 return;
00133 }
00134
00135 void print(std::ostream& os) const {
00136 os << "(" << _t[0] << ", " << _t[1] << ")";
00137 }
00138
00139 static int cmp(Tuple a, Tuple b) {return a.cmp(b);}
00140 static double distance(Tuple a, Tuple b) {return a.distance(b);}
00141 static double angle(const Tuple& a, const Tuple& b, const Tuple& c) {return b.angle(a, c);}
00142 static void angleCheck(const Tuple& a, const Tuple& b, const Tuple& c, bool& ob, bool& sm, double M) { b.angleCheck(a, c, ob, sm, M); }
00143 };
00144
00145 static inline std::ostream& operator<<(std::ostream& os, const Tuple& rhs) {
00146 rhs.print(os);
00147 return os;
00148 }
00149
00150 static inline Tuple operator*(double d, Tuple rhs) {
00151 return rhs * d;
00152 }
00153
00154
00155 #endif