KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
polygon.h
1/**
2 * @brief A 2D polygon defined by an ordered list of vertices.
3 */
4#ifndef POLYGON_H
5#define POLYGON_H
6#include "point.h"
7#include "line.h"
8
9/**
10 * @brief Represents a 2D polygon as an ordered list of Point vertices.
11 *
12 * Provides area, centroid, containment, and movement operations.
13 */
14class KANOOP_EXPORT Polygon
15{
16public:
17 /** @brief Default constructor — creates an empty (invalid) polygon. */
19
20 /**
21 * @brief Construct a rectangle-shaped polygon from a line expanded by a width.
22 * @param line Centre line of the polygon
23 * @param expandBy Half-width of the resulting rectangular polygon
24 * @return Rectangular Polygon centred on the line
25 */
26 static Polygon fromLine(const Line& line, int expandBy);
27
28 /**
29 * @brief Append a vertex to the polygon.
30 * @param point Vertex to add
31 */
32 void appendPoint(const Point& point) { _points.append(point); }
33
34 /**
35 * @brief Compute the area of the polygon using the shoelace formula.
36 * @return Signed area (positive for counter-clockwise winding)
37 */
38 double area() const;
39
40 /**
41 * @brief Move all vertices by a bearing and distance.
42 * @param bearing Direction of movement in degrees
43 * @param distance Distance to move
44 */
45 void move(double bearing, double distance);
46
47 /**
48 * @brief Rotate all vertices around a centroid by an angle.
49 * @param centroid Centre of rotation
50 * @param angle Rotation angle in degrees
51 */
52 void rotate(const Point& centroid, double angle);
53
54 /**
55 * @brief Compute the centroid (geometric centre) of the polygon.
56 * @return Centroid Point
57 */
58 Point centroid() const;
59
60 /**
61 * @brief Test whether a point lies inside the polygon.
62 * @param point Point to test
63 * @return true if the point is inside the polygon boundary
64 */
65 bool contains(const Point& point) const;
66
67 /**
68 * @brief Return the edges of the polygon as a list of lines.
69 * @return Line list connecting consecutive vertices
70 */
72
73 /**
74 * @brief Return the bounding rectangle of this polygon.
75 * @return Smallest Rectangle enclosing all vertices
76 */
78
79 /**
80 * @brief Test whether this polygon has at least 3 vertices.
81 * @return true if the polygon has enough vertices to be valid
82 */
83 bool isValid() const { return _points.count() > 2; }
84
85private:
86 /** @brief Build the polygon from a list of edges. */
87 void createFromLines(const Line::List &lines);
88 /** @brief Return the minimum coordinate for winding calculations. */
89 double minimumXY() const;
90
91 Point::List _points;
92};
93
94#endif // POLYGON_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 2D polygon defined by an ordered list of vertices.
Definition polygon.h:15
bool isValid() const
Test whether this polygon has at least 3 vertices.
Definition polygon.h:83
Line::List lines() const
Return the edges of the polygon as a list of lines.
void appendPoint(const Point &point)
Append a vertex to the polygon.
Definition polygon.h:32
double area() const
Compute the area of the polygon using the shoelace formula.
Point centroid() const
Compute the centroid (geometric centre) of the polygon.
Rectangle boundingRectangle() const
Return the bounding rectangle of this polygon.
static Polygon fromLine(const Line &line, int expandBy)
Construct a rectangle-shaped polygon from a line expanded by a width.
bool contains(const Point &point) const
Test whether a point lies inside the polygon.
Polygon()
Default constructor — creates an empty (invalid) polygon.
Definition polygon.h:18
void rotate(const Point &centroid, double angle)
Rotate all vertices around a centroid by an angle.
void move(double bearing, double distance)
Move all vertices by a bearing and distance.
A 2D rectangle extending QRectF with edge, corner, and geometric query helpers.
Definition rectangle.h:16