moreupdates
[synfig.git] / synfig-core / trunk / src / synfig / rect.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file rect.h
3 **      \brief Rectangle Class
4 **
5 **      $Id: rect.h,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __SYNFIG_RECT_H
25 #define __SYNFIG_RECT_H
26
27 /* === H E A D E R S ======================================================= */
28
29 #include <ETL/rect>
30 #include "real.h"
31 #include "vector.h"
32 #include <limits>
33 #include <cmath>
34
35 /* === M A C R O S ========================================================= */
36
37 /* === T Y P E D E F S ===================================================== */
38
39 /* === C L A S S E S & S T R U C T S ======================================= */
40
41 namespace synfig {
42
43 class Rect : public etl::rect<Real>
44 {
45 public:
46
47         using etl::rect<Real>::set_point;
48         using etl::rect<Real>::expand;
49         using etl::rect<Real>::set;
50
51         static Rect full_plane();
52
53         static Rect zero()
54         {
55                 return Rect(
56                         0,
57                         0,
58                         0,
59                         0
60                 );
61         }
62
63         Rect() { }
64
65         Rect(const Point& x) { set_point(x); }
66
67         Rect(const Point& min, const Point& max) { set_point(min); expand(max); }
68
69         Rect(const value_type &x1,const value_type &y1) { set_point(x1,y1); }
70         
71         Rect(const value_type &x1,const value_type &y1,
72                         const value_type &x2,const value_type &y2)
73         {
74                 set_point(x1,y1);
75                 expand(x2,y2);
76         }
77                 
78         void set_point(const Point& max) { set_point(max[0],max[1]);    }
79         
80         Rect& expand(const Point& max) { expand(max[0],max[1]); return *this; }
81
82         Rect& expand(const Real& r) { minx-=r; miny-=r; maxx+=r; maxy+=r; return *this; }
83
84         Rect& expand_x(const Real& r) { minx-=r; maxx+=r; return *this; }
85
86         Rect& expand_y(const Real& r) { miny-=r; maxy+=r; return *this; }
87         
88         Rect& set(const Point& min,const Point& max) { set(min[0],min[1],max[0],max[1]); return *this; }
89         
90         Point get_min()const { return Point(minx,miny); }
91         Point get_max()const { return Point(maxx,maxy); }
92         
93         bool is_inside(const Point& x) { return x[0]>minx && x[0]<maxx && x[1]>miny && x[1]<maxy; }
94                 
95         Real area()const
96         {
97                 return (maxx-minx)*(maxy-miny);
98         }
99         
100         // Operators
101         
102         Rect& operator+=(const Vector& rhs)
103         {
104                 minx+=rhs[0]; miny+=rhs[1];
105                 maxx+=rhs[0]; maxy+=rhs[1];
106                 return *this;
107         }
108
109         Rect& operator-=(const Vector& rhs)
110         {
111                 minx-=rhs[0]; miny-=rhs[1];
112                 maxx-=rhs[0]; maxy-=rhs[1];
113                 return *this;
114         }
115
116         Rect& operator*=(const Real& rhs)
117         {
118                 minx*=rhs; miny*=rhs;
119                 maxx*=rhs; maxy*=rhs;
120                 return *this;
121         }
122
123         Rect& operator/=(Real rhs)
124         {
125                 rhs=1.0/rhs; // Avoid doing several divisions
126                 minx*=rhs; miny*=rhs;
127                 maxx*=rhs; maxy*=rhs;
128                 return *this;
129         }
130
131         Rect& operator&=(const Rect& rhs)
132         {
133                 if(rhs.area()>0.00000001 && area()>0.00000001)
134                         etl::set_intersect(*this,*this,rhs);
135                 else
136                         *this=zero();
137                 return *this;
138         }
139
140         Rect& operator|=(const Rect& rhs)
141         {
142                 if(rhs.area()>0.00000001 && area()>0.00000001)
143                         etl::set_union(*this,*this,rhs);
144                 else
145                 {
146                         if(area()<rhs.area())
147                                 *this=rhs;
148                 }
149                 return *this;
150         }
151
152         Rect operator+(const Vector& rhs)const { return Rect(*this)+=rhs; }
153
154         Rect operator-(const Vector& rhs)const { return Rect(*this)-=rhs; }
155
156         Rect operator*(const Real& rhs)const { return Rect(*this)*=rhs; }
157
158         Rect operator/(const Real& rhs)const { return Rect(*this)/=rhs; }
159
160         Rect operator&(const Rect& rhs)const { return Rect(*this)&=rhs; }
161
162         Rect operator|(const Rect& rhs)const { return Rect(*this)|=rhs; }
163
164         bool operator&&(const Rect& rhs)const { return etl::intersect(*this, rhs); }
165
166         bool is_valid()const { return valid(); }
167 }; // END of class Rect
168
169 }; // END of namespace synfig
170
171 /* === E N D =============================================================== */
172
173 #endif