KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
flatgeo.h
1/**
2 * @brief Static 2D planar geometry calculation helpers.
3 */
4#ifndef FLATGEO_H
5#define FLATGEO_H
6
7#include "point.h"
8#include "Kanoop/kanoopcommon.h"
9#include <QRect>
10
11class Circle;
12class Line;
13class Angle;
14class QRect;
15
16/**
17 * @brief Static helper methods for 2D (flat / Cartesian) geometric computations.
18 *
19 * All coordinates use Qt's screen coordinate system where Y increases downward.
20 * Angles are in degrees unless otherwise specified.
21 */
22class KANOOP_EXPORT FlatGeo
23{
24public:
25 /**
26 * @brief Compute the signed vector angle between two lines.
27 * @param l1 First line
28 * @param l2 Second line
29 * @return Signed angle in degrees
30 */
31 static double vectorAngle(const Line& l1, const Line& l2);
32
33 /**
34 * @brief Compute the angle between two lines as an Angle object.
35 * @param l1 First line
36 * @param l2 Second line
37 * @return Angle between the lines
38 */
39 static Angle angle(const Line& l1, const Line& l2);
40
41 /**
42 * @brief Compute the angle at a vertex formed by two rays.
43 * @param p1 First point on the first ray
44 * @param vertex Vertex of the angle
45 * @param p2 First point on the second ray
46 * @return Angle at the vertex
47 */
48 static Angle angle(const QPointF& p1, const QPointF& vertex, const QPointF& p2);
49
50 /**
51 * @brief Compute the Euclidean distance between two points.
52 * @param p1 First point
53 * @param p2 Second point
54 * @return Distance in scene units
55 */
56 static double distance(const QPointF& p1, const QPointF& p2);
57
58 /**
59 * @brief Convert radians to degrees.
60 * @param angle Angle in radians
61 * @return Equivalent angle in degrees
62 */
63 static double degrees(double angle);
64
65 /**
66 * @brief Convert degrees to radians.
67 * @param angle Angle in degrees
68 * @return Equivalent angle in radians
69 */
70 static double radians(double angle);
71
72 /**
73 * @brief Compute the absolute angular difference between two lines.
74 * @param l1 First line
75 * @param l2 Second line
76 * @return Absolute angular difference in degrees
77 */
78 static double angularDifference(const Line& l1, const Line& l2);
79
80 /**
81 * @brief Compute a point at a given bearing and distance from a source point.
82 * @param from Origin point
83 * @param bearing Compass bearing in degrees (0 = up/north)
84 * @param distance Distance from origin
85 * @return Resulting Point
86 */
87 static Point getPoint(const QPointF& from, double bearing, double distance);
88
89 /**
90 * @brief Compute the intersection point of two lines.
91 * @param l1 First line
92 * @param l2 Second line
93 * @return Intersection point, or an undefined point if lines are parallel
94 */
95 static QPointF intersection(const Line& l1, const Line& l2);
96
97 /**
98 * @brief Test whether a point lies within a circle.
99 * @param point Point to test
100 * @param circle Circle to test against
101 * @return true if the point is inside or on the circle boundary
102 */
103 static bool isInCircle(const QPointF& point, const Circle& circle);
104
105 /**
106 * @brief Compute the A, B, C coefficients of the line equation Ax + By + C = 0.
107 * @param line Input line
108 * @param a Output coefficient A
109 * @param b Output coefficient B
110 * @param c Output coefficient C
111 */
112 static void getLineABC(const Line& line, double& a, double& b, double& c);
113
114 /**
115 * @brief Move a point by a given bearing and distance.
116 * @param point Starting point
117 * @param bearing Direction of movement in degrees
118 * @param distance Distance to move
119 * @return New QPointF after movement
120 */
121 static QPointF move(const QPointF& point, double bearing, double distance);
122
123 /**
124 * @brief Rotate a point around a centroid by an angle.
125 * @param point Point to rotate
126 * @param centroid Centre of rotation
127 * @param angle Rotation angle in degrees (clockwise)
128 * @return Rotated QPointF
129 */
130 static QPointF rotate(const QPointF& point, const QPointF& centroid, double angle);
131
132 /**
133 * @brief Test whether two points are equal within a given precision.
134 * @param p1 First point
135 * @param p2 Second point
136 * @param precision Number of decimal places to compare (0 = integer precision)
137 * @return true if the points are equal within the specified precision
138 */
139 static bool arePointsEqual(const QPointF& p1, const QPointF& p2, int precision = 0);
140
141 /** @brief Test whether point is to the left of other (smaller X).
142 * @param point Point to test.
143 * @param other Reference point.
144 * @return True if point.x < other.x. */
145 static inline bool isPointLeftOf(const QPointF& point, const QPointF& other)
146 {
147 return point.x() < other.x();
148 }
149 /** @brief Test whether point is to the right of other (larger X).
150 * @param point Point to test.
151 * @param other Reference point.
152 * @return True if point.x > other.x. */
153 static inline bool isPointRightOf(const QPointF& point, const QPointF& other)
154 {
155 return point.x() > other.x();
156 }
157 /** @brief Test whether point is above other (smaller Y).
158 * @param point Point to test.
159 * @param other Reference point.
160 * @return True if point.y < other.y. */
161 static inline bool isPointAbove(const QPointF& point, const QPointF& other)
162 {
163 return point.y() < other.y();
164 }
165 /** @brief Test whether point is below other (larger Y).
166 * @param point Point to test.
167 * @param other Reference point.
168 * @return True if point.y > other.y. */
169 static inline bool isPointBelow(const QPointF& point, const QPointF& other)
170 {
171 return point.y() > other.y();
172 }
173 /** @brief Test whether rect's right edge is left of other.
174 * @param rect Rectangle to test.
175 * @param other Reference point.
176 * @return True if rect's right edge < other.x. */
177 static inline bool isRectLeftOf(const QRectF& rect, const QPointF& other)
178 {
179 return rect.x() + rect.width() < other.x();
180 }
181 /** @brief Test whether rect's left edge is right of other.
182 * @param rect Rectangle to test.
183 * @param other Reference point.
184 * @return True if rect.x > other.x. */
185 static inline bool isRectRightOf(const QRectF& rect, const QPointF& other)
186 {
187 return rect.x() > other.x();
188 }
189 /** @brief Test whether rect's bottom edge is above other.
190 * @param rect Rectangle to test.
191 * @param other Reference point.
192 * @return True if rect's bottom < other.y. */
193 static inline bool isRectAbove(const QRectF& rect, const QPointF& other)
194 {
195 return rect.y() + rect.height() < other.y();
196 }
197 /** @brief Test whether rect's top edge is below other.
198 * @param rect Rectangle to test.
199 * @param other Reference point.
200 * @return True if rect.y > other.y. */
201 static inline bool isRectBelow(const QRectF& rect, const QPointF& other)
202 {
203 return rect.y() > other.y();
204 }
205 /** @brief Test whether other is to the left of rect.
206 * @param rect Reference rectangle
207 * @param other Rectangle to compare
208 * @return true if other.x < rect.x */
209 static inline bool isRectLeftOf(const QRectF& rect, const QRectF& other)
210 {
211 return other.x() < rect.x();
212 }
213 /** @brief Test whether other is to the right of rect.
214 * @param rect Reference rectangle
215 * @param other Rectangle to compare
216 * @return true if other.x > rect's right edge */
217 static inline bool isRectRightOf(const QRectF& rect, const QRectF& other)
218 {
219 return other.x() > rect.x() + rect.width();
220 }
221 /** @brief Test whether other is above rect.
222 * @param rect Reference rectangle
223 * @param other Rectangle to compare
224 * @return true if other.y < rect.y */
225 static inline bool isRectAbove(const QRectF& rect, const QRectF& other)
226 {
227 return other.y() < rect.y();
228 }
229 /** @brief Test whether other is below rect.
230 * @param rect Reference rectangle
231 * @param other Rectangle to compare
232 * @return true if other.y > rect's bottom edge */
233 static inline bool isRectBelow(const QRectF& rect, const QRectF& other)
234 {
235 return other.y() > rect.y() + rect.height();
236 }
237
238 /**
239 * @brief Determine the spatial relationship of a point relative to an origin point.
240 * @param origin Reference point
241 * @param other Point to compare
242 * @return SpatialRelationship flags describing the relative position
243 */
244 static Geo::SpatialRelationship relationTo(const QPointF& origin, const QPointF& other);
245
246 /**
247 * @brief Determine the spatial relationship of a point relative to a rectangle.
248 * @param origin Reference rectangle
249 * @param other Point to compare
250 * @return SpatialRelationship flags describing the relative position
251 */
252 static Geo::SpatialRelationship relationTo(const QRectF& origin, const QPointF& other);
253
254 /**
255 * @brief Determine the spatial relationship of a rectangle relative to an origin point.
256 * @param origin Reference point
257 * @param other Rectangle to compare
258 * @return SpatialRelationship flags describing the relative position
259 */
260 static Geo::SpatialRelationship relationTo(const QPointF& origin, const QRectF& other);
261
262 /**
263 * @brief Determine the spatial relationship between two rectangles.
264 * @param origin Reference rectangle
265 * @param other Rectangle to compare
266 * @return SpatialRelationship flags describing the relative position
267 */
268 static Geo::SpatialRelationship relationTo(const QRectF& origin, const QRectF& other);
269
270 /**
271 * @brief Format an integer point as a comma-separated string.
272 * @param p Point to format
273 * @return String of the form "x, y"
274 */
275 static QString makePointString(const QPoint& p);
276
277 /**
278 * @brief Format a floating-point point as a comma-separated string.
279 * @param p Point to format
280 * @return String of the form "x, y"
281 */
282 static QString makePointString(const QPointF& p);
283
284 /** @brief Number of decimal places used for rounding in geometric comparisons. */
285 static const int Precision = 6;
286
287private:
288 /** @brief Alternative angle computation implementation. */
289 static Angle anglev2(const Line& l1, const Line& l2);
290 /** @brief Format a double rounded to the given precision as a string. */
291 static QString roundedString(double value, int precision);
292 /** @brief Round a double to the given number of decimal places. */
293 static double roundedDouble(double value, int precision);
294};
295
296
297#endif // FLATGEO_H
A compass bearing angle measured in degrees, with wraparound arithmetic.
Definition angle.h:17
Represents a 2D circle with centre and radius, supporting geometric queries.
Definition circle.h:17
Static helper methods for 2D (flat / Cartesian) geometric computations.
Definition flatgeo.h:23
static bool isRectAbove(const QRectF &rect, const QRectF &other)
Test whether other is above rect.
Definition flatgeo.h:225
static QPointF rotate(const QPointF &point, const QPointF &centroid, double angle)
Rotate a point around a centroid by an angle.
static bool isRectLeftOf(const QRectF &rect, const QRectF &other)
Test whether other is to the left of rect.
Definition flatgeo.h:209
static void getLineABC(const Line &line, double &a, double &b, double &c)
Compute the A, B, C coefficients of the line equation Ax + By + C = 0.
static double angularDifference(const Line &l1, const Line &l2)
Compute the absolute angular difference between two lines.
static double distance(const QPointF &p1, const QPointF &p2)
Compute the Euclidean distance between two points.
static bool isPointLeftOf(const QPointF &point, const QPointF &other)
Test whether point is to the left of other (smaller X).
Definition flatgeo.h:145
static QPointF intersection(const Line &l1, const Line &l2)
Compute the intersection point of two lines.
static bool isPointBelow(const QPointF &point, const QPointF &other)
Test whether point is below other (larger Y).
Definition flatgeo.h:169
static bool isPointRightOf(const QPointF &point, const QPointF &other)
Test whether point is to the right of other (larger X).
Definition flatgeo.h:153
static bool isRectRightOf(const QRectF &rect, const QRectF &other)
Test whether other is to the right of rect.
Definition flatgeo.h:217
static bool isPointAbove(const QPointF &point, const QPointF &other)
Test whether point is above other (smaller Y).
Definition flatgeo.h:161
static bool isRectBelow(const QRectF &rect, const QPointF &other)
Test whether rect's top edge is below other.
Definition flatgeo.h:201
static Angle angle(const Line &l1, const Line &l2)
Compute the angle between two lines as an Angle object.
static Geo::SpatialRelationship relationTo(const QRectF &origin, const QRectF &other)
Determine the spatial relationship between two rectangles.
static bool isInCircle(const QPointF &point, const Circle &circle)
Test whether a point lies within a circle.
static double degrees(double angle)
Convert radians to degrees.
static QPointF move(const QPointF &point, double bearing, double distance)
Move a point by a given bearing and distance.
static Geo::SpatialRelationship relationTo(const QRectF &origin, const QPointF &other)
Determine the spatial relationship of a point relative to a rectangle.
static Geo::SpatialRelationship relationTo(const QPointF &origin, const QPointF &other)
Determine the spatial relationship of a point relative to an origin point.
static double vectorAngle(const Line &l1, const Line &l2)
Compute the signed vector angle between two lines.
static Angle angle(const QPointF &p1, const QPointF &vertex, const QPointF &p2)
Compute the angle at a vertex formed by two rays.
static Geo::SpatialRelationship relationTo(const QPointF &origin, const QRectF &other)
Determine the spatial relationship of a rectangle relative to an origin point.
static bool isRectAbove(const QRectF &rect, const QPointF &other)
Test whether rect's bottom edge is above other.
Definition flatgeo.h:193
static double radians(double angle)
Convert degrees to radians.
static QString makePointString(const QPoint &p)
Format an integer point as a comma-separated string.
static bool isRectRightOf(const QRectF &rect, const QPointF &other)
Test whether rect's left edge is right of other.
Definition flatgeo.h:185
static QString makePointString(const QPointF &p)
Format a floating-point point as a comma-separated string.
static Point getPoint(const QPointF &from, double bearing, double distance)
Compute a point at a given bearing and distance from a source point.
static bool isRectBelow(const QRectF &rect, const QRectF &other)
Test whether other is below rect.
Definition flatgeo.h:233
static bool isRectLeftOf(const QRectF &rect, const QPointF &other)
Test whether rect's right edge is left of other.
Definition flatgeo.h:177
static bool arePointsEqual(const QPointF &p1, const QPointF &p2, int precision=0)
Test whether two points are equal within a given precision.
Represents a 2D line segment between two Point endpoints.
Definition line.h:28
A 2D floating-point point extending QPointF with movement and spatial query methods.
Definition point.h:16
SpatialRelationship
Spatial relationship between two geometric objects.
Definition geo.h:50