KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
point.h
1/**
2 * @brief A 2D floating-point point extending QPointF with movement and spatial query methods.
3 */
4#ifndef POINT_H
5#define POINT_H
6#include <QList>
7#include <QPointF>
8#include "Kanoop/kanoopcommon.h"
9
10#include "geo.h"
11
12/**
13 * @brief Extends QPointF with movement transforms, string conversion, and a list subclass.
14 */
15class KANOOP_EXPORT Point : public QPointF
16{
17public:
18 /** @brief Default constructor — creates a point at the origin. */
19 Point() : QPointF() {}
20 /** @brief Construct from a QPointF.
21 * @param other Source QPointF */
22 Point(const QPointF& other) : QPointF(other) {}
23 /** @brief Construct from a QPoint (integer coordinates).
24 * @param other Source QPoint */
25 Point(const QPoint& other) : QPointF(other) {}
26 /**
27 * @brief Construct from explicit X and Y coordinates.
28 * @param x X coordinate
29 * @param y Y coordinate
30 */
31 Point(double x, double y) : QPointF(x, y) {}
32
33 /** @brief Test whether this point is to the left of other (smaller X).
34 * @param other Reference point
35 * @return true if this point's X is less than other's X */
36 bool isLeftOf(const QPointF& other) const { return x() < other.x(); }
37 /** @brief Test whether this point is to the right of other (larger X).
38 * @param other Reference point
39 * @return true if this point's X is greater than other's X */
40 bool isRightOf(const QPointF& other) const { return x() > other.x(); }
41 /** @brief Test whether this point is above other (smaller Y).
42 * @param other Reference point
43 * @return true if this point's Y is less than other's Y */
44 bool isAbove(const QPointF& other) const { return y() < other.y(); }
45 /** @brief Test whether this point is below other (larger Y).
46 * @param other Reference point
47 * @return true if this point's Y is greater than other's Y */
48 bool isBelow(const QPointF& other) const { return y() > other.y(); }
49
50 /**
51 * @brief Move this point in a cardinal direction by the given amount.
52 * @param direction Cardinal direction to move
53 * @param amount Distance to move
54 * @return Reference to this point
55 */
56 Point& move(Geo::Direction direction, double amount);
57
58 /**
59 * @brief Move this point by a bearing and distance.
60 * @param bearing Compass bearing in degrees
61 * @param distance Distance to move
62 * @return Reference to this point
63 */
64 Point& move(double bearing, double distance);
65
66 /**
67 * @brief Translate this point by a (dx, dy) delta.
68 * @param dx Horizontal displacement
69 * @param dy Vertical displacement
70 * @return Reference to this point
71 */
72 Point& moveDelta(double dx, double dy);
73
74 /** @brief Round both coordinates to the nearest integer in place.
75 * @return Reference to this point */
77
78 /**
79 * @brief Offset this point by the given (x, y) amounts.
80 * @param x Horizontal offset
81 * @param y Vertical offset
82 * @return Reference to this point
83 */
84 Point& offset(double x, double y);
85
86 /**
87 * @brief Compute the (dx, dy) vector from this point to another.
88 * @param other Target point
89 * @return Delta point representing the displacement
90 */
91 Point delta(const Point& other) const;
92
93 /**
94 * @brief Format this point as "x, y".
95 * @return String representation
96 */
97 QString toString() const { return QString("%1, %2").arg(x()).arg(y()); }
98
99 /**
100 * @brief Parse a Point from a string produced by toString().
101 * @param value String of the form "x, y"
102 * @return Parsed Point
103 */
104 static Point fromString(const QString& value);
105
106 /**
107 * @brief A list of Point objects with corner-finding helpers.
108 */
109 class KANOOP_EXPORT List : public QList<Point>
110 {
111 public:
112 /** @brief Default constructor. */
113 List() {}
114
115 /** @brief Construct from a QList<Point>.
116 * @param other Source list of Points */
117 List(const QList<Point>& other)
118 {
119 for(const Point& p : other) {
120 append(p);
121 }
122 }
123
124 /** @brief Return the top-left point (minimum X and Y).
125 * @return Top-left Point */
126 Point topLeft() const;
127 /** @brief Return the top-right point (maximum X, minimum Y).
128 * @return Top-right Point */
130 /** @brief Return the bottom-left point (minimum X, maximum Y).
131 * @return Bottom-left Point */
133 /** @brief Return the bottom-right point (maximum X and Y).
134 * @return Bottom-right Point */
136
137 /** @brief Convert to a list of integer QPoint objects.
138 * @return QList of QPoint values */
139 QList<QPoint> toPointList() const
140 {
141 QList<QPoint> result;
142 for(const Point& point : *this) {
143 result.append(point.toPoint());
144 }
145 return result;
146 }
147 };
148};
149
150#endif // POINT_H
A list of Point objects with corner-finding helpers.
Definition point.h:110
QList< QPoint > toPointList() const
Convert to a list of integer QPoint objects.
Definition point.h:139
Point bottomRight() const
Return the bottom-right point (maximum X and Y).
Point topLeft() const
Return the top-left point (minimum X and Y).
Point topRight() const
Return the top-right point (maximum X, minimum Y).
Point bottomLeft() const
Return the bottom-left point (minimum X, maximum Y).
List()
Default constructor.
Definition point.h:113
List(const QList< Point > &other)
Construct from a QList<Point>.
Definition point.h:117
A 2D floating-point point extending QPointF with movement and spatial query methods.
Definition point.h:16
bool isBelow(const QPointF &other) const
Test whether this point is below other (larger Y).
Definition point.h:48
static Point fromString(const QString &value)
Parse a Point from a string produced by toString().
Point & move(Geo::Direction direction, double amount)
Move this point in a cardinal direction by the given amount.
bool isLeftOf(const QPointF &other) const
Test whether this point is to the left of other (smaller X).
Definition point.h:36
Point(const QPointF &other)
Construct from a QPointF.
Definition point.h:22
QString toString() const
Format this point as "x, y".
Definition point.h:97
bool isAbove(const QPointF &other) const
Test whether this point is above other (smaller Y).
Definition point.h:44
Point(double x, double y)
Construct from explicit X and Y coordinates.
Definition point.h:31
Point(const QPoint &other)
Construct from a QPoint (integer coordinates).
Definition point.h:25
Point & round()
Round both coordinates to the nearest integer in place.
Point & moveDelta(double dx, double dy)
Translate this point by a (dx, dy) delta.
Point & offset(double x, double y)
Offset this point by the given (x, y) amounts.
Point delta(const Point &other) const
Compute the (dx, dy) vector from this point to another.
Point()
Default constructor — creates a point at the origin.
Definition point.h:19
bool isRightOf(const QPointF &other) const
Test whether this point is to the right of other (larger X).
Definition point.h:40
Point & move(double bearing, double distance)
Move this point by a bearing and distance.
Direction
Cardinal directions, aliased to the corresponding Side values.
Definition geo.h:37