1 #ifndef SEEN_GEOM_RECT_HULL_H
2 #define SEEN_GEOM_RECT_HULL_H
4 /* ex:set et ts=4 sw=4: */
6 /*
7 * A class representing the convex hull of a set of points.
8 *
9 * Copyright 2004 MenTaLguY <mental@rydia.net>
10 *
11 * This code is licensed under the GNU GPL; see COPYING for more information.
12 */
14 #include <2geom/rect.h>
16 namespace Geom {
18 class RectHull {
19 public:
20 RectHull() : _bounds() {}
21 explicit RectHull(Point const &p) : _bounds(Rect(p, p)) {}
23 boost::optional<Point> midpoint() const {
24 if (_bounds) {
25 return _bounds->midpoint();
26 } else {
27 return boost::optional<Point>();
28 }
29 }
31 void add(Point const &p) {
32 if (_bounds) {
33 _bounds->expandTo(p);
34 } else {
35 _bounds = Rect(p, p);
36 }
37 }
38 void add(Rect const &r) {
39 // Note that this is a hack. when convexhull actually works
40 // you will need to add all four points.
41 _bounds = unify(_bounds, r);
42 }
43 void add(RectHull const &h) {
44 if (h._bounds) {
45 add(*h._bounds);
46 }
47 }
49 OptRect const &bounds() const {
50 return _bounds;
51 }
53 private:
54 OptRect _bounds;
55 };
57 } /* namespace Geom */
59 #endif