Board  0.9.5
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 <iostream>
30 #include "board/Point.h"
31 
32 namespace LibBoard
33 {
34 
39 struct Rect {
40  double left;
41  double top;
42  double width;
43  double height;
53  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) {}
54 
62  Rect(Point topLeft, double width = 0.0, double height = 0.0) : left(topLeft.x), top(topLeft.y), width(width), height(height) {}
63 
70  Rect(Point topLeft, Point bottomRight) : left(topLeft.x), top(topLeft.y), width(bottomRight.x - topLeft.x), height(topLeft.y - bottomRight.y) {}
71 
72  Point topLeft() const { return Point(left, top); }
73  Point topRight() const { return Point(left + width, top); }
74  Point bottomLeft() const { return Point(left, top - height); }
75  Point bottomRight() const { return Point(left + width, top - height); }
76  Point center() const { return Point(left + width / 2.0, top - height / 2.0); }
77  Point centerLeft() const { return Point(left, top - height / 2.0); }
78  Point centerRight() const { return Point(left + width, top - height / 2.0); }
79  Point centerTop() const { return Point(left + width / 2.0, top); }
80  Point centerBottom() const { return Point(left + width / 2.0, top - height); }
81  double bottom() const { return top - height; }
82  double right() const { return left + width; }
83  void clear() { left = top = width = height = 0.0; }
84  void growToContain(const Point &);
85  void growToContain(const std::vector<Point> & points);
86  bool contains(Point) const;
87  bool strictlyContains(Point) const;
88  bool intersects(const Rect &) const;
89  bool strictlyIntersects(const Rect &) const;
90  Rect & grow(double margin);
91  inline bool isNull() const;
92 };
93 
102 Rect operator||(const Rect & rectA, const Rect & rectB);
103 
112 Rect operator&&(const Rect & rectA, const Rect & rectB);
113 
114 } // namespace LibBoard
115 
124 std::ostream & operator<<(std::ostream & out, const LibBoard::Rect & rect);
125 
126 // Inline methods
127 
128 namespace LibBoard
129 {
130 
131 bool Rect::isNull() const
132 {
133  return width == 0.0 && height == 0.0;
134 }
135 
136 } // namespace LibBoard
137 
138 #endif // BOARD_RECT_H
Point.h
The Point structure. @copyright This source code is part of the Board project, a C++ library whose pu...
LibBoard::operator||
Rect operator||(const Rect &rectA, const Rect &rectB)
Definition: Rect.cpp:32
LibBoard::Rect::top
double top
Definition: Rect.h:41
LibBoard::Rect
Struct representing a rectangle on the plane.
Definition: Rect.h:39
LibBoard::Rect::Rect
Rect(Point topLeft, Point bottomRight)
Definition: Rect.h:70
LibBoard::Point
Struct representing a 2D point.
Definition: Point.h:42
LibBoard::Rect::left
double left
Definition: Rect.h:40
LibBoard::Rect::Rect
Rect(double left=0.0, double top=0.0, double width=0.0, double height=0.0)
Definition: Rect.h:53
LibBoard::Rect::Rect
Rect(Point topLeft, double width=0.0, double height=0.0)
Definition: Rect.h:62
LibBoard::Rect::width
double width
Definition: Rect.h:42
LibBoard::operator&&
Rect operator&&(const Rect &rectA, const Rect &rectB)
Definition: Rect.cpp:57
operator<<
std::ostream & operator<<(std::ostream &out, const LibBoard::Rect &rect)
Definition: Rect.cpp:135
LibBoard::Rect::height
double height
Definition: Rect.h:43