FifeGUI 0.3.0
A C++ GUI library designed for games.
point.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_POINT_HPP_
6#define INCLUDE_FIFECHAN_POINT_HPP_
7
8// Standard library includes
9#include <array>
10#include <cassert>
11#include <iostream>
12#include <utility>
13#include <vector>
14
15// Platform config include
16#include "fifechan/platform.hpp"
17
18// Project headers (subdirs before local)
19#include "fifechan/math.hpp"
20
21namespace fcn
22{
23
33 class Point
34 {
35 public:
36 union
37 {
38 std::array<int, 2> val;
39 struct
40 {
41 int x, y;
42 };
43 };
44
53 explicit Point(int _x = 0, int _y = 0) : x(_x), y(_y)
54 {
55 }
56
57 ~Point() = default;
58
64 Point(Point const & rhs) : x(rhs.x), y(rhs.y)
65 {
66 }
67
73 Point(Point&& rhs) noexcept : x(rhs.x), y(rhs.y)
74 {
75 }
76
82 Point& operator=(Point const & rhs)
83 {
84 if (this != &rhs) {
85 val = rhs.val;
86 }
87 return *this;
88 }
89
95 Point& operator=(Point&& rhs) noexcept
96 {
97 if (this != &rhs) {
98 val = rhs.val;
99 }
100 return *this;
101 }
102
109 Point operator+(Point const & p) const
110 {
111 return Point(x + p.x, y + p.y);
112 }
113
120 Point operator-(Point const & p) const
121 {
122 return Point(x - p.x, y - p.y);
123 }
124
132 {
133 x += p.x;
134 y += p.y;
135 return *this;
136 }
137
145 {
146 x -= p.x;
147 y -= p.y;
148 return *this;
149 }
150
157 Point operator*(int const & i) const
158 {
159 return Point(x * i, y * i);
160 }
161
168 Point operator/(int const & i) const
169 {
170 return Point(x / i, y / i);
171 }
172
179 bool operator==(Point const & p) const
180 {
181 return x == p.x && y == p.y;
182 }
183
190 bool operator!=(Point const & p) const
191 {
192 return x != p.x || y != p.y;
193 }
194
198 int length() const
199 {
200 double const sq = (static_cast<double>(x) * x) + (static_cast<double>(y) * y);
201 return static_cast<int>(Mathf::Sqrt(sq));
202 }
203
208 {
209 float const len = length();
210
211 if (len > Mathf::zeroTolerance()) {
212 float const invLength = 1.0F / len;
213
214 x = static_cast<int>(x * invLength);
215 y = static_cast<int>(y * invLength);
216 } else {
217 x = 0;
218 y = 0;
219 }
220 }
221
228 Point rotated(int angle) const
229 {
230 double const theta = static_cast<double>(angle) * Mathd::pi() / 180.0;
231
232 double const c = Mathd::Cos(theta);
233 double const s = Mathd::Sin(theta);
234
235 return Point(
236 static_cast<int>(std::round((c * x) - (s * y))), static_cast<int>(std::round((s * x) + (c * y))));
237 }
238
244 void rotate(double angle)
245 {
246 double const theta = angle * Mathd::pi() / 180.0;
247
248 double const costheta = Mathd::Cos(theta);
249 double const sintheta = Mathd::Sin(theta);
250
251 double const nx = static_cast<double>(x);
252 double const ny = static_cast<double>(y);
253
254 x = static_cast<int>((costheta * nx) - (sintheta * ny));
255 y = static_cast<int>((sintheta * nx) + (costheta * ny));
256 }
257
267 void rotate(Point const & origin, int angle)
268 {
269 // 1. Translate point to origin-relative coordinates (promote to double)
270 double const nx = static_cast<double>(x - origin.x);
271 double const ny = static_cast<double>(y - origin.y);
272
273 // 2. Rotate point by angle (in radians)
274 double const theta = static_cast<double>(angle) * Mathd::pi() / 180.0;
275
276 // 3. Calculate rotation using standard 2D rotation matrix
277 double const costheta = Mathd::Cos(theta);
278 double const sintheta = Mathd::Sin(theta);
279
280 // 4. Apply rotation to point
281 double const rx = (costheta * nx) - (sintheta * ny);
282 double const ry = (sintheta * nx) + (costheta * ny);
283
284 // 5. Translate back to world coordinates AND cast to int (with rounding)
285 x = static_cast<int>(std::round(origin.x + rx));
286 y = static_cast<int>(std::round(origin.y + ry));
287 }
288
295 void set(int _x, int _y)
296 {
297 x = _x;
298 y = _y;
299 }
300
307 int& operator[](int ind)
308 {
309 assert(ind > -1 && ind < 2);
310 return val.at(static_cast<size_t>(ind));
311 }
312
322 friend std::ostream& operator<<(std::ostream& os, Point const & p)
323 {
324 return os << "(" << p.x << ":" << p.y << ")";
325 }
326 };
327
331 using PointVector = std::vector<Point>;
332} // namespace fcn
333
334#endif // INCLUDE_FIFECHAN_POINT_HPP_
static num_type zeroTolerance()
Definition math.hpp:255
static float Sqrt(float _val)
static double Cos(double _val)
static num_type pi()
Definition math.hpp:267
static double Sin(double _val)
Represents a 2D coordinate (X, Y).
Definition point.hpp:34
Point & operator+=(Point const &p)
Vector inplace addition.
Definition point.hpp:131
void rotate(Point const &origin, int angle)
Rotates the point around a given origin by angle degrees.
Definition point.hpp:267
bool operator!=(Point const &p) const
Equality comparision.
Definition point.hpp:190
int & operator[](int ind)
Index accessor for the point components.
Definition point.hpp:307
int length() const
Return length.
Definition point.hpp:198
Point(Point const &rhs)
Copy Constructor.
Definition point.hpp:64
Point operator-(Point const &p) const
Vector subtraction.
Definition point.hpp:120
void normalize()
Normalizes the point.
Definition point.hpp:207
Point & operator=(Point const &rhs)
Copy assignment.
Definition point.hpp:82
Point & operator=(Point &&rhs) noexcept
Move assignment.
Definition point.hpp:95
Point(Point &&rhs) noexcept
Move Constructor.
Definition point.hpp:73
void rotate(double angle)
Rotates the point around the origin by angle degrees.
Definition point.hpp:244
friend std::ostream & operator<<(std::ostream &os, Point const &p)
Stream output operator for debug/logging.
Definition point.hpp:322
Point & operator-=(Point const &p)
Vector inplace subtraction.
Definition point.hpp:144
Point operator*(int const &i) const
Scalar multiplication with an integer value.
Definition point.hpp:157
Point(int _x=0, int _y=0)
Constructor.
Definition point.hpp:53
bool operator==(Point const &p) const
Equality comparision.
Definition point.hpp:179
Point rotated(int angle) const
Rotates the point around the origin by angle degrees.
Definition point.hpp:228
Point operator+(Point const &p) const
Vector addition.
Definition point.hpp:109
Point operator/(int const &i) const
Scalar division with an integer value.
Definition point.hpp:168
void set(int _x, int _y)
Sets the x and y coordinate of the 2D point.
Definition point.hpp:295
Used replacement tokens by configure_file():
std::vector< Point > PointVector
A list of points.
Definition point.hpp:331