ezgl  1.0.1
An Easy Graphics & GUI Library
rectangle.hpp
1 /*
2  * Copyright 2019-2022 University of Toronto
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Authors: Mario Badr, Sameh Attia, Tanner Young-Schultz and Vaughn Betz
17  */
18 
19 #ifndef EZGL_RECTANGLE_HPP
20 #define EZGL_RECTANGLE_HPP
21 
22 #include "ezgl/point.hpp"
23 
24 #include <algorithm>
25 
26 namespace ezgl {
27 
31 class rectangle {
32 public:
36  rectangle() : m_first({0, 0}), m_second({0, 0})
37  {
38  }
39 
43  rectangle(point2d origin_pt, point2d top_right_pt) : m_first(origin_pt), m_second(top_right_pt)
44  {
45  }
46 
50  rectangle(point2d origin_pt, double rec_width, double rec_height) : m_first(origin_pt), m_second(origin_pt)
51  {
52  m_second.x += rec_width;
53  m_second.y += rec_height;
54  }
55 
59  double left() const
60  {
61  return std::min(m_first.x, m_second.x);
62  }
63 
67  double right() const
68  {
69  return std::max(m_first.x, m_second.x);
70  }
71 
75  double bottom() const
76  {
77  return std::min(m_first.y, m_second.y);
78  }
79 
83  double top() const
84  {
85  return std::max(m_first.y, m_second.y);
86  }
87 
92  {
93  return {left(), bottom()};
94  }
95 
99  point2d top_left() const
100  {
101  return {left(), top()};
102  }
103 
108  {
109  return {right(), bottom()};
110  }
111 
116  {
117  return {right(), top()};
118  }
119 
123  bool contains(double x, double y) const
124  {
125  if(x < left() || right() < x || y < bottom() || top() < y) {
126  return false;
127  }
128 
129  return true;
130  }
131 
135  bool contains(point2d point) const
136  {
137  return contains(point.x, point.y);
138  }
139 
143  double width() const
144  {
145  return right() - left();
146  }
147 
151  double height() const
152  {
153  return top() - bottom();
154  }
155 
160  double area() const
161  {
162  return width() * height();
163  }
164 
168  double center_x() const
169  {
170  return (right() + left()) * 0.5;
171  }
172 
176  double center_y() const
177  {
178  return (top() + bottom()) * 0.5;
179  }
180 
184  point2d center() const
185  {
186  return {center_x(), center_y()};
187  }
188 
192  bool operator==(const rectangle &rhs) const
193  {
194  return m_first == rhs.m_first && m_second == rhs.m_second;
195  }
196 
200  bool operator!=(const rectangle &rhs) const
201  {
202  return !(rhs == *this);
203  }
204 
208  friend rectangle &operator+=(rectangle &lhs, point2d const &rhs)
209  {
210  lhs.m_first += rhs;
211  lhs.m_second += rhs;
212 
213  return lhs;
214  }
215 
219  friend rectangle &operator-=(rectangle &lhs, point2d const &rhs)
220  {
221  lhs.m_first -= rhs;
222  lhs.m_second -= rhs;
223 
224  return lhs;
225  }
226 
230  friend rectangle operator-(rectangle &lhs, point2d const &rhs)
231  {
232  return rectangle(lhs.m_first - rhs, lhs.m_second - rhs);
233  }
234 
238  friend rectangle operator+(rectangle &lhs, point2d const &rhs)
239  {
240  return rectangle(lhs.m_first + rhs, lhs.m_second + rhs);
241  }
242 
245 
248 };
249 }
250 
251 #endif //EZGL_RECTANGLE_HPP
Represents a two-dimensional point.
Definition: point.hpp:27
double y
Location of the y-coordinate.
Definition: point.hpp:51
double x
Location of the x-coordinate.
Definition: point.hpp:46
Represents a rectangle as two diagonally opposite points.
Definition: rectangle.hpp:31
friend rectangle operator-(rectangle &lhs, point2d const &rhs)
Create a new rectangle that is translated (negative offsets).
Definition: rectangle.hpp:230
double center_y() const
The center of the rectangle in the y plane.
Definition: rectangle.hpp:176
double left() const
The minimum x-coordinate.
Definition: rectangle.hpp:59
double bottom() const
The minimum y-coordinate.
Definition: rectangle.hpp:75
friend rectangle & operator-=(rectangle &lhs, point2d const &rhs)
translate the rectangle by negative offsets.
Definition: rectangle.hpp:219
double right() const
The maximum x-coordinate.
Definition: rectangle.hpp:67
point2d center() const
The center of the recangle.
Definition: rectangle.hpp:184
double top() const
The maximum y-coordinate.
Definition: rectangle.hpp:83
point2d m_first
The first point of the rectangle.
Definition: rectangle.hpp:244
point2d bottom_right() const
The maximum x-coordinate and the minimum y-coordinate.
Definition: rectangle.hpp:107
friend rectangle & operator+=(rectangle &lhs, point2d const &rhs)
translate the rectangle by positive offsets.
Definition: rectangle.hpp:208
double center_x() const
The center of the rectangle in the x plane.
Definition: rectangle.hpp:168
rectangle(point2d origin_pt, point2d top_right_pt)
Create a rectangle from two diagonally opposite points.
Definition: rectangle.hpp:43
rectangle(point2d origin_pt, double rec_width, double rec_height)
Create a rectangle with a given width and height.
Definition: rectangle.hpp:50
bool contains(double x, double y) const
Test if the x and y values are within the rectangle.
Definition: rectangle.hpp:123
double width() const
The width of the rectangle.
Definition: rectangle.hpp:143
point2d bottom_left() const
The minimum x-coordinate and the minimum y-coordinate.
Definition: rectangle.hpp:91
double area() const
The area of the rectangle.
Definition: rectangle.hpp:160
point2d m_second
The second point of the rectangle.
Definition: rectangle.hpp:247
double height() const
The height of the rectangle.
Definition: rectangle.hpp:151
bool contains(point2d point) const
Test if the x and y values are within the rectangle.
Definition: rectangle.hpp:135
point2d top_left() const
The minimum x-coordinate and the maximum y-coordinate.
Definition: rectangle.hpp:99
rectangle()
Default constructor: Create a zero-sized rectangle at {0,0}.
Definition: rectangle.hpp:36
bool operator==(const rectangle &rhs) const
Test for equality.
Definition: rectangle.hpp:192
friend rectangle operator+(rectangle &lhs, point2d const &rhs)
Create a new rectangle that is translated (positive offsets).
Definition: rectangle.hpp:238
bool operator!=(const rectangle &rhs) const
Test for inequality.
Definition: rectangle.hpp:200
point2d top_right() const
The maximum x-coordinate and the maximum y-coordinate.
Definition: rectangle.hpp:115
A library for creating a graphical user interface.
Definition: application.hpp:40