rat_point.h
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:7k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  rat_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. #ifndef LEDA_RAT_POINT_H
  12. #define LEDA_RAT_POINT_H
  13. #include <math.h>
  14. #include <LEDA/integer.h>
  15. class rat_point;
  16. class rat_segment;
  17. //------------------------------------------------------------------------------
  18. // rat_points
  19. //------------------------------------------------------------------------------
  20. class rat_point_rep  : public handle_rep {
  21. friend class rat_point;
  22. friend class rat_segment;
  23.    
  24.    integer x;
  25.    integer y;
  26.    integer w;
  27.    double  xd;
  28.    double  yd;
  29.    double  wd;
  30. public:
  31.    rat_point_rep() : x(0), y(0), w(1)
  32.    { xd = 0;
  33.      yd = 0;
  34.      wd = 1;
  35.     }
  36.    rat_point_rep(integer a, integer b) : x(a), y(b), w(1)
  37.    { xd = a.todouble(); 
  38.      yd = b.todouble(); 
  39.      wd = 1;
  40.     }
  41.    rat_point_rep(integer a, integer b, integer c) : x(a), y(b), w(c)
  42.    { xd = a.todouble(); 
  43.      yd = b.todouble(); 
  44.      wd = c.todouble();
  45.     }
  46.   ~rat_point_rep() {}
  47. friend int orientation(const rat_point&, const rat_point&, const rat_point&);
  48.    
  49. };
  50. /*{Manpage {rat_point} {} {Rational Points} }*/
  51. class rat_point  : public handle_base {
  52. /*{Mdefinition
  53. An instance of the data type $rat_point$ is a point with rational coordinates 
  54. in the two-dimensional plane. A point $(a,b)$ is represented by homogeneous
  55. coordinates $(x,y,w)$ of arbitrary length integers (see ref{Integers of 
  56. Arbitrary Length}) such that $a = x/w$ and $b = y/w$. }*/
  57.  friend class rat_segment;
  58.  rat_point_rep* ptr() const { return (rat_point_rep*)PTR; } 
  59. public:
  60. static int orient_count;
  61. static int exact_orient_count;
  62. static int cmp_count;
  63. static int exact_cmp_count;
  64. static int use_filter;
  65. /*{Mcreation p}*/
  66. rat_point() { PTR = new rat_point_rep; }
  67. /*{Mcreate introduces a variable var of type name
  68. initialized to the point $(0,0)$.}*/
  69.  rat_point(integer a, integer b)       { PTR = new rat_point_rep(a,b); }
  70. /*{Mcreate introduces a variable var of type name
  71. initialized to the point $(a,b)$.}*/
  72.  rat_point(integer x, integer y, integer w){ PTR = new rat_point_rep(x,y,w); }
  73. /*{Mcreate introduces a variable var of type name
  74. initialized to the point with homogeneous coordinates$(x,y,w)$.}*/
  75.  rat_point(const rat_point& p) : handle_base(p) {}
  76. ~rat_point() {}
  77.  rat_point& operator=(const rat_point& p) 
  78.  { handle_base::operator=(p); return *this; }
  79. /*{Moperations 2 3.5 }*/
  80. double xcoord() const { return ptr()->xd/ptr()->wd;}
  81. /*{Mop     returns a double precision floating point approximation of the 
  82.             $x$-coordinate of var.}*/
  83. double ycoord() const { return ptr()->yd/ptr()->wd;}
  84. /*{Mop     returns a double precision floating point approximation of the 
  85.             $y$-coordinate of var.}*/
  86. integer X() const { return ptr()->x; }
  87. /*{Mop     returns the first homogeneous coordinate of var.}*/
  88. integer Y() const { return ptr()->y; }
  89. /*{Mop     returns the second homogeneous coordinate of var.}*/
  90. integer W() const { return ptr()->w; }
  91. /*{Mop     returns the third homogeneous coordinate of var.}*/
  92. double XD() const { return ptr()->xd; }
  93. /*{Mop     returns a floating point approximation of the first homogeneous 
  94.             coordinate of var.}*/
  95. double YD() const { return ptr()->yd; }
  96. /*{Mop     returns a floating point approximation of the second homogeneous 
  97.             coordinate of var.}*/
  98. double WD() const { return ptr()->wd; }
  99. /*{Mop     returns a floating point approximation of the third homogeneous 
  100.             coordinate of var.}*/
  101. rat_point rotate90(const rat_point& q) const;
  102. /*{Mopl    returns var rotated by 90 degrees about $q$. }  */
  103. rat_point rotate90() const;
  104. /*{Mop     returns var rotated by 90 degrees about the origin. }  */
  105. rat_point translate(const rat_point& p, int i) const;
  106. /*{Mopl    returns var translated by ... }  */
  107. friend bool identical(const rat_point& p, const rat_point& q)
  108. { return p.ptr() == q.ptr(); }
  109. /*{Mfuncl  Test for identity.}*/
  110. friend bool operator==(const rat_point& p, const rat_point& q)
  111. { return (identical(p,q)) || rat_point::cmp(p,q) == 0; }
  112. /*{Mbinopfunc  Test for equality.}*/
  113. friend bool operator!=(const rat_point& p, const rat_point& q)
  114. { return (!identical(p,q)) && rat_point::cmp(p,q) != 0; }
  115. /*{Mbinopfunc  Test for inequality.}*/
  116. friend ostream& operator<<(ostream& O, const rat_point& p) ;
  117. /*{Mbinopfunc  writes the homogeneous coordinates $(x,y,w)$ of var to 
  118.                 output stream $O$.}*/
  119. friend istream& operator>>(istream& I, rat_point& p) ;
  120. /*{Mbinopfunc  reads the homogeneous coordinates $(x,y,w)$ of var from 
  121.                 input stream $I$.}*/
  122. friend int orientation(const rat_point& a, const rat_point& b, const rat_point& c);
  123. /*{Mfuncl  computes the orientation of points $a$, $b$, $c$, i.e.,
  124.            the sign of the determinant\
  125.            [ leftLvert begin{array}{ccc} a_x & a_y & a_w\
  126.                                         b_x & b_y & b_w\
  127.                                         c_x & c_y & c_w
  128.                        end{array} rightLvert ] 
  129.            }*/
  130. static int cmp(const rat_point&, const rat_point&);
  131. };
  132. extern 
  133. double area(const rat_point& a, const rat_point& b, const rat_point& c);
  134. /*{Mfuncl computes the signed area of the triangle determined by $a$,$b$,$c$,
  135.            positive if $orientation(a,b,c) > 0$ and negative otherwise. }*/
  136. extern
  137. int incircle(const rat_point& a, const rat_point& b, const rat_point& c,
  138.                                                      const rat_point& d);
  139. /*{Mfuncl returns $+1$ if point $d$ lies in the interior of the circle
  140.            through points $a$, $b$, and $c$, $0$ if $a$,$b$,$c$,and $d$ are
  141.            cocircular, and $-1$ otherwise. }*/
  142. inline int compare(const rat_point& a, const rat_point& b)
  143. {  return (identical(a,b))  ? 0 : rat_point::cmp(a,b); }
  144. inline void Print(const rat_point& p, ostream& out) { out << p; } 
  145. inline void Read(rat_point& p,  istream& in)        { in >> p; }
  146. inline bool collinear(const rat_point& a,const rat_point& b,const rat_point& c)
  147. { return orientation(a,b,c) == 0; }
  148. /*{Mfuncl  returns true if points $a$, $b$, $c$ are collinear, i.e.,
  149.            $orientation(a,b,c) = 0$, and false otherwise. }*/
  150. inline bool right_turn(const rat_point& a,const rat_point& b,const rat_point& c)
  151. { return orientation(a,b,c) < 0; }
  152. /*{Mfuncl  returns true if points $a$, $b$, $c$ form a righ turn, i.e.,
  153.            $orientation(a,b,c) > 0$, and false otherwise. }*/
  154. inline bool left_turn(const rat_point& a,const rat_point& b,const rat_point& c)
  155. { return orientation(a,b,c) > 0; }
  156. /*{Mfuncl  returns true if points $a$, $b$, $c$ form a left turn, i.e.,
  157.            $orientation(a,b,c) < 0$, and false otherwise. }*/
  158. #endif
  159.