KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
rectangle.h
1/**
2 * @brief A 2D rectangle extending QRectF with edge, corner, and geometric query helpers.
3 */
4#ifndef RECTANGLE_H
5#define RECTANGLE_H
6#include <QMap>
7#include <QRectF>
8#include "point.h"
9#include "line.h"
10#include "Kanoop/kanoopcommon.h"
11
12/**
13 * @brief Extends QRectF with edge and corner accessors, containment tests, and scale/grow/shrink operations.
14 */
15class KANOOP_EXPORT Rectangle : public QRectF
16{
17public:
18 /** @brief Default constructor — creates a null rectangle. */
19 Rectangle() : QRectF() {}
20 /** @brief Construct from a QRectF.
21 * @param other Source QRectF */
22 Rectangle(const QRectF& other) : QRectF(other) {}
23 /** @brief Construct from a QRect (integer coordinates).
24 * @param other Source QRect */
25 Rectangle(const QRect& other) : QRectF(other) {}
26 /**
27 * @brief Construct from a top-left point and size.
28 * @param topLeft Top-left corner
29 * @param size Dimensions of the rectangle
30 */
31 Rectangle(const QPointF& topLeft, const QSizeF& size) : QRectF(topLeft, size) {}
32 /**
33 * @brief Construct from explicit position and dimensions.
34 * @param x Left edge X coordinate
35 * @param y Top edge Y coordinate
36 * @param w Width
37 * @param h Height
38 */
39 Rectangle(double x, double y, int w, int h) : QRectF(x, y, w, h) {}
40
41 /**
42 * @brief Build the bounding rectangle of a list of points.
43 * @param points List of points to enclose
44 * @return Bounding Rectangle
45 */
46 static Rectangle fromPoints(const Point::List& points);
47
48 /**
49 * @brief Build the bounding rectangle of two points.
50 * @param p1 First corner point
51 * @param p2 Opposite corner point
52 * @return Rectangle with p1 and p2 as opposite corners
53 */
54 static Rectangle fromPoints(const Point& p1, const Point& p2);
55
56 /**
57 * @brief Create a rectangle centred on a line with a given width.
58 * @param centerLine Centre line of the rectangle
59 * @param expand Half-width of the rectangle (perpendicular to the line)
60 * @return Rectangle enclosing the line
61 */
62 static Rectangle fromCenterLine(const Line& centerLine, double expand);
63
64 /**
65 * @brief Create a rectangle centred on a point with a given half-size.
66 * @param centerPoint Centre of the rectangle
67 * @param expand Half-dimension in each direction
68 * @return Square rectangle centred on centerPoint
69 */
70 static Rectangle fromCenterPoint(const Point& centerPoint, double expand);
71
72 /**
73 * @brief Parse a Rectangle from a string produced by toString().
74 * @param value String representation
75 * @return Parsed Rectangle
76 */
77 static Rectangle fromString(const QString& value);
78
79 /**
80 * @brief Return the corner of this rectangle closest to an origin point.
81 * @param origin Reference point
82 * @return Closest corner Point
83 */
84 Point closestCorner(const Point& origin) const;
85
86 /**
87 * @brief Return all four corners of the rectangle.
88 * @return List of the four corner Points (top-left, top-right, bottom-right, bottom-left)
89 */
91
92 /**
93 * @brief Return all four edges of the rectangle as lines.
94 * @return Line list of the four edges
95 */
97
98 /**
99 * @brief Return a map from Side to edge Line for all four edges.
100 * @return Map keyed by Geo::Side containing each edge
101 */
102 QMap<Geo::Side, Line> edgeMap() const;
103
104 /** @brief Return the two vertical edges.
105 * @return Line list containing the left and right edges */
107 /** @brief Return the two horizontal edges.
108 * @return Line list containing the top and bottom edges */
110
111 /** @brief Return the top edge as a Line.
112 * @return Top edge Line */
113 Line topEdge() const;
114 /** @brief Return the bottom edge as a Line.
115 * @return Bottom edge Line */
117 /** @brief Return the left edge as a Line.
118 * @return Left edge Line */
119 Line leftEdge() const;
120 /** @brief Return the right edge as a Line.
121 * @return Right edge Line */
123
124 /**
125 * @brief Return the edge closest to a given point.
126 * @param point Reference point
127 * @return Closest edge Line
128 */
129 Line closestEdge(const Point& point);
130
131 /**
132 * @brief Test whether a point lies on any edge, outputting the edge.
133 * @param point Point to test
134 * @param foundEdge Output set to the edge containing the point
135 * @return true if the point is on an edge
136 */
137 bool isPointOnEdge(const Point& point, Line &foundEdge) const;
138
139 /**
140 * @brief Test whether a point lies on any edge.
141 * @param point Point to test
142 * @return true if the point is on an edge
143 */
144 bool isPointOnEdge(const Point& point) const;
145
146 /**
147 * @brief Test whether a line's endpoints include any point within this rectangle.
148 * @param line Line to test
149 * @return true if either endpoint is inside the rectangle
150 */
151 bool containsAnyPoint(const Line& line) const;
152
153 /** @brief Return the geometric centre point of the rectangle.
154 * @return Centre Point */
156
157 /**
158 * @brief Return which side of the rectangle is closest to a point.
159 * @param point Reference point
160 * @return Closest Geo::Side value
161 */
162 Geo::Side closestSideToPoint(const Point &point) const;
163
164 /**
165 * @brief Scale the rectangle dimensions by a factor, keeping the top-left corner fixed.
166 * @param scale Scale factor
167 * @return Reference to this rectangle
168 */
169 Rectangle& scale(double scale);
170
171 /**
172 * @brief Return a scaled copy of this rectangle.
173 * @param scale Scale factor
174 * @return New scaled Rectangle
175 */
176 Rectangle scaled(double scale) const;
177
178 /**
179 * @brief Grow the rectangle by expanding all edges outward.
180 * @param amount Amount to expand each edge
181 * @return Reference to this rectangle
182 */
183 Rectangle& grow(double amount);
184
185 /**
186 * @brief Return a grown copy of this rectangle.
187 * @param amount Amount to expand each edge
188 * @return New grown Rectangle
189 */
190 Rectangle grown(double amount) const;
191
192 /**
193 * @brief Shrink the rectangle by contracting all edges inward.
194 * @param amount Amount to contract each edge
195 * @return Reference to this rectangle
196 */
197 Rectangle& shrink(double amount) { return grow(-amount); }
198
199 /**
200 * @brief Return a shrunk copy of this rectangle.
201 * @param amount Amount to contract each edge
202 * @return New shrunk Rectangle
203 */
204 Rectangle shrunk(double amount) const { return grown(-amount); }
205
206 /** @brief Format this rectangle as a human-readable string.
207 * @return String representation */
208 QString toString() const;
209
210 /** @brief A simple list type for Rectangle objects. */
211 class List : public QList<Rectangle> {};
212
213};
214
215#endif // RECTANGLE_H
A list of Line objects with spatial query helpers.
Definition line.h:127
Represents a 2D line segment between two Point endpoints.
Definition line.h:28
A list of Point objects with corner-finding helpers.
Definition point.h:110
A 2D floating-point point extending QPointF with movement and spatial query methods.
Definition point.h:16
A simple list type for Rectangle objects.
Definition rectangle.h:211
A 2D rectangle extending QRectF with edge, corner, and geometric query helpers.
Definition rectangle.h:16
Line rightEdge() const
Return the right edge as a Line.
Rectangle(const QRect &other)
Construct from a QRect (integer coordinates).
Definition rectangle.h:25
QMap< Geo::Side, Line > edgeMap() const
Return a map from Side to edge Line for all four edges.
Geo::Side closestSideToPoint(const Point &point) const
Return which side of the rectangle is closest to a point.
Rectangle scaled(double scale) const
Return a scaled copy of this rectangle.
bool isPointOnEdge(const Point &point) const
Test whether a point lies on any edge.
static Rectangle fromCenterPoint(const Point &centerPoint, double expand)
Create a rectangle centred on a point with a given half-size.
static Rectangle fromString(const QString &value)
Parse a Rectangle from a string produced by toString().
bool isPointOnEdge(const Point &point, Line &foundEdge) const
Test whether a point lies on any edge, outputting the edge.
QString toString() const
Format this rectangle as a human-readable string.
Rectangle(const QRectF &other)
Construct from a QRectF.
Definition rectangle.h:22
Point closestCorner(const Point &origin) const
Return the corner of this rectangle closest to an origin point.
Rectangle(const QPointF &topLeft, const QSizeF &size)
Construct from a top-left point and size.
Definition rectangle.h:31
Rectangle shrunk(double amount) const
Return a shrunk copy of this rectangle.
Definition rectangle.h:204
Rectangle()
Default constructor — creates a null rectangle.
Definition rectangle.h:19
static Rectangle fromPoints(const Point::List &points)
Build the bounding rectangle of a list of points.
Rectangle(double x, double y, int w, int h)
Construct from explicit position and dimensions.
Definition rectangle.h:39
Rectangle & shrink(double amount)
Shrink the rectangle by contracting all edges inward.
Definition rectangle.h:197
Point::List corners() const
Return all four corners of the rectangle.
Rectangle & scale(double scale)
Scale the rectangle dimensions by a factor, keeping the top-left corner fixed.
static Rectangle fromCenterLine(const Line &centerLine, double expand)
Create a rectangle centred on a line with a given width.
Point centerPoint() const
Return the geometric centre point of the rectangle.
bool containsAnyPoint(const Line &line) const
Test whether a line's endpoints include any point within this rectangle.
Line::List horizontalLines() const
Return the two horizontal edges.
Line bottomEdge() const
Return the bottom edge as a Line.
Rectangle grown(double amount) const
Return a grown copy of this rectangle.
Line leftEdge() const
Return the left edge as a Line.
static Rectangle fromPoints(const Point &p1, const Point &p2)
Build the bounding rectangle of two points.
Rectangle & grow(double amount)
Grow the rectangle by expanding all edges outward.
Line::List verticalLines() const
Return the two vertical edges.
Line closestEdge(const Point &point)
Return the edge closest to a given point.
Line topEdge() const
Return the top edge as a Line.
Line::List edges() const
Return all four edges of the rectangle as lines.
Side
Bitmask identifying which side(s) of a rectangle are relevant.
Definition geo.h:19