Effective implementation of algorithms (Master Thesis)
Effective and error-free implementation of algorithms
|
00001 #ifndef H_GEOMETRY_TWO_D_POINT 00002 #define H_GEOMETRY_TWO_D_POINT 00003 #include <complex> 00004 00005 namespace geometry { 00006 namespace two_d { 00007 00008 template<typename T> 00009 class Point { 00010 public: 00011 Point(); 00012 Point(T x,T y); 00013 Point(std::complex<T>); 00014 00015 void operator=(const Point<T>& b) { point = b.point; } 00016 00017 T dot(const Point<T> &b) const { 00018 return real(conj(point) * b._point()); 00019 } 00020 00021 T cross(const Point<T>&b) const { 00022 return imag(conj(point) * b._point()); 00023 } 00024 00025 T x() const { 00026 return point.real(); 00027 } 00028 00029 T y() const { 00030 return point.imag(); 00031 } 00032 00033 void swap() { 00034 point = std::complex<T>(point.imag(), point.real()); 00035 } 00036 00037 std::complex<T> inline _point() const { return point; } 00038 00039 // this will allow casting to long double. 00040 // We will not support other castings as it may be undesired 00041 // in conversions 00042 operator Point<long double>() const { 00043 return Point<long double>(x(), y()); 00044 } 00045 private: 00046 std::complex<T> point; 00047 }; 00048 00049 00050 template<typename T> 00051 Point<T>::Point():point(0, 0) { 00052 } 00053 00054 template<typename T> 00055 Point<T>::Point(T x, T y):point(x,y) { 00056 } 00057 00058 template<typename T> 00059 Point<T>::Point(std::complex<T> p):point(p) { 00060 } 00061 00062 template<typename T> 00063 bool operator==(const Point<T>&a, const Point<T>& b) { 00064 return a._point() == b._point(); 00065 } 00066 00067 template<typename T> 00068 bool operator!=(const Point<T>&a, const Point<T>& b) { 00069 return a._point() != b._point(); 00070 } 00071 00072 template<typename T> 00073 Point<T> operator+(const Point<T> &a, const Point<T>& b) { 00074 return Point<T>(a._point() + b._point()); 00075 } 00076 00077 template<typename T> 00078 Point<T> operator-(const Point<T> &a, const Point<T>& b) { 00079 return Point<T>(a._point() - b._point()); 00080 } 00081 00082 template<typename T> 00083 Point<T> operator-(const Point<T> &a) { 00084 return a * -1; 00085 } 00086 00087 template<typename T> 00088 Point<T> operator*(const Point<T> &a, T scalar) { 00089 return Point<T>(a._point() * scalar); 00090 } 00091 00092 template<typename T> 00093 Point<T> operator/(const Point<T> &a, T scalar) { 00094 return Point<T>(a._point() / scalar); 00095 } 00096 00097 } // namespace two_d 00098 } // namespace geometry 00099 00100 #endif