Board 0.9.6
Rect.h
Go to the documentation of this file.
1/* -*- mode: c++ -*- */
26#ifndef BOARD_RECT_H
27#define BOARD_RECT_H
28
29#include <board/Point.h>
30#include <cmath>
31#include <iostream>
32
33namespace LibBoard
34{
35
40struct Rect {
41 double left;
42 double top;
43 double width;
44 double height;
54 Rect(double left = 0.0, double top = 0.0, double width = 0.0, double height = 0.0) : left(left), top(top), width(width), height(height) {}
55
63 Rect(Point topLeft, double width = 0.0, double height = 0.0) : left(topLeft.x), top(topLeft.y), width(width), height(height) {}
64
72
77 Point topLeft() const { return Point(left, top); }
78
83 Point topRight() const { return Point(left + width, top); }
84
89 Point bottomLeft() const { return Point(left, top - height); }
90
95 Point bottomRight() const { return Point(left + width, top - height); }
96
101 Point center() const { return Point(left + width / 2.0, top - height / 2.0); }
102
107 Point centerLeft() const { return Point(left, top - height / 2.0); }
108
113 Point centerRight() const { return Point(left + width, top - height / 2.0); }
114
119 Point centerTop() const { return Point(left + width / 2.0, top); }
120
125 Point centerBottom() const { return Point(left + width / 2.0, top - height); }
126
131 double bottom() const { return top - height; }
132
137 double right() const { return left + width; }
138
143 double diameter() const { return std::hypot(width, height); }
144
148 void clear() { left = top = width = height = 0.0; }
149
155 Rect & growToContain(const Point & p);
156
162 Rect & growToContain(const std::vector<Point> & points);
163
169 bool contains(Point p) const;
170
176 bool strictlyContains(Point p) const;
177
183 bool intersects(const Rect & other) const;
184
190 bool strictlyIntersects(const Rect & other) const;
191
197 Rect & grow(double margin);
198
204 Rect growed(double margin);
205
210 inline bool isNull() const;
211};
212
220Rect operator||(const Rect & rectA, const Rect & rectB);
221
229Rect operator&&(const Rect & rectA, const Rect & rectB);
230
231} // namespace LibBoard
232
241std::ostream & operator<<(std::ostream & out, const LibBoard::Rect & rect);
242
243// Inline methods
244
245namespace LibBoard
246{
247
248bool Rect::isNull() const
249{
250 return width == 0.0 && height == 0.0;
251}
252
253} // namespace LibBoard
254
255#endif // BOARD_RECT_H
The Point structure. @copyright This source code is part of the Board project, a C++ library whose pu...
std::ostream & operator<<(std::ostream &out, const LibBoard::Rect &rect)
Definition Rect.cpp:142
Definition Board.h:55
Rect operator&&(const Rect &rectA, const Rect &rectB)
Definition Rect.cpp:57
Rect operator||(const Rect &rectA, const Rect &rectB)
Definition Rect.cpp:32
Struct representing a 2D point.
Definition Point.h:42
Struct representing a rectangle on the plane.
Definition Rect.h:40
Rect(Point topLeft, Point bottomRight)
Definition Rect.h:71
Point topLeft() const
topLeft Top-left point of the rectangle
Definition Rect.h:77
bool contains(Point p) const
Check whether or not a point is contained in the rectangle (including its border)
Definition Rect.cpp:104
Point bottomLeft() const
bottomLeft Bottom-left point of the rectangle
Definition Rect.h:89
bool strictlyIntersects(const Rect &other) const
Check whether the rectangle strictly intersects another rectangle.
Definition Rect.cpp:120
double diameter() const
diameter
Definition Rect.h:143
bool isNull() const
Check if a rectangle is null (i.e., zero sized)
Definition Rect.h:248
double bottom() const
The y coordinate of the bottom of the rectangle.
Definition Rect.h:131
double height
Definition Rect.h:44
Rect(double left=0.0, double top=0.0, double width=0.0, double height=0.0)
Definition Rect.h:54
Point centerRight() const
centerRight The center of the right border of the rectangle
Definition Rect.h:113
Rect & grow(double margin)
Grow the rectangle by a given distance (margin) in each direction.
Definition Rect.cpp:126
Point centerTop() const
centerTop The center of the top border of the rectangle
Definition Rect.h:119
double right() const
The x coordinate of the right side of the rectangle.
Definition Rect.h:137
bool intersects(const Rect &other) const
Check whether the rectangle intersects another rectangle.
Definition Rect.cpp:114
Point centerLeft() const
centerLeft The center of the left border of the rectangle
Definition Rect.h:107
Point bottomRight() const
bottomRight Bottom-right point of the rectangle
Definition Rect.h:95
double width
Definition Rect.h:43
Rect(Point topLeft, double width=0.0, double height=0.0)
Definition Rect.h:63
Point centerBottom() const
centerBottom The center of the bottom border of the rectangle
Definition Rect.h:125
double left
Definition Rect.h:41
void clear()
Set to a rectangle with zero width and height at position (0,0)
Definition Rect.h:148
double top
Definition Rect.h:42
bool strictlyContains(Point p) const
Check whether or not a point is strictly contained in the rectangle.
Definition Rect.cpp:109
Point topRight() const
topRight Top-right point of the rectangle
Definition Rect.h:83
Rect growed(double margin)
Return the rectangle growed by a given distance (margin) in each direction.
Definition Rect.cpp:135
Point center() const
center The center of the rectangle
Definition Rect.h:101
Rect & growToContain(const Point &p)
Grow the rectangle so that it contains the point p.
Definition Rect.cpp:77