Scribus
Open source desktop publishing at your fingertips
rect.h
1 /*
2  * rect.h - D2<Interval> specialization to Rect
3  *
4  * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it either under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation
9  * (the "LGPL") or, at your option, under the terms of the Mozilla
10  * Public License Version 1.1 (the "MPL"). If you do not alter this
11  * notice, a recipient may use your version of this file under either
12  * the MPL or the LGPL.
13  *
14  * You should have received a copy of the LGPL along with this library
15  * in the file COPYING-LGPL-2.1; if not, output to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  * You should have received a copy of the MPL along with this library
18  * in the file COPYING-MPL-1.1
19  *
20  * The contents of this file are subject to the Mozilla Public License
21  * Version 1.1 (the "License"); you may not use this file except in
22  * compliance with the License. You may obtain a copy of the License at
23  * http://www.mozilla.org/MPL/
24  *
25  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27  * the specific language governing rights and limitations.
28  *
29  */
30 
31 /* Authors of original rect class:
32  * Lauris Kaplinski <lauris@kaplinski.com>
33  * Nathan Hurst <njh@mail.csse.monash.edu.au>
34  * bulia byak <buliabyak@users.sf.net>
35  * MenTaLguY <mental@rydia.net>
36  */
37 
38 #ifdef _2GEOM_D2 /*This is intentional: we don't actually want anyone to
39  include this, other than D2.h. If somone else tries, D2
40  won't be defined. If it is, this will already be included. */
41 #ifndef _2GEOM_RECT
42 #define _2GEOM_RECT
43 
44 #include "matrix.h"
45 #include <boost/optional/optional.hpp>
46 
47 namespace Geom {
48 
49 typedef D2<Interval> Rect;
50 
51 Rect unify(const Rect &, const Rect &);
52 
53 template<>
54 class D2<Interval> {
55  private:
56  Interval f[2];
57  public:
58  D2<Interval>() { f[X] = f[Y] = Interval(0, 0); }
59 
60  D2<Interval>(Interval const &a, Interval const &b) {
61  f[X] = a;
62  f[Y] = b;
63  }
64 
65  D2<Interval>(Point const & a, Point const & b) {
66  f[X] = Interval(a[X], b[X]);
67  f[Y] = Interval(a[Y], b[Y]);
68  }
69 
70  inline Interval& operator[](unsigned i) { return f[i]; }
71  inline Interval const & operator[](unsigned i) const { return f[i]; }
72 
73  inline Point min() const { return Point(f[X].min(), f[Y].min()); }
74  inline Point max() const { return Point(f[X].max(), f[Y].max()); }
75 
78  Point corner(unsigned i) const {
79  switch(i % 4) {
80  case 0: return Point(f[X].min(), f[Y].min());
81  case 1: return Point(f[X].max(), f[Y].min());
82  case 2: return Point(f[X].max(), f[Y].max());
83  default: return Point(f[X].min(), f[Y].max());
84  }
85  }
86 
87  //We should probably remove these - they're coord sys gnostic
88  inline double top() const { return f[Y].min(); }
89  inline double bottom() const { return f[Y].max(); }
90  inline double left() const { return f[X].min(); }
91  inline double right() const { return f[X].max(); }
92 
93  inline double width() const { return f[X].extent(); }
94  inline double height() const { return f[Y].extent(); }
95 
97  inline Point dimensions() const { return Point(f[X].extent(), f[Y].extent()); }
98  inline Point midpoint() const { return Point(f[X].middle(), f[Y].middle()); }
99 
100  inline double area() const { return f[X].extent() * f[Y].extent(); }
101  inline double maxExtent() const { return std::max(f[X].extent(), f[Y].extent()); }
102 
103  inline bool isEmpty() const {
104  return f[X].isEmpty() && f[Y].isEmpty();
105  }
106  inline bool intersects(Rect const &r) const {
107  return f[X].intersects(r[X]) && f[Y].intersects(r[Y]);
108  }
109  inline bool contains(Rect const &r) const {
110  return f[X].contains(r[X]) && f[Y].contains(r[Y]);
111  }
112  inline bool contains(Point const &p) const {
113  return f[X].contains(p[X]) && f[Y].contains(p[Y]);
114  }
115 
116  inline void expandTo(Point p) {
117  f[X].extendTo(p[X]); f[Y].extendTo(p[Y]);
118  }
119  inline void unionWith(Rect const &b) {
120  f[X].unionWith(b[X]); f[Y].unionWith(b[Y]);
121  }
122 
123  inline void expandBy(double amnt) {
124  f[X].expandBy(amnt); f[Y].expandBy(amnt);
125  }
126  inline void expandBy(Point const p) {
127  f[X].expandBy(p[X]); f[Y].expandBy(p[Y]);
128  }
129 
132  inline Rect operator*(Matrix const m) const {
133  return unify(Rect(corner(0) * m, corner(2) * m),
134  Rect(corner(1) * m, corner(3) * m));
135  }
136 };
137 
138 inline Rect unify(Rect const & a, Rect const & b) {
139  return Rect(unify(a[X], b[X]), unify(a[Y], b[Y]));
140 }
141 
142 inline Rect union_list(std::vector<Rect> const &r) {
143  if(r.empty()) return Rect(Interval(0,0), Interval(0,0));
144  Rect ret = r[0];
145  for(unsigned i = 1; i < r.size(); i++)
146  ret.unionWith(r[i]);
147  return ret;
148 }
149 
150 inline boost::optional<Rect> intersect(Rect const & a, Rect const & b) {
151  boost::optional<Interval> x = intersect(a[X], b[X]);
152  boost::optional<Interval> y = intersect(a[Y], b[Y]);
153  return x && y ? boost::optional<Rect>(Rect(*x, *y)) : boost::optional<Rect>();
154 }
155 
156 }
157 
158 #endif //_2GEOM_RECT
159 #endif //_2GEOM_D2
160 /*
161  Local Variables:
162  mode:c++
163  c-file-style:"stroustrup"
164  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
165  indent-tabs-mode:nil
166  fill-column:99
167  End:
168 */
169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
Definition: angle.h:38