FifeGUI 0.2.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#include <array>
9#include <cassert>
10#include <iostream>
11#include <utility>
12#include <vector>
13
14#include "fifechan/math.hpp"
15
16namespace fcn
17{
18
28 class Point
29 {
30 public:
31 union
32 {
33 std::array<int, 2> val;
34 struct
35 {
36 int x, y;
37 };
38 };
39
45 explicit Point(int _x = 0, int _y = 0) : val{_x, _y} { }
46
50 Point(Point const & rhs) : val{rhs.val[0], rhs.val[1]} { }
51
55 Point(Point&& rhs) noexcept : val{rhs.val} { }
56
60 Point& operator=(Point const & rhs)
61 {
62 if (this != &rhs) {
63 val = rhs.val;
64 }
65 return *this;
66 }
67
71 Point& operator=(Point&& rhs) noexcept
72 {
73 if (this != &rhs) {
74 val = rhs.val;
75 }
76 return *this;
77 }
78
82 Point operator+(Point const & p) const
83 {
84 return Point(x + p.x, y + p.y);
85 }
86
90 Point operator-(Point const & p) const
91 {
92 return Point(x - p.x, y - p.y);
93 }
94
99 {
100 x += p.x;
101 y += p.y;
102 return *this;
103 }
104
109 {
110 x -= p.x;
111 y -= p.y;
112 return *this;
113 }
114
118 Point operator*(int const & i) const
119 {
120 return Point(x * i, y * i);
121 }
122
126 Point operator/(int const & i) const
127 {
128 return Point(x / i, y / i);
129 }
130
134 bool operator==(Point const & p) const
135 {
136 return x == p.x && y == p.y;
137 }
138
142 bool operator!=(Point const & p) const
143 {
144 return x != p.x || y != p.y;
145 }
146
150 int length() const
151 {
152 double const sq = (static_cast<double>(x) * x) + (static_cast<double>(y) * y);
153 return static_cast<int>(Mathf::Sqrt(sq));
154 }
155
160 {
161 float const len = length();
162
163 if (len > Mathf::zeroTolerance()) {
164 float const invLength = 1.0F / len;
165 x = static_cast<int>(x * invLength);
166 y = static_cast<int>(y * invLength);
167 } else {
168 x = 0;
169 y = 0;
170 }
171 }
172
176 Point rotated(int angle) const
177 {
178 double const theta = static_cast<double>(angle) * Mathd::pi() / 180.0;
179 double const c = Mathd::Cos(theta);
180 double const s = Mathd::Sin(theta);
181
182 return Point(
183 static_cast<int>(std::round((c * x) - (s * y))), static_cast<int>(std::round((s * x) + (c * y))));
184 }
185
189 void rotate(double angle)
190 {
191 double const theta = angle * Mathd::pi() / 180.0;
192
193 double const costheta = Mathd::Cos(theta);
194 double const sintheta = Mathd::Sin(theta);
195
196 double const nx = static_cast<double>(x);
197 double const ny = static_cast<double>(y);
198
199 x = static_cast<int>((costheta * nx) - (sintheta * ny));
200 y = static_cast<int>((sintheta * nx) + (costheta * ny));
201 }
202
209 void rotate(Point const & origin, int angle)
210 {
211 // 1. Translate point to origin-relative coordinates (promote to double)
212 double const nx = static_cast<double>(x - origin.x);
213 double const ny = static_cast<double>(y - origin.y);
214
215 // 2. Rotate point by angle (in radians)
216 double const theta = static_cast<double>(angle) * Mathd::pi() / 180.0;
217
218 // 3. Calculate rotation using standard 2D rotation matrix
219 double const costheta = Mathd::Cos(theta);
220 double const sintheta = Mathd::Sin(theta);
221
222 // 4. Apply rotation to point
223 double const rx = (costheta * nx) - (sintheta * ny);
224 double const ry = (sintheta * nx) + (costheta * ny);
225
226 // 5. Translate back to world coordinates AND cast to int (with rounding)
227 x = static_cast<int>(std::round(origin.x + rx));
228 y = static_cast<int>(std::round(origin.y + ry));
229 }
230
234 void set(int _x, int _y)
235 {
236 x = _x;
237 y = _y;
238 }
239
246 int& operator[](int ind)
247 {
248 assert(ind > -1 && ind < 2);
249 return val[ind];
250 }
251
257 friend std::ostream& operator<<(std::ostream& os, Point const & p)
258 {
259 return os << "(" << p.x << ":" << p.y << ")";
260 }
261 };
262
263 using PointVector = std::vector<Point>;
264} // namespace fcn
265
266#endif // INCLUDE_FIFECHAN_POINT_HPP_
static num_type zeroTolerance()
Definition math.hpp:251
static float Sqrt(float _val)
static double Cos(double _val)
static num_type pi()
Definition math.hpp:263
static double Sin(double _val)
Point & operator+=(Point const &p)
Vector inplace addition.
Definition point.hpp:98
void rotate(Point const &origin, int angle)
Rotates the point around a given origin by angle degrees.
Definition point.hpp:209
bool operator!=(Point const &p) const
Equality comparision.
Definition point.hpp:142
int & operator[](int ind)
Index accessor for the point components.
Definition point.hpp:246
int length() const
Return length.
Definition point.hpp:150
Point(Point const &rhs)
Copy Constructor.
Definition point.hpp:50
Point operator-(Point const &p) const
Vector subtraction.
Definition point.hpp:90
void normalize()
Normalizes the point.
Definition point.hpp:159
Point & operator=(Point const &rhs)
Copy assignment.
Definition point.hpp:60
Point & operator=(Point &&rhs) noexcept
Move assignment.
Definition point.hpp:71
Point(Point &&rhs) noexcept
Move Constructor.
Definition point.hpp:55
void rotate(double angle)
Rotates the point around the origin by angle degrees.
Definition point.hpp:189
friend std::ostream & operator<<(std::ostream &os, Point const &p)
Stream output operator for debug/logging.
Definition point.hpp:257
Point & operator-=(Point const &p)
Vector inplace subtraction.
Definition point.hpp:108
Point operator*(int const &i) const
Scalar multiplication with an integer value.
Definition point.hpp:118
Point(int _x=0, int _y=0)
Constructor.
Definition point.hpp:45
bool operator==(Point const &p) const
Equality comparision.
Definition point.hpp:134
Point rotated(int angle) const
Rotates the point around the origin by angle degrees.
Definition point.hpp:176
Point operator+(Point const &p) const
Vector addition.
Definition point.hpp:82
Point operator/(int const &i) const
Scalar division with an integer value.
Definition point.hpp:126
void set(int _x, int _y)
Sets the x and y coordinate of the 2D point.
Definition point.hpp:234