GUIDOLib  1.7.7
Guido Engine Internal Documentation
TRect.h
1 
2 /*
3  GUIDO Library
4  Copyright (C) 2003-2004 Grame
5 
6  This Source Code Form is subject to the terms of the Mozilla Public
7  License, v. 2.0. If a copy of the MPL was not distributed with this
8  file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10  Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
11  research@grame.fr
12 
13 */
14 
15 #ifndef TRect_H
16 #define TRect_H
17 
18 #include<ostream>
19 
20 // ---------------------------------------------------------------------------------
21 // Origin is the top-left corner, when y grows, positions are descending.
22 template <typename T>
23 class TRect
24 {
25  public:
26 
27  T left;
28  T top;
29  T right;
30  T bottom;
31 
32  TRect() : left(0), top(0), right(0), bottom(0) { }
33  TRect( T inLeft, T inTop, T inRight, T inBottom )
34  : left( inLeft ), top( inTop ), right( inRight ), bottom( inBottom ) {}
35 
36  void Set( T inLeft, T inTop, T inRight, T inBottom )
37  { left = inLeft; top = inTop; right = inRight; bottom = inBottom; }
38 
39  void SetValid( T inLeft, T inTop, T inRight, T inBottom )
40  {
41  if (inLeft > inRight) {
42  left = inRight; right = inLeft;
43  }
44  else {
45  left = inLeft; right = inRight;
46  }
47  if (inTop > inBottom) {
48  top = inBottom; bottom = inTop;
49  }
50  else {
51  top = inTop; bottom = inBottom;
52  }
53  }
54 
55  void Set( const TRect & in )
56  { left = in.left; top = in.top; right = in.right; bottom = in.bottom; }
57 
58  bool IsValid() const { return (Width() > 0 && Height() > 0); }
59  T Width() const { return (right - left); }
60  T Height() const { return (bottom - top); }
61 
62  void SetDims( T inWidth, T inHeight ) { SetWidth( inWidth ); SetHeight( inHeight ); }
63  void SetWidth( T in ) { right = left + in; }
64  void SetHeight( T in ) { bottom = top + in; }
65 
66  void SetPos( T x, T y ) { SetXPos( x ); SetYPos( y ); }
67  void SetXPos( T inXPos ) { right += (inXPos - left); left = inXPos; }
68  void SetYPos( T inYPos ) { bottom += (inYPos - top); top = inYPos; }
69 
70  void GetPos( T* x, T* y ) { *x = left; *y = top;}
71  void GetXPos( T* inXPos ) { *inXPos = left; }
72  void GetYPos( T* inYPos ) { *inYPos = top; }
73  void GetPos( T* x1, T* y1, T* x2, T* y2 ) { *x1 = left; *y1 = top; *x2 = right; *y2 = bottom;}
74 
75  void ShiftX( T inOffset ) { left += inOffset; right += inOffset; }
76  void ShiftY( T inOffset ) { top += inOffset; bottom += inOffset; }
77  void Shift( T x, T y ) { left += x; right += x; top += y; bottom += y; }
78  void Scale( T x, T y ) { left *= x; right *= x; top *= y; bottom *= y; }
79  void Expand( T val ) { left -= val; right += val; top -= val; bottom += val; }
80  void Magnify( float ratio ) { T x = Width()*ratio; T y = Height()*ratio; left -= x/2.; right += x/2.; top -= y/2.; bottom += y/2.; }
81  void Zoom( T offset ) { left -= offset; top -= offset;
82  right += offset; bottom += offset; }
83 
84  // ---------------------------------------------------------------------------------
85  template <typename S> TRect<T> & operator += ( const S & inVector )
86  {
87  left += inVector.x;
88  right += inVector.x;
89  top += inVector.y;
90  bottom += inVector.y;
91  return *this;
92  }
93 
94  // ---------------------------------------------------------------------------------
95  template <typename S> bool operator == ( const S & inRect ) const
96  {
97  return ( (left == inRect.left)
98  && (right == inRect.right)
99  && (top == inRect.top)
100  && (bottom == inRect.bottom) );
101  }
102 
103  // used to sort rectangles using y ordering
104  // ---------------------------------------------------------------------------------
105  template <typename S> bool operator < ( const S & inRect ) const
106  {
107  return ( (left <= inRect.left)
108  && (top < inRect.top) );
109  }
110 
111  // ---------------------------------------------------------------------------------
112  template <typename S> void CalcMiddle( S * outVector )
113  {
114  outVector->x = (left + right) * 0.5;
115  outVector->y = (top + bottom) * 0.5;
116  }
117 
118  // ---------------------------------------------------------------------------------
119  void CalcMiddle( T * oX, T * oY )
120  {
121  *oX = (T)((left + right) * (0.5));
122  *oY = (T)((top + bottom) * (0.5));
123  }
124 
125  // ---------------------------------------------------------------------------------
126  template <typename S> bool Contains( const S & inVector ) const
127  {
128  return (inVector.x >= left && inVector.x < right &&
129  inVector.y >= top && inVector.y < bottom );
130  }
131 
132  // ---------------------------------------------------------------------------------
133  bool Contains( const TRect<T> & in ) const
134  {
135  return (in.left >= left && in.right <= right && in.top >= top && in.bottom <= bottom );
136  }
137 
138  // ---------------------------------------------------------------------------------
139  bool Contains( T inX, T inY ) const
140  {
141  return (inX >= left && inX < right && inY >= top && inY < bottom );
142  }
143 
144  // ---------------------------------------------------------------------------------
145  template <typename S> bool Collides( const S & inRect ) const
146  {
147  return (( inRect.left < right ) && ( inRect.right > left )
148  && ( inRect.top < bottom ) && ( inRect.bottom > top ));
149  }
150 
151  // ---------------------------------------------------------------------------------
152  template <typename S> void Merge( const S & inRect )
153  {
154  if( inRect.Width() <= 0 || inRect.Height() <= 0 ) return;
155  else if( Width() <= 0 || Height() <= 0 ) Set( inRect );
156  else
157  {
158  if( inRect.left < left ) left = inRect.left;
159  if( inRect.right > right ) right = inRect.right;
160  if( inRect.top < top ) top = inRect.top;
161  if( inRect.bottom > bottom ) bottom = inRect.bottom;
162  }
163  }
164 
165  // ---------------------------------------------------------------------------------
166  template <typename S> void Intersect( const S & inRect )
167  {
168  if( inRect.left > left ) left = inRect.left;
169  if( inRect.right < right ) right = inRect.right;
170  if( inRect.top > top ) top = inRect.top;
171  if( inRect.bottom < bottom ) bottom = inRect.bottom;
172 
173  if( right < left ) { right = 0; left = 0; }
174  if( bottom < top ) { top = 0; bottom = 0; }
175  }
176 
177  // ----------------------------------------------------------------------------
178  // returns the power-of-two of the distance between input point and our
179  // nearest edge returns 0 of the point is inside us.
180  T DistanceToPoint( T x, T y )
181  {
182  T deltaX = 0;
183  if( x < left ) deltaX = left - x;
184  else if( x > right ) deltaX = x - right;
185 
186  T deltaY = 0;
187  if( y < top ) deltaY = top - y;
188  else if( y > bottom ) deltaY = y - bottom;
189 
190  return ((deltaX * deltaX) + (deltaY * deltaY));
191  }
192 
193  // ----------------------------------------------------------------------------
194  void print( std::ostream& os ) const { os << "[" << left << ", " << top << ", " << right << ", " << bottom << "]"; }
195 };
196 
197 template<typename T> std::ostream& operator<< (std::ostream& os, const TRect<T>& r) { r.print(os); return os; }
198 
199 // - Usual rects
200 typedef TRect<float> FloatRect;
201 typedef TRect<int> IntRect;
202 
203 #endif
TRect::Expand
void Expand(T val)
Definition: TRect.h:79
TRect::ShiftY
void ShiftY(T inOffset)
Definition: TRect.h:76
TRect::GetPos
void GetPos(T *x, T *y)
Definition: TRect.h:70
TRect::operator+=
TRect< T > & operator+=(const S &inVector)
Definition: TRect.h:85
TRect::TRect
TRect(T inLeft, T inTop, T inRight, T inBottom)
Definition: TRect.h:33
TRect::Contains
bool Contains(T inX, T inY) const
Definition: TRect.h:139
TRect::TRect
TRect()
Definition: TRect.h:32
TRect::IsValid
bool IsValid() const
Definition: TRect.h:58
TRect::Contains
bool Contains(const TRect< T > &in) const
Definition: TRect.h:133
TRect::CalcMiddle
void CalcMiddle(T *oX, T *oY)
Definition: TRect.h:119
TRect::DistanceToPoint
T DistanceToPoint(T x, T y)
Definition: TRect.h:180
TRect::operator==
bool operator==(const S &inRect) const
Definition: TRect.h:95
TRect::SetXPos
void SetXPos(T inXPos)
Definition: TRect.h:67
TRect::GetPos
void GetPos(T *x1, T *y1, T *x2, T *y2)
Definition: TRect.h:73
TRect::operator<
bool operator<(const S &inRect) const
Definition: TRect.h:105
TRect::SetYPos
void SetYPos(T inYPos)
Definition: TRect.h:68
TRect::Scale
void Scale(T x, T y)
Definition: TRect.h:78
TRect::Merge
void Merge(const S &inRect)
Definition: TRect.h:152
TRect::Magnify
void Magnify(float ratio)
Definition: TRect.h:80
TRect::Intersect
void Intersect(const S &inRect)
Definition: TRect.h:166
TRect::Set
void Set(T inLeft, T inTop, T inRight, T inBottom)
Definition: TRect.h:36
TRect::print
void print(std::ostream &os) const
Definition: TRect.h:194
TRect::SetPos
void SetPos(T x, T y)
Definition: TRect.h:66
TRect::Zoom
void Zoom(T offset)
Definition: TRect.h:81
TRect::SetValid
void SetValid(T inLeft, T inTop, T inRight, T inBottom)
Definition: TRect.h:39
TRect::ShiftX
void ShiftX(T inOffset)
Definition: TRect.h:75
TRect::SetHeight
void SetHeight(T in)
Definition: TRect.h:64
TRect::Collides
bool Collides(const S &inRect) const
Definition: TRect.h:145
TRect::GetYPos
void GetYPos(T *inYPos)
Definition: TRect.h:72
TRect::left
T left
Definition: TRect.h:27
TRect::Contains
bool Contains(const S &inVector) const
Definition: TRect.h:126
TRect::SetDims
void SetDims(T inWidth, T inHeight)
Definition: TRect.h:62
TRect::SetWidth
void SetWidth(T in)
Definition: TRect.h:63
TRect::Width
T Width() const
Definition: TRect.h:59
TRect::Shift
void Shift(T x, T y)
Definition: TRect.h:77
TRect::right
T right
Definition: TRect.h:29
TRect::bottom
T bottom
Definition: TRect.h:30
operator<<
std::ostream & operator<<(std::ostream &os, const svgendl &eol)
TRect::top
T top
Definition: TRect.h:28
TRect
Definition: TRect.h:23
TRect::Set
void Set(const TRect &in)
Definition: TRect.h:55
TRect::GetXPos
void GetXPos(T *inXPos)
Definition: TRect.h:71
TRect::Height
T Height() const
Definition: TRect.h:60
TRect::CalcMiddle
void CalcMiddle(S *outVector)
Definition: TRect.h:112

Guido Project Copyright © 2019 Grame-CNCM