JNR
laRect2.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 #ifndef M_RECT_H
30 #define M_RECT_H
31 
33 
34 
36 //
38 
39 class laRect2
40 {
41 public:
42  laPoint2 pt[2];
43  M_BOOL bInitialized;
44 
46 
47 
48  laRect2(void) {
49  reset();
50  }
51 
52  laRect2( const laPoint2 &pos, const laPoint2 &size){
53  reset();
54  add( pos ); add( pos + size );
55  }
56 
57  //laRect2(laPoint2 pt) { GridRect(pt); bInitialized = M_TRUE; }
58 
59  inline void reset() { bInitialized = M_FALSE; };
60 
61 
62  // \name Adding 2D geometry to the bounding rectangle
64  inline void add( const laPoint2 &p ){
65  PROFILE_COL(laRect2_add);
66 
67  if(bInitialized)
68  {
69  if( p.x() < pt[0].x() ) pt[0].x( p.x() );
70  if( p.y() < pt[0].y() ) pt[0].y( p.y() );
71  if( p.x() > pt[1].x() ) pt[1].x( p.x() );
72  if( p.y() > pt[1].y() ) pt[1].y( p.y() );
73  }
74  else
75  {
76  pt[0] = pt[1] = p;
77  bInitialized = M_TRUE;
78  }
79  }
80 
81  inline void add( const laLine2 &ln ){
82  add( ln.origin );
83  add( ln.end() );
84  }
85 
86  inline void add( const laRect2 &r ){
87  add(r.pt[0]);
88  add(r.pt[1]);
89  }
90 
92  inline void inflate(const laPoint2 &inf){
93  add(pt[0] + inf);
94  add(pt[1] + inf);
95  }
97 
98  // ! Test if two rectangles are intersecting
99  //
100  inline M_BOOL intersecting(const laRect2 &r) const{
101  PROFILE_COL(laRect2_intersecting);
102  ASSERT(bInitialized, "Bounding box not initialzied");
103 
104  if(r.pt[0].x() > pt[1].x()) return M_FALSE;
105  if(pt[0].x() > r.pt[1].x()) return M_FALSE;
106 
107  if(r.pt[0].y() > pt[1].y()) return M_FALSE;
108  if(pt[0].y() > r.pt[1].y()) return M_FALSE;
109 
110  return M_TRUE;
111  }
112 
113  // ! Offset rectangle
114  //
115  inline void offset(const laPoint2 &ofs) {
116  pt[0] += ofs;
117  pt[1] += ofs;
118  }
119 
120  //Construct a standard M_UNIT rect around the point (x,y)
121  void GridRect(laPoint2 &pt);
122 
123  //Construct a bounding box
124  //void BoundingBox(laPoint2 a, laPoint2 b);
125 
126  //Check if the point (x,y) is outwith the rect
127  /*void isSafelyOutside(laPoint2 &pt, double delta);
128 
129  //Check if the point (x,y) is within the rect
130  void isSafelyInside(laPoint2 &pt, double delta);*/
131 
133  void draw(laRenderer *r, laPoint2 &pt = laPoint2());
134 };
136 #endif //#ifndef M_RECT_H
M_BOOL bInitialized
Validity flag.
Definition: laRect2.h:43
void draw(laRenderer *r, laPoint2 &pt=laPoint2())
Draw rectangle ( primarily for debug purpoces )
Definition: laRect2.cpp:91
2D Point
Definition: laPoint_novec.h:41
void add(const laPoint2 &p)
Revert to "uninitialized" state.
Definition: laRect2.h:64
laPoint2 end() const
Get the end-point.
Definition: laLine2.h:78
void inflate(const laPoint2 &inf)
Add vector to the rectangle (e.g. velocity)
Definition: laRect2.h:92
2D line segment
Definition: laLine2.h:40
2D rectangle (used by the GUI and also as a simple tool for proximity testing)
Definition: laRect2.h:39
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
laPoint2 origin
Point of origin.
Definition: laLine2.h:43
laPoint2 pt[2]
Rectangle coordinates (upper-left and lower-right point)
Definition: laRect2.h:42