ubit
ugeom.hpp
1 /************************************************************************
2  *
3  * ugeom.hpp: graphics geometry
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _ugeom_hpp_
18 #define _ugeom_hpp_ 1
19 #include <ubit/uobject.hpp>
20 namespace ubit {
21 
22  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
25  class UPoint {
26  public:
27  float x, y;
28 
29  UPoint() : x(0), y(0) {}
30  UPoint(float _x, float _y) : x(_x), y(_y) {}
31 
32  float getX() const {return x;}
33  float getY() const {return y;}
34 
35  void set(float _x, float _y) {x = _x, y = _y;}
36  void set(const UPoint& p) {*this = p;}
37 
38  friend std::ostream& operator<<(std::ostream&, const UPoint&);
40 
41  friend std::istream& operator>>(std::istream&, UPoint&);
43  };
44 
45  std::ostream& operator<<(std::ostream&, const UPoint&);
47 
48  std::istream& operator>>(std::istream&, UPoint&);
50 
51 
52  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55  class UDimension {
56  public:
57  float width, height;
58 
59  UDimension() : width(0), height(0) {}
60  UDimension(float _w, float _h) : width(_w), height(_h) {}
61 
62  float getWidth() const {return width;}
63  float getHeight() const {return height;}
64 
65  void set(float _w, float _h) {width = _w, height = _h;}
66  void set(const UDimension& d) {*this = d;}
67 
68  friend std::ostream& operator<<(std::ostream&, const UDimension&);
70 
71  friend std::istream& operator>>(std::istream&, UDimension&);
73  };
74 
75  std::ostream& operator<<(std::ostream&, const UDimension&);
77 
78  std::istream& operator>>(std::istream&, UDimension&);
80 
81 
82  // =============================================================================
85  class UShape : public UObject {
86  public:
87  UABSTRACT_CLASS(UShape)
88 
89  enum ShapeType {
90  LINE = 1,
91  RECT = 2,
92  ELLIPSE = 3,
93  ARC = 4,
94  POLYGON = 5
95  };
96 
97  virtual int getShapeType() const = 0;
98 
99  virtual URect getBounds() const = 0;
101 
102  virtual bool contains(const UPoint&) const = 0;
104 
105  virtual bool contains(const URect&) const = 0;
107 
108  virtual bool intersects(const URect&) const = 0;
110 
111  virtual void draw(const UGraph&) const = 0;
112  virtual void fill(const UGraph&) const = 0;
113  };
114 
115  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118  class ULine : public UShape {
119  public:
120  UCLASS(ULine)
121 
122  UPoint p1, p2;
123 
124  int getShapeType() const {return LINE;}
125 
126  ULine() {}
127  ULine(const UPoint& _p1, const UPoint& _p2) : p1(_p1), p2(_p2) {}
128 
129  URect getBounds() const;
130  UPoint getP1() const {return p1;}
131  UPoint getP2() const {return p2;}
132 
133  virtual void setLine(const UPoint& _p1, const UPoint& _p2) {p1 = _p1; p2 = _p2;}
134  virtual void setLine(const ULine& l) {*this = l;}
135 
136  bool contains(const UPoint&) const {return false;}
137  bool contains(const URect&) const {return false;}
138 
139  bool intersects(const URect&) const;
141 
142  bool intersectsLine(const ULine&) const;
144 
145  static bool linesIntersect(const UPoint& a, const UPoint& b, const UPoint& c, const UPoint& d);
147 
148  static int relativeCCW(UPoint a, UPoint b, UPoint p);
155  int relativeCCW(const UPoint&) const;
157 
158  virtual void draw(const UGraph&) const;
159  virtual void fill(const UGraph&) const {};
160  };
161 
162  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
165  class URect : public UShape {
166  public:
167  UCLASS(URect)
168 
169  float x, y, width, height;
170 
171  int getShapeType() const {return RECT;}
172 
173  URect() : x(0), y(0), width(0), height(0) {}
174  URect(float _x, float _y, float _w, float _h) : x(_x), y(_y), width(_w), height(_h) {}
175  URect(const UPoint& p1, const UPoint& p2) {setRect(p1, p2);}
176 
177  URect getBounds() const {return *this;}
178  bool isEmpty() const {return width <= 0.0 || height <= 0.0;}
179 
180  void setRect(const URect& r) {*this = r;}
181  void setRect(float x, float y, float width, float height);
182  void setRect(const UPoint& p1, const UPoint& p2);
183  void setFrame(const URect& r) {*this = r;}
184 
185  bool contains(const UPoint&) const;
186  bool contains(const URect&) const;
187 
188  bool intersects(const URect&) const;
190 
191  bool intersectsLine(const ULine&) const;
193 
194  bool doIntersection(const URect&);
199  void doUnion(const URect&);
204  enum {OUT_LEFT = 1, OUT_TOP = 2, OUT_RIGHT = 4, OUT_BOTTOM = 8};
205 
206  int outcode(const UPoint& p) const;
208 
209  virtual void draw(const UGraph&) const;
210  virtual void fill(const UGraph&) const;
211  };
212 
213  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
216  class UEllipse : public UShape {
217  public:
218  UCLASS(UEllipse)
219 
220  float x, y, width, height;
221 
222  int getShapeType() const {return ELLIPSE;}
223 
224  UEllipse();
225  UEllipse(const URect&);
226  UEllipse(float x, float y, float weight, float height);
227 
228  URect getBounds() const;
229  bool isEmpty() const {return width <= 0.0 || height <= 0.0;}
230 
231  void setFrame(const URect&);
232  void setFrame(float x, float y, float width, float height);
233 
234  bool contains(float x, float y) const;
235  bool contains(const UPoint&) const;
236  bool contains(const URect&) const;
237  bool intersects(const URect&) const;
238 
239  virtual void draw(const UGraph&) const;
240  virtual void fill(const UGraph&) const;
241  };
242 
243  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
246  class UPolygon : public UShape {
247  public:
248  UCLASS(UPolygon)
249 
250  virtual int getShapeType() const {return POLYGON;}
251 
252  virtual ~UPolygon();
253 
254  UPolygon(bool closed = true);
256 
257  UPolygon(const std::vector<UPoint>& points, bool closed = true);
259 
260  UPolygon(const UPolygon& poly2);
262 
263  UPolygon& operator=(const UPolygon& poly2);
265 
266  UPolygon& addPoint(const UPoint& point);
268 
269  UPolygon& addPoints(const std::vector<UPoint>& points);
271 
272  UPolygon& addPoints(const float* _points, int _npoints);
274 
275  URect getBounds() const {return bounds;}
277 
278  void translate(float dx, float dy);
280 
281  void reset();
283 
284  bool contains(const UPoint&) const;
285  bool contains(const URect&) const;
286  bool intersects(const URect&) const;
287 
288  void draw(const UGraph&) const;
289  void fill(const UGraph&) const;
290 
291  private:
292  int npoints, memsize;
293  bool closed;
294  float* points; // the array contains x0, y0, x1, y1, x2, y2...
295  URect bounds;
296  int evaluateCrossings(float x, float y, bool useYaxis, float distance) const;
297  void augment(int n);
298  void updateBounds(float x, float y);
299  };
300 
301 }
302 #endif
2D Dimension.
Definition: ugeom.hpp:55
2D Point.
Definition: ugeom.hpp:25
2D Ellipse.
Definition: ugeom.hpp:216
URect getBounds() const
returns the bounding box of this shape.
Definition: ugeom.hpp:177
2D Line.
Definition: ugeom.hpp:118
bool contains(const UPoint &) const
tests if a given point is inside the boundary of this Shape.
Definition: ugeom.hpp:136
class for drawing on widgets.
Definition: ugraph.hpp:44
2D Rectangle.
Definition: ugeom.hpp:165
friend std::istream & operator>>(std::istream &, UPoint &)
reads the point from an input stream (eg: cin >> point).
URect getBounds() const
returns the bounding box of the polygon.
Definition: ugeom.hpp:275
bool contains(const URect &) const
tests if the interior of this Shape entirely contains the specified Rectangle
Definition: ugeom.hpp:137
Abstract Base class for geometrical shapes.
Definition: ugeom.hpp:85
friend std::ostream & operator<<(std::ostream &, const UPoint &)
prints the point on an output stream (eg: cout << point).
Definition: uhardfont.hpp:31
2D Polygon or Polyline.
Definition: ugeom.hpp:246
Base class of most Ubit objects (SEE DETAILS!).
Definition: uobject.hpp:113