KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
angle.h
1/**
2 * @brief A compass bearing angle measured in degrees, with wraparound arithmetic.
3 */
4#ifndef ANGLE_H
5#define ANGLE_H
6
7#include <QString>
8#include "Kanoop/kanoopcommon.h"
9
10/**
11 * @brief Represents an angle in degrees with wraparound-aware addition and subtraction.
12 *
13 * Angles are stored in degrees. Arithmetic operators automatically keep values
14 * in the range [0, 360).
15 */
16class KANOOP_EXPORT Angle
17{
18public:
19 /** @brief Default constructor — creates an invalid angle with value -1. */
20 Angle() : _degrees(-1) {}
21
22 /**
23 * @brief Construct an Angle with the given degree value.
24 * @param degrees Angle in degrees
25 */
26 Angle(double degrees) : _degrees(degrees) {}
27
28 /**
29 * @brief Add another Angle, wrapping around 360 degrees.
30 * @param other Angle to add
31 * @return Resulting angle
32 */
33 Angle operator+(const Angle& other) const { return *this + other._degrees; }
34
35 /**
36 * @brief Add a scalar degree value, wrapping around 360 degrees.
37 * @param degrees Degrees to add
38 * @return Resulting angle
39 */
40 Angle operator+(double degrees) const;
41
42 /**
43 * @brief Subtract another Angle, wrapping around 360 degrees.
44 * @param other Angle to subtract
45 * @return Resulting angle
46 */
47 Angle operator-(const Angle& other) const { return *this - other._degrees; }
48
49 /**
50 * @brief Subtract a scalar degree value, wrapping around 360 degrees.
51 * @param degrees Degrees to subtract
52 * @return Resulting angle
53 */
54 Angle operator-(double degrees) const;
55
56 /** @brief Add another Angle in place.
57 * @param other Angle to add. */
58 void operator+=(const Angle& other) { *this = *this + other; }
59 /** @brief Add a scalar degree value in place.
60 * @param degrees Degrees to add. */
61 void operator+=(double degrees) { *this = *this + degrees; }
62 /** @brief Subtract another Angle in place.
63 * @param other Angle to subtract. */
64 void operator-=(const Angle& other) { *this = *this - other; }
65 /** @brief Subtract a scalar degree value in place.
66 * @param degrees Degrees to subtract. */
67 void operator-=(double degrees) { *this = *this - degrees; }
68
69 /**
70 * @brief Return the angle in degrees.
71 * @return Degree value
72 */
73 double degrees() const { return _degrees; }
74
75 /**
76 * @brief Set the angle in degrees.
77 * @param value New degree value
78 */
79 void setDegrees(double value) { _degrees = value; }
80
81 /**
82 * @brief Add a degree amount with wraparound in place.
83 * @param degrees Degrees to add
84 */
85 void add(double degrees);
86
87 /**
88 * @brief Subtract a degree amount with wraparound in place.
89 * @param degrees Degrees to subtract
90 */
91 void subtract(double degrees);
92
93 /**
94 * @brief Format the angle as a string.
95 * @return String of the form "X.Y deg"
96 */
97 QString toString() const { return QString("%1 deg").arg(_degrees); }
98
99private:
100 /** @brief Add an amount to a degree value with wraparound. */
101 static double add(double degrees, double amount);
102 /** @brief Subtract an amount from a degree value with wraparound. */
103 static double subtract(double degrees, double amount);
104
105 double _degrees;
106};
107
108#endif // ANGLE_H
A compass bearing angle measured in degrees, with wraparound arithmetic.
Definition angle.h:17
void subtract(double degrees)
Subtract a degree amount with wraparound in place.
void operator+=(double degrees)
Add a scalar degree value in place.
Definition angle.h:61
void operator+=(const Angle &other)
Add another Angle in place.
Definition angle.h:58
Angle(double degrees)
Construct an Angle with the given degree value.
Definition angle.h:26
void add(double degrees)
Add a degree amount with wraparound in place.
void setDegrees(double value)
Set the angle in degrees.
Definition angle.h:79
Angle operator+(const Angle &other) const
Add another Angle, wrapping around 360 degrees.
Definition angle.h:33
double degrees() const
Return the angle in degrees.
Definition angle.h:73
QString toString() const
Format the angle as a string.
Definition angle.h:97
Angle operator-(double degrees) const
Subtract a scalar degree value, wrapping around 360 degrees.
Angle()
Default constructor — creates an invalid angle with value -1.
Definition angle.h:20
void operator-=(double degrees)
Subtract a scalar degree value in place.
Definition angle.h:67
Angle operator+(double degrees) const
Add a scalar degree value, wrapping around 360 degrees.
Angle operator-(const Angle &other) const
Subtract another Angle, wrapping around 360 degrees.
Definition angle.h:47
void operator-=(const Angle &other)
Subtract another Angle in place.
Definition angle.h:64