1 #include "rectangle.h"
2
3 #include "geom.h"
4
Rectangle()5 Rectangle::Rectangle():x(0.0), y(0.0), width(0.0), height(0.0){
6
7 }
8
Rectangle(const glm::vec4 & _vec4)9 Rectangle::Rectangle(const glm::vec4 &_vec4){
10 set(_vec4);
11 }
12
Rectangle(const glm::ivec4 & _vec4)13 Rectangle::Rectangle(const glm::ivec4 &_vec4){
14 set(_vec4);
15 }
16
Rectangle(const Rectangle & _rect,const float & _margin)17 Rectangle::Rectangle(const Rectangle &_rect, const float &_margin){
18 set(_rect.x-_margin, _rect.y-_margin, _rect.width+_margin*2., _rect.height+_margin*2.);
19 }
20
Rectangle(const float & _x,const float & _y,const float & _width,const float & _height)21 Rectangle::Rectangle(const float &_x, const float &_y, const float &_width, const float &_height){
22 set(_x, _y, _width, _height);
23 }
24
~Rectangle()25 Rectangle::~Rectangle(){
26 }
27
set(const glm::vec4 & _vec4)28 void Rectangle::set(const glm::vec4 &_vec4){
29 set(_vec4.x, _vec4.y, _vec4.z, _vec4.w);
30 }
31
set(const glm::ivec4 & _vec4)32 void Rectangle::set(const glm::ivec4 &_vec4){
33 set(_vec4.x, _vec4.y, _vec4.z, _vec4.w);
34 }
35
set(const float & _x,const float & _y,const float & _width,const float & _height)36 void Rectangle::set(const float &_x, const float &_y, const float &_width, const float &_height){
37 x = _x;
38 y = _y;
39 width = _width;
40 height = _height;
41 }
42
translate(const glm::vec3 & _pos)43 void Rectangle::translate(const glm::vec3 &_pos){
44 x += _pos.x;
45 y += _pos.y;
46 }
47
48 //----------------------------------------------------------
getMin() const49 glm::vec3 Rectangle::getMin() const {
50 return glm::vec3(getMinX(),getMinY(),0.);
51 }
52
53 //----------------------------------------------------------
getMax() const54 glm::vec3 Rectangle::getMax() const {
55 return glm::vec3(getMaxX(),getMaxY(),0.);
56 }
57
58 //----------------------------------------------------------
getMinX() const59 float Rectangle::getMinX() const {
60 return MIN(x, x + width); // - width
61 }
62
63 //----------------------------------------------------------
getMaxX() const64 float Rectangle::getMaxX() const {
65 return MAX(x, x + width); // - width
66 }
67
68 //----------------------------------------------------------
getMinY() const69 float Rectangle::getMinY() const{
70 return MIN(y, y + height); // - height
71 }
72
73 //----------------------------------------------------------
getMaxY() const74 float Rectangle::getMaxY() const {
75 return MAX(y, y + height); // - height
76 }
77
inside(const float & _px,const float & _py) const78 bool Rectangle::inside(const float &_px, const float &_py) const {
79 return inside(glm::vec3(_px,_py,0.));
80 }
81
getLeft() const82 float Rectangle::getLeft() const {
83 return getMinX();
84 }
85
86 //----------------------------------------------------------
getRight() const87 float Rectangle::getRight() const {
88 return getMaxX();
89 }
90
91 //----------------------------------------------------------
getTop() const92 float Rectangle::getTop() const {
93 return getMinY();
94 }
95
96 //----------------------------------------------------------
getBottom() const97 float Rectangle::getBottom() const {
98 return getMaxY();
99 }
100
101 //----------------------------------------------------------
getTopLeft() const102 glm::vec3 Rectangle::getTopLeft() const {
103 return getMin();
104 }
105
106 //----------------------------------------------------------
getTopRight() const107 glm::vec3 Rectangle::getTopRight() const {
108 return glm::vec3(getRight(),getTop(),0.);
109 }
110
111 //----------------------------------------------------------
getBottomLeft() const112 glm::vec3 Rectangle::getBottomLeft() const {
113 return glm::vec3(getLeft(),getBottom(),0.);
114 }
115
116 //----------------------------------------------------------
getBottomRight() const117 glm::vec3 Rectangle::getBottomRight() const {
118 return getMax();
119 }
120
getCenter() const121 glm::vec3 Rectangle::getCenter() const {
122 return glm::vec3(x + width * 0.5f, y + height * 0.5f, 0.0);
123 }
124
125 //----------------------------------------------------------
inside(const glm::vec3 & p) const126 bool Rectangle::inside(const glm::vec3& p) const {
127 return p.x > getMinX() && p.y > getMinY() &&
128 p.x < getMaxX() && p.y < getMaxY();
129 }
130
131 //----------------------------------------------------------
inside(const Rectangle & rect) const132 bool Rectangle::inside(const Rectangle& rect) const {
133 return inside(rect.getMinX(),rect.getMinY()) &&
134 inside(rect.getMaxX(),rect.getMaxY());
135 }
136
137 //----------------------------------------------------------
inside(const glm::vec3 & p0,const glm::vec3 & p1) const138 bool Rectangle::inside(const glm::vec3& p0, const glm::vec3& p1) const {
139 // check to see if a line segment is inside the rectangle
140 return inside(p0) && inside(p1);
141 }
142
143 //----------------------------------------------------------
intersects(const Rectangle & rect) const144 bool Rectangle::intersects(const Rectangle& rect) const {
145 return (getMinX() < rect.getMaxX() && getMaxX() > rect.getMinX() &&
146 getMinY() < rect.getMaxY() && getMaxY() > rect.getMinY());
147 }
148
growToInclude(const glm::vec3 & p)149 void Rectangle::growToInclude(const glm::vec3& p){
150 float x0 = MIN(getMinX(),p.x);
151 float x1 = MAX(getMaxX(),p.x);
152 float y0 = MIN(getMinY(),p.y);
153 float y1 = MAX(getMaxY(),p.y);
154 float w = x1 - x0;
155 float h = y1 - y0;
156 set(x0,y0,w,h);
157 }
158
growToInclude(const std::vector<glm::vec3> & _points)159 void Rectangle::growToInclude(const std::vector<glm::vec3> &_points){
160 for(auto &it: _points){
161 growToInclude(it);
162 }
163 }
164