TeiaCareSDK  v0.1.0
TeiaCareSDK is a collection of reusable C++ components
Loading...
Searching...
No Matches
point.hpp
1// Copyright 2024 TeiaCare
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <cmath>
18#include <string>
19
20namespace tc::sdk
21{
22/*!
23 * \class point
24 * \brief 2D point specified by a pair of (x,y) coordinates.
25 * \tparam T Coordinates type
26 *
27 */
28template <typename T>
29class point
30{
31public:
32 /*!
33 * \brief Default Constructor. Creates a default tc::sdk::point instance.
34 */
35 constexpr point() = default;
36
37 /*!
38 * \brief Copy Constructor. Copy a tc::sdk::point instance into another one.
39 */
40 constexpr point(const point&) = default;
41
42 /*!
43 * \brief Move Constructor. Copy a tc::sdk::point instance into another one.
44 */
45 constexpr point(point&&) = default;
46
47 /*!
48 * \brief Constructor. Creates tc::sdk::point instance with given X and Y coordinates.
49 * \param x the X coordinate of the point
50 * \param y the Y coordinate of the point
51 */
52 constexpr explicit point(T x, T y) noexcept
53 : _x{x}
54 , _y{y}
55 {
56 }
57
58 /*!
59 * \brief Assignment operator. Assign a tc::sdk::point instance to another one.
60 */
61 point& operator=(const point&) = default;
62
63 /*!
64 * \brief Move assignment operator. Assign a tc::sdk::point instance to another one.
65 */
66 point& operator=(point&&) = default;
67
68 /*!
69 * \brief Equality operator.
70 * \param other the point to compare against.
71 * \return true if the two points have the same coordinates.
72 */
73 constexpr inline bool operator==(const point& other) const noexcept
74 {
75 return _x == other._x && _y == other._y;
76 }
77
78 /*!
79 * \brief Inequality operator.
80 * \param other the point to compare against.
81 * \return true if the two points have the different coordinates.
82 */
83 constexpr inline bool operator!=(const point& other) const noexcept
84 {
85 return !operator==(other);
86 }
87
88 /*!
89 * \brief Check if the point is the origin.
90 * \return true if the point has coordinates (0,0)
91 */
92 constexpr bool is_origin() const noexcept
93 {
94 return *this == point();
95 }
96
97 /*!
98 * \brief X coordinate getter.
99 * \return X coordinate of the point.
100 */
101 constexpr inline T x() const noexcept
102 {
103 return _x;
104 }
105
106 /*!
107 * \brief Y coordinate getter.
108 * \return Y coordinate of the point.
109 */
110 constexpr inline T y() const noexcept
111 {
112 return _y;
113 }
114
115 /*!
116 * \brief X coordinate setter. Set the X coordinate of the point.
117 */
118 inline void set_x(T x) noexcept
119 {
120 _x = x;
121 }
122
123 /*!
124 * \brief Y coordinate setter. Set the Y coordinate of the point.
125 */
126 inline void set_y(T y) noexcept
127 {
128 _y = y;
129 }
130
131 /*!
132 * \brief Increments point's coordinate of delta_x and delta_y values.
133 * \param delta_x Value to be added to the current X coordinate.
134 * \param delta_y Value to be added to the current Y coordinate.
135 */
136 void add_delta(T delta_x, T delta_y) noexcept
137 {
138 _x += delta_x;
139 _y += delta_y;
140 }
141
142 /*!
143 * \brief Decrements point's coordinate of delta_x and delta_y values.
144 * \param delta_x Value to be added to the current X coordinate.
145 * \param delta_y Value to be added to the current Y coordinate.
146 */
147 void sub_delta(T delta_x, T delta_y) noexcept
148 {
149 _x -= delta_x;
150 _y -= delta_y;
151 }
152
153 /*!
154 * \brief Distance between the current point and the origin.
155 * \return The resulting distance.
156 */
158 {
159 return static_cast<T>(std::sqrt(_x * _x + _y * _y));
160 }
161
162 /*!
163 * \brief Distance between the current point and another one.
164 * \param other The point to compute the distance from.
165 * \return The resulting distance.
166 */
167 constexpr T distance(point other) const noexcept
168 {
169 T dx = other._x - _x;
170 T dy = other._y - _y;
171 return static_cast<T>(std::sqrt(dx * dx + dy * dy));
172 }
173
174 /*!
175 * \brief Add two points.
176 * \param other The point to be added to the current one.
177 * \return The resulting point.
178 */
179 constexpr point operator+(point other) const noexcept
180 {
181 return point{_x + other._x, _y + other._y};
182 }
183
184 /*!
185 * \brief Add another point's coordinate to the current one.
186 * \param other The point to be added to the current one.
187 * \return The current point instance after the addition of the other.
188 */
190 {
191 _x += other._x;
192 _y += other._y;
193 return *this;
194 }
195
196 /*!
197 * \brief Subtract two points.
198 * \param other The point to be subtracted to the current one.
199 * \return The resulting point.
200 */
201 constexpr point operator-(point other) const noexcept
202 {
203 return point{_x - other._x, _y - other._y};
204 }
205
206 /*!
207 * \brief Subtract another point's coordinate to the current one.
208 * \param other The point to be subtracted to the current one.
209 * \return The current point instance after the subtraction of the other.
210 */
212 {
213 _x -= other._x;
214 _y -= other._y;
215 return *this;
216 }
217
218 /*!
219 * \brief Multiply current point's coordinate by a scalar value.
220 * \param scalar The scalar value to multiply the current point with.
221 * \return The current point instance after the scalar multiplication with the scalar value.
222 */
223 template <typename ScalarT>
225 {
226 _x = static_cast<T>(_x * scalar);
227 _y = static_cast<T>(_y * scalar);
228 return *this;
229 }
230
231 /*!
232 * \brief Multiply a point by a given scalar value.
233 * \param scalar The scalar value to multiply the resulting point with.
234 * \return The resulting point.
235 */
236 template <typename ScalarT>
237 constexpr point operator*(ScalarT scalar) const noexcept
238 {
239 return point{static_cast<T>(_x * scalar), static_cast<T>(_y * scalar)};
240 }
241
242 /*!
243 * \brief Divide current point's coordinate by a scalar value.
244 * \param scalar The scalar value to multiply the current point with.
245 * \return The current point instance after the scalar multiplication with the scalar value.
246 */
247 template <typename ScalarT>
249 {
250 _x = static_cast<T>(_x / scalar);
251 _y = static_cast<T>(_y / scalar);
252 return *this;
253 }
254
255 /*!
256 * \brief Divide a point by a given scalar value.
257 * \param scalar The scalar value to divide the resulting point with.
258 * \return The resulting point.
259 */
260 template <typename ScalarT>
261 constexpr point operator/(ScalarT scalar) const
262 {
263 return point{static_cast<T>(_x / scalar), static_cast<T>(_y / scalar)};
264 }
265
266 /*!
267 * \brief Get the point string representation
268 * \return String representation of the current point.
269 */
270 constexpr std::string to_string() const
271 {
272 return std::string("(" + std::to_string(_x) + ", " + std::to_string(_y) + ")");
273 }
274
275 /*!
276 * \brief Output stream operator.
277 * \param stream the output stream to write into.
278 * \param p the point object to stream.
279 * \return reference to the output stream operator, with the point string representation written into it.
280 */
281 friend std::ostream& operator<<(std::ostream& stream, const point& p)
282 {
283 return stream << p.to_string();
284 }
285
286private:
287 T _x{};
288 T _y{};
289};
290
291}
Thread safe, blocking queue.
2D point specified by a pair of (x,y) coordinates.
Definition point.hpp:30
constexpr T x() const noexcept
X coordinate getter.
Definition point.hpp:101
void add_delta(T delta_x, T delta_y) noexcept
Increments point's coordinate of delta_x and delta_y values.
Definition point.hpp:136
constexpr T distance_from_origin() const noexcept
Distance between the current point and the origin.
Definition point.hpp:157
constexpr point(const point &)=default
Copy Constructor. Copy a tc::sdk::point instance into another one.
constexpr T y() const noexcept
Y coordinate getter.
Definition point.hpp:110
constexpr point(point &&)=default
Move Constructor. Copy a tc::sdk::point instance into another one.
constexpr point operator/(ScalarT scalar) const
Divide a point by a given scalar value.
Definition point.hpp:261
constexpr point()=default
Default Constructor. Creates a default tc::sdk::point instance.
void set_y(T y) noexcept
Y coordinate setter. Set the Y coordinate of the point.
Definition point.hpp:126
constexpr point operator-(point other) const noexcept
Subtract two points.
Definition point.hpp:201
constexpr std::string to_string() const
Get the point string representation.
Definition point.hpp:270
constexpr bool operator==(const point &other) const noexcept
Equality operator.
Definition point.hpp:73
void set_x(T x) noexcept
X coordinate setter. Set the X coordinate of the point.
Definition point.hpp:118
point & operator-=(point other) noexcept
Subtract another point's coordinate to the current one.
Definition point.hpp:211
constexpr point(T x, T y) noexcept
Constructor. Creates tc::sdk::point instance with given X and Y coordinates.
Definition point.hpp:52
constexpr bool operator!=(const point &other) const noexcept
Inequality operator.
Definition point.hpp:83
constexpr point operator*(ScalarT scalar) const noexcept
Multiply a point by a given scalar value.
Definition point.hpp:237
point & operator+=(point other) noexcept
Add another point's coordinate to the current one.
Definition point.hpp:189
point & operator=(const point &)=default
Assignment operator. Assign a tc::sdk::point instance to another one.
constexpr bool is_origin() const noexcept
Check if the point is the origin.
Definition point.hpp:92
constexpr T distance(point other) const noexcept
Distance between the current point and another one.
Definition point.hpp:167
point & operator/=(ScalarT scalar)
Divide current point's coordinate by a scalar value.
Definition point.hpp:248
constexpr point operator+(point other) const noexcept
Add two points.
Definition point.hpp:179
void sub_delta(T delta_x, T delta_y) noexcept
Decrements point's coordinate of delta_x and delta_y values.
Definition point.hpp:147
point & operator=(point &&)=default
Move assignment operator. Assign a tc::sdk::point instance to another one.
point & operator*=(ScalarT scalar) noexcept
Multiply current point's coordinate by a scalar value.
Definition point.hpp:224
friend std::ostream & operator<<(std::ostream &stream, const point &p)
Output stream operator.
Definition point.hpp:281