_point.h
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 6k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _point.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. // generic points, having coordinates of type T
  12. // T must support the following operators and functions:
  13. //
  14. // T::operator+, T::operator-, T::operator*, T::operator/
  15. // T::operator+=, T::operator-=, T::operator*=, T::operator/=
  16. // T::operator=, T::operator==
  17. // T::operator<, T::operator>
  18. // T::operator<<, T::operator>>
  19. // sqrt(), sin(), cos(), acos()
  20. #ifndef LEDA_POINT_H
  21. #define LEDA_POINT_H
  22. #include "vector.h"
  23. #include "ctype.h"
  24. //----------------------------------------------------------------------
  25. // points
  26. //----------------------------------------------------------------------
  27. template<class T> class point
  28. {
  29.   friend class segment;
  30.   friend class line;
  31.   friend class circle;
  32.    
  33.   T x;
  34.   T y;
  35.     
  36. public:
  37.   point();
  38.   point(const T&, const T&);
  39.   point(const vector<T>&);
  40.   point(const point<T>&);
  41.   ~point();
  42.   point<T>& operator=(const point<T>&);
  43. // operator vector<T>();
  44.   T xcoord() const;
  45.   T ycoord() const;
  46.   T angle(const point<T>&, const point<T>&) const;
  47.   T angle() const;
  48.   T distance(const point<T>&) const;
  49.   T distance() const;
  50.   point<T> translate(const T&, const T&) const;
  51.   point<T> translate(const vector<T>&) const;
  52.   point<T> rotate(point<T>, const T&) const;
  53.   point<T> rotate(const T&) const;
  54.   point<T> operator+(const vector<T>&) const;
  55.   point<T> operator-(const vector<T>&) const;
  56.   friend point<T> operator-(const point<T>&); // unary minus
  57.   friend int operator==(const point<T>&, const point<T>&);
  58.   friend int operator!=(const point<T>&, const point<T>&);
  59.   friend ostream& operator<<(ostream&, const point<T>&);
  60.   friend istream& operator>>(istream&, point<T>&);
  61.   friend void Print(const point<T>&, ostream&);
  62.   friend void Read(point<T>&,  istream&);
  63. /*
  64.   friend int compare(const point<T>&, const point<T>&);
  65. */
  66. };
  67. template<class T>
  68. point<T>::point()
  69. { x = 0; y = 0; };
  70. template<class T>
  71. point<T>::point(const T& px, const T& py)
  72. { x = px; y = py; };
  73. template<class T>
  74. point<T>::point(const vector<T>& v)
  75. {
  76.   if (v.dim() != 2)
  77.     error_handler(1,"point: vector has dimension other than 2");
  78.   x = v[0]; y = v[1];
  79. };
  80. template<class T>
  81. point<T>::point(const point<T>& p)
  82. { x = p.x; y = p.y; };
  83. template<class T>
  84. point<T>::~point()
  85. {};
  86. template<class T> point<T>&
  87. point<T>::operator=(const point<T>& p)
  88. { x = p.x; y = p.y; return *this; };
  89. template<class T> T
  90. point<T>::xcoord() const
  91. { return x; };
  92. template<class T> T
  93. point<T>::ycoord() const
  94. { return y; };
  95. template<class T> T
  96. point<T>::angle(const point<T>& q, const point<T>& r) const
  97. {
  98.   T dx  = q.x - x,
  99.     dy  = q.y - y,
  100.     dxs = r.x - q.x,
  101.     dys = r.y - q.y; 
  102.   
  103.   T norm = sqrt((dx * dx + dy * dy) * (dxs * dxs + dys * dys)),
  104.     cosphi = (dx * dxs + dy * dys) / norm;
  105.   if (cosphi >=  1) return 0;
  106.   if (cosphi <= -1) return M_PI;
  107.   
  108.   T phi = acos(cosphi);
  109.   if (dx * dys > dy *dxs) return phi;
  110.   else return -phi;
  111. };
  112. template<class T> T
  113. point<T>::angle() const
  114. {
  115.   point<T> x_line(1,0);
  116.   return -(x_line.angle(point<T>(0,0),*this));
  117. };
  118. template<class T> T
  119. point<T>::distance(const point<T>& p) const
  120. {
  121.   T dx = p.x - x,
  122.     dy = p.y - y;
  123.   return sqrt(dx * dx + dy * dy);
  124. };
  125. template<class T> T
  126. point<T>::distance() const
  127. { return distance(point<T>(0,0)); };
  128. template<class T> point<T>
  129. point<T>::translate(const T& alpha, const T& d) const
  130. {
  131.   T cosalpha = cos(alpha),
  132.     sinalpha = sin(alpha);
  133.   return point<T>(x + cosalpha * d, y + sinalpha * d);
  134. };
  135. template<class T> point<T>
  136. point<T>::translate(const vector<T>& v) const
  137. { return point<T>(x + v[0], y + v[1]); };
  138. template<class T> point<T>
  139. point<T>::rotate(point<T> center, const T& alpha) const
  140. {
  141.   if (*this == center) return *this;
  142.   T cosalpha = cos(alpha),
  143.     sinalpha = sin(alpha),
  144.     xdiff = x - center.x,
  145.     ydiff = y - center.y;
  146.   return point<T>(cosalpha * xdiff - sinalpha * ydiff + center.x,
  147.                   sinalpha * xdiff + cosalpha * ydiff + center.y);
  148. };
  149. template<class T> point<T>
  150. point<T>::rotate(const T& alpha) const
  151. { return rotate(point<T>(0,0), alpha); };
  152. template<class T> point<T>
  153. point<T>::operator+(const vector<T>& v) const
  154. { return translate(v); };
  155. template<class T> point<T>
  156. point<T>::operator-(const vector<T>& v) const
  157. { return translate(-v); };
  158. template<class T> point<T>
  159. operator-(const point<T>& v)
  160. { return point<T>(-v.x,-v.y); };
  161. template<class T> int
  162. operator==(const point<T>& p, const point<T>& q)
  163. { return ((p.x == q.x) && (p.y == q.y)); };
  164. template<class T> int
  165. operator!=(const point<T>& p, const point<T>& q)
  166. { return !(p == q); };
  167. template<class T> ostream&
  168. operator<<(ostream& out, const point<T>& p)
  169. {
  170.   out << "(" << p.x << "," << p.y << ")";
  171.   return out;
  172. };
  173. template<class T> istream&
  174. operator>>(istream& in, point<T>& p) 
  175. { // syntax: {(} x {,} y {)}
  176.   T x,y; 
  177.   char c;
  178.   do in.get(c); while (in && isspace(c));
  179.   if (!in) return in;
  180.   if (c != '(') in.putback(c);
  181.   in >> x;
  182.   do in.get(c); while (isspace(c));
  183.   if (c != ',') in.putback(c);
  184.   in >> y; 
  185.   do in.get(c); while (c == ' ');
  186.   if (c != ')') in.putback(c);
  187.   p = point<T>(x,y); 
  188.   return in; 
  189. };
  190. template<class T> void
  191. Print(const point<T>& p, ostream& out)
  192. { out << p; }; 
  193. template<class T> void
  194. Read(point<T>& p,  istream& in)
  195. { in >> p; };
  196. /*
  197. template<class T> int
  198. compare(const point<T>& a, const point<T>& b)
  199. {
  200.   int r = compare(a.x, b.x);
  201.   if (r == 0) return compare(a.y, b.y);
  202.   return r;
  203. };
  204. */
  205. #endif