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

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  polygon.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_POLYGON_H
  12. #define LEDA_POLYGON_H
  13. #include <LEDA/point.h>
  14. #include <LEDA/segment.h>
  15. #include <LEDA/line.h>
  16. //------------------------------------------------------------------------------
  17. // polygons
  18. //------------------------------------------------------------------------------
  19. class polygon_rep : public handle_rep {
  20. friend class polygon;
  21.   list<segment> seg_list;
  22. public:
  23.   polygon_rep() {}
  24.   polygon_rep(const list<segment>& L) : seg_list(L) {}
  25.  ~polygon_rep() {}
  26.    
  27. };
  28. /*{Manpage {polygon} {} {Polygons} }*/
  29. class list_polygon_;
  30. class polygon   : public handle_base 
  31. {
  32. /*{Mdefinition
  33. An instance $P$ of the data type $polygon$ is a simple polygon
  34. in the two-dimensional plane defined by the sequence of its vertices
  35. in clockwise order. The number of vertices is called the size
  36. of $P$. A polygon with empty vertex sequence is called empty.}*/
  37. polygon_rep* ptr() const { return (polygon_rep*)PTR; }
  38. polygon(const list<segment>& sl) { PTR = new polygon_rep(sl); }
  39. polygon(const list<point>& pl, bool check);
  40. double compute_area(const list<segment>&) const;
  41. public:
  42. /*{Mcreation P }*/
  43. polygon(const list<point>& pl);
  44. /*{Mcreate 
  45. introduces a variable var of type name. var is initialized to the
  46. polygon with vertex sequence $pl$.\
  47. precond The vertices in $pl$ are given
  48. in clockwise order and define a simple polygon.}*/
  49. polygon() { PTR = new polygon_rep; }
  50. /*{Mcreate 
  51. introduces a variable var of type name. var is initialized to the
  52. empty polygon.}*/
  53.  polygon(const polygon& P) : handle_base(P) {}
  54. ~polygon() {}
  55.  polygon& operator=(const polygon& P) 
  56.  { handle_base::operator=(P); return *this;}
  57. /*{Moperations 2.5 4.6}*/
  58. list<point>   vertices() const;  
  59. /*{Mop     returns the vertex sequence of var.}*/
  60. list<segment> segments() const { return ptr()->seg_list; }
  61. /*{Mop     returns the sequence of bounding segments 
  62.     of var in clockwise order.}*/
  63. list<point> intersection(const segment& s) const;
  64. /*{Mopl    returns $P cap s$ as a list of points.}*/
  65. list<point> intersection(const line& l) const;
  66. /*{Mopl    returns $P cap l$ as a list of points.}*/
  67. list_polygon_ unite(const polygon& Q) const;
  68. /*
  69. list<polygon> unite(const polygon& Q) const;
  70. */
  71. /*{Mopl    returns $P cup Q$ as a list of polygons. The first polygon
  72.             in the list gives the outer boundary of the contour of the union.
  73.             Possibly following polygons define the inner boundaries (holes) 
  74.             of the contour (holes). }*/
  75. list_polygon_ intersection(const polygon& Q) const;
  76. /*
  77. list<polygon> intersection(const polygon& Q) const;
  78. */
  79. /*{Mopl    returns $P cap Q$ as a list of polygons.}*/
  80. bool        inside(const point& p) const;  
  81. /*{Mop     returns true if $p$ lies inside of $P$,
  82.     false otherwise.}*/
  83. bool        outside(const point& p) const; 
  84. /*{Mop     returns !var.inside($p$).}*/
  85. double      area() const { return compute_area(ptr()->seg_list); }
  86. /*{Mop     returns the area of $P$.}*/
  87. polygon     translate(double a, double d) const;
  88. /*{Mopl    returns the polygon created by a translation 
  89.     of var in direction $a$ by distance $d$.}*/
  90. polygon     translate(const vector& v) const;
  91. /*{Mop     returns $P+v$, i.e., the polygon created by 
  92.     translating $P$ by vector $v$.\
  93.     precond $v.dim() = 2$.}*/ 
  94. polygon     rotate(const point& q, double a) const;
  95. /*{Mopl    returns the polygon created by a rotation of 
  96.     $P$ about point $q$ by angle $a$.}*/
  97. polygon     rotate(double) const;
  98. int         size()   const  { return ptr()->seg_list.size(); }
  99. /*{Mop     returns the size of var.}*/
  100. bool        empty()  const  { return ptr()->seg_list.empty(); }
  101. /*{Mop     returns true if var is empty, false otherwise.}*/
  102. polygon operator+(const vector& v) const { return translate(v); }
  103. bool    operator==(const polygon& P) const { return P.ptr() == ptr(); }
  104. friend ostream& operator<<(ostream& out, const polygon& p);
  105. friend istream& operator>>(istream& in,  polygon& p);
  106. };
  107. inline void Print(const polygon& P, ostream& out) { out << P; } 
  108. inline void Read(polygon& P, istream& in)         { in  >> P; }
  109. struct list_polygon_: public list<polygon>
  110. {
  111.   list_polygon_(const list<polygon>& L) : list<polygon>(L) {}
  112.   list_polygon_() {}
  113.  ~list_polygon_() {}
  114. };
  115. #endif