1 /*! ========================================================================
2 ** Extended Template Library
3 ** Rectangle Basic Class Implementation
6 ** Copyright (c) 2002 Adrian Bentley
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
30 /* === H E A D E R S ======================================================= */
35 /* === M A C R O S ========================================================= */
37 /* === T Y P E D E F S ===================================================== */
39 /* === C L A S S E S & S T R U C T S ======================================= */
43 template < typename T >
46 public: //type niceties
49 public: //representation
51 value_type minx,maxx,miny,maxy;
57 rect(const value_type &x1,const value_type &y1)
62 rect(const value_type &x1,const value_type &y1,
63 const value_type &x2,const value_type &y2)
69 rect(const rect<T> &o)
70 :minx(o.minx),maxx(o.maxx),miny(o.miny),maxy(o.maxy)
73 template < typename U >
74 rect(const rect<U> &o)
75 :minx(o.minx),maxx(o.maxx),miny(o.miny),maxy(o.maxy)
78 void set_point(const value_type &x1,const value_type &y1)
84 void expand(const value_type &x1,const value_type &y1)
86 minx = std::min(minx,x1);
87 maxx = std::max(maxx,x1);
88 miny = std::min(miny,y1);
89 maxy = std::max(maxy,y1);
92 void set(const value_type &x1,const value_type &y1,
93 const value_type &x2,const value_type &y2)
99 //HACK HACK HACK (stupid compiler doesn't like default arguments of any type)
102 return valid(std::less<T>());
105 template < typename F >
106 bool valid(const F & func) const
108 return func(minx,maxx) && func(miny,maxy);
112 template < typename T, typename F >
113 inline bool intersect(const rect<T> &r1, const rect<T> &r2, const F & func)
115 /* We wan to do the edge compare test
117 |------| intersecting
120 |-----| not intersecting
122 So we want to compare the mins of the one against the maxs of the other, and
125 by default (exclude edge sharing) less will not be true if they are equal...
128 return func(r1.minx,r2.maxx) &&
129 func(r2.minx,r1.maxx) &&
130 func(r1.miny,r2.maxy) &&
131 func(r2.miny,r1.maxy);
134 template < typename T >
135 inline bool intersect(const rect<T> &r1, const rect<T> &r2)
137 return intersect(r1,r2,std::less<T>());
140 template < typename T >
141 void set_intersect(rect<T> &rout, const rect<T> &r1, const rect<T> &r2)
143 //takes the intersection of the two rectangles
144 rout.minx = std::max(r1.minx,r2.minx);
145 rout.miny = std::max(r1.miny,r2.miny);
146 rout.maxx = std::min(r1.maxx,r2.maxx);
147 rout.maxy = std::min(r1.maxy,r2.maxy);
150 template < typename T >
151 void set_union(rect<T> &rout, const rect<T> &r1, const rect<T> &r2)
153 //takes the union of the two rectangles (bounds both... will contain extra info, but that's ok)
155 std::min(r1.minx,r2.minx),
156 std::min(r1.miny,r2.miny),
157 std::max(r1.maxx,r2.maxx),
158 std::max(r1.maxy,r2.maxy));
159 /*rect<T> local = r1;
160 rout.expand(r2.minx,r2.miny);
161 rout.expand(r2.maxx,r2.maxy);
167 /* === E X T E R N S ======================================================= */
169 /* === E N D =============================================================== */