home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / delaunay / geom2d.h next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  3.2 KB  |  172 lines

  1. #ifndef GEOM2D_H
  2. #define GEOM2D_H
  3.  
  4. #include <math.h>
  5. #include <iostream.h>
  6.  
  7. #ifndef ABS
  8. #define ABS(a)    ((a) >= 0 ? (a) : -(a))
  9. #endif
  10.  
  11. #ifndef MAX
  12. #define MAX(a, b)    ((a) >= (b) ? (a) : (b))
  13. #define MIN(a, b)    ((a) <= (b) ? (a) : (b))
  14. #endif
  15.  
  16. #ifndef TRUE
  17. #define FALSE 0
  18. #define TRUE  1
  19. #endif
  20.  
  21. #define EPS 1e-6
  22.  
  23. typedef double    Real;
  24.  
  25. class Vector2d {
  26. public:
  27.     Real x, y;
  28.     Vector2d()                    { x = 0; y = 0; }
  29.     Vector2d(Real a, Real b)    { x = a; y = b; }
  30.     Real norm() const;
  31.     void normalize();
  32.     Vector2d operator+(const Vector2d&) const;
  33.     Vector2d operator-(const Vector2d&) const;
  34.     friend Vector2d operator*(Real, const Vector2d&);
  35.     friend Real dot(const Vector2d&, const Vector2d&);
  36.     friend istream& operator>>(istream&, Vector2d&);
  37.     friend ostream& operator<<(ostream&, const Vector2d&);
  38. };
  39.  
  40. class Point2d {
  41. public:
  42.     Real x, y;
  43.     Point2d()                    { x = 0; y = 0; }
  44.     Point2d(Real a, Real b)        { x = a; y = b; }
  45.     Point2d(const Point2d& p)    { *this = p; }
  46.     Point2d operator+(const Vector2d&) const;
  47.     Vector2d operator-(const Point2d&) const;
  48.     int operator==(const Point2d&) const;
  49.     friend istream& operator>>(istream&, Point2d&);
  50.     friend ostream& operator<<(ostream&, const Point2d&);
  51. };
  52.  
  53. class Line {
  54. public:
  55.     Line()    {}
  56.     Line(const Point2d&, const Point2d&);
  57.     Real eval(const Point2d&) const;
  58.     int classify(const Point2d&) const;
  59. private:
  60.     Real a, b, c;
  61. };
  62.  
  63. // Vector2d:
  64.  
  65. inline Real Vector2d::norm() const
  66. {
  67.     return sqrt(x * x + y * y);
  68. }
  69.  
  70. inline void Vector2d::normalize()
  71. {
  72.     Real len;
  73.  
  74.     if ((len = sqrt(x * x + y * y)) == 0.0)
  75.         cerr << "Vector2d::normalize: Division by 0\n";
  76.     else {
  77.         x /= len;
  78.         y /= len;
  79.     }
  80. }
  81.  
  82. inline Vector2d Vector2d::operator+(const Vector2d& v) const
  83. {
  84.     return Vector2d(x + v.x, y + v.y);
  85. }
  86.  
  87. inline Vector2d Vector2d::operator-(const Vector2d& v) const
  88. {
  89.     return Vector2d(x - v.x, y - v.y);
  90. }
  91.  
  92. inline Vector2d operator*(Real c, const Vector2d& v)
  93. {
  94.     return Vector2d(c * v.x, c * v.y);
  95. }
  96.  
  97. inline Real dot(const Vector2d& u, const Vector2d& v)
  98. {
  99.     return u.x * v.x + u.y * v.y;
  100. }
  101.  
  102. inline ostream& operator<<(ostream& os, const Vector2d& v)
  103. {
  104.     os << '(' << v.x << ", " << v.y << ')';
  105.     return os;
  106. }
  107.  
  108. inline istream& operator>>(istream& is, Vector2d& v)
  109. {
  110.     is >> v.x >> v.y;
  111.     return is;
  112. }
  113.  
  114. // Point2d:
  115.  
  116. inline Point2d Point2d::operator+(const Vector2d& v) const
  117. {
  118.     return Point2d(x + v.x, y + v.y);
  119. }
  120.  
  121. inline Vector2d Point2d::operator-(const Point2d& p) const
  122. {
  123.     return Vector2d(x - p.x, y - p.y);
  124. }
  125.  
  126. inline int Point2d::operator==(const Point2d& p) const
  127. {
  128.     return ((*this - p).norm() < EPS);
  129. }
  130.  
  131. inline istream& operator>>(istream& is, Point2d& p)
  132. {
  133.     is >> p.x >> p.y;
  134.     return is;
  135. }
  136.  
  137. inline ostream& operator<<(ostream& os, const Point2d& p)
  138. {
  139.     os << '(' << p.x << ", " << p.y << ')';
  140.     return os;
  141. }
  142.  
  143. // Line:
  144.  
  145. inline Line::Line(const Point2d& p, const Point2d& q)
  146. // Computes the normalized line equation through the
  147. // points p and q.
  148. {
  149.     Vector2d t = q - p;
  150.     Real len = t.norm();
  151.     a =   t.y / len;
  152.     b = - t.x / len;
  153.     c = -(a*p.x + b*p.y);
  154. }
  155.  
  156. inline Real Line::eval(const Point2d& p) const
  157. // Plugs point p into the line equation.
  158. {
  159.     return (a * p.x + b* p.y + c);
  160. }
  161.  
  162. inline int Line::classify(const Point2d& p) const
  163. // Returns -1, 0, or 1, if p is to the left of, on,
  164. // or right of the line, respectively.
  165. {
  166.     Real d = eval(p);
  167.     return (d < -EPS) ? -1 : (d > EPS ? 1 : 0);
  168. }
  169.  
  170. #endif
  171.  
  172.