FastLED 3.9.15
Loading...
Searching...
No Matches
geometry.h
Go to the documentation of this file.
1
2#pragma once
3
4#include "fl/math.h"
5
6namespace fl {
7
8template <typename T> struct vec2 {
9 // value_type
10 using value_type = T;
11 T x = 0;
12 T y = 0;
13 constexpr vec2() = default;
14 constexpr vec2(T x, T y) : x(x), y(y) {}
15
16 template <typename U> explicit constexpr vec2(U xy) : x(xy), y(xy) {}
17
18 constexpr vec2(const vec2 &p) : x(p.x), y(p.y) {}
19 vec2 &operator*=(const float &f) {
20 x *= f;
21 y *= f;
22 return *this;
23 }
24 vec2 &operator/=(const float &f) {
25 // *this = point_xy_math::div(*this, f);
26 x /= f;
27 y /= f;
28 return *this;
29 }
30 vec2 &operator*=(const double &f) {
31 // *this = point_xy_math::mul(*this, f);
32 x *= f;
33 y *= f;
34 return *this;
35 }
36 vec2 &operator/=(const double &f) {
37 // *this = point_xy_math::div(*this, f);
38 x /= f;
39 y /= f;
40 return *this;
41 }
42
43 vec2 &operator/=(const uint16_t &d) {
44 // *this = point_xy_math::div(*this, d);
45 x /= d;
46 y /= d;
47 return *this;
48 }
49
50 vec2 &operator/=(const int &d) {
51 // *this = point_xy_math::div(*this, d);
52 x /= d;
53 y /= d;
54 return *this;
55 }
56
57 vec2 &operator/=(const vec2 &p) {
58 // *this = point_xy_math::div(*this, p);
59 x /= p.x;
60 y /= p.y;
61 return *this;
62 }
63
64 vec2 &operator+=(const vec2 &p) {
65 //*this = point_xy_math::add(*this, p);
66 x += p.x;
67 y += p.y;
68 return *this;
69 }
70
71 vec2 &operator-=(const vec2 &p) {
72 // *this = point_xy_math::sub(*this, p);
73 x -= p.x;
74 y -= p.y;
75 return *this;
76 }
77
78 vec2 &operator=(const vec2 &p) {
79 x = p.x;
80 y = p.y;
81 return *this;
82 }
83
84 vec2 operator-(const vec2 &p) const { return vec2(x - p.x, y - p.y); }
85
86 vec2 operator+(const vec2 &p) const { return vec2(x + p.x, y + p.y); }
87
88 vec2 operator*(const vec2 &p) const { return vec2(x * p.x, y * p.y); }
89
90 vec2 operator/(const vec2 &p) const { return vec2(x / p.x, y / p.y); }
91
92 template <typename NumberT> vec2 operator+(const NumberT &p) const {
93 return vec2(x + p, y + p);
94 }
95
96 template <typename U> vec2 operator+(const vec2<U> &p) const {
97 return vec2(x + p.x, y + p.x);
98 }
99
100 template <typename NumberT> vec2 operator-(const NumberT &p) const {
101 return vec2(x - p, y - p);
102 }
103
104 template <typename NumberT> vec2 operator*(const NumberT &p) const {
105 return vec2(x * p, y * p);
106 }
107
108 template <typename NumberT> vec2 operator/(const NumberT &p) const {
109 T a = x / p;
110 T b = y / p;
111 return vec2<T>(a, b);
112 }
113
114 bool operator==(const vec2 &p) const { return (x == p.x && y == p.y); }
115
116 bool operator!=(const vec2 &p) const { return (x != p.x || y != p.y); }
117
118 template <typename U> bool operator==(const vec2<U> &p) const {
119 return (x == p.x && y == p.y);
120 }
121
122 template <typename U> bool operator!=(const vec2<U> &p) const {
123 return (x != p.x || y != p.y);
124 }
125
126 vec2 getMax(const vec2 &p) const { return vec2(MAX(x, p.x), MAX(y, p.y)); }
127
128 vec2 getMin(const vec2 &p) const { return vec2(MIN(x, p.x), MIN(y, p.y)); }
129
130 template <typename U> vec2<U> cast() const {
131 return vec2<U>(static_cast<U>(x), static_cast<U>(y));
132 }
133
134 template <typename U> vec2 getMax(const vec2<U> &p) const {
135 return vec2<U>(MAX(x, p.x), MAX(y, p.y));
136 }
137
138 template <typename U> vec2 getMin(const vec2<U> &p) const {
139 return vec2<U>(MIN(x, p.x), MIN(y, p.y));
140 }
141
142 T distance(const vec2 &p) const {
143 T dx = x - p.x;
144 T dy = y - p.y;
145 return sqrt(dx * dx + dy * dy);
146 }
147
148 bool is_zero() const { return (x == 0 && y == 0); }
149};
150
151using vec2f = vec2<float>; // Full precision but slow.
152
153// Legacy support
154
155using pair_xy_float = vec2<float>; // Legacy name for vec2f
156
157// pair_xy<T> is the legacy name for vec2<T>
158template <typename T> struct pair_xy : public vec2<T> {
159 using value_type = T;
160 using vec2<T>::vec2;
161 pair_xy() = default;
162 pair_xy(const vec2<T> &p) : vec2<T>(p) {}
163};
164
165template <typename T> struct line_xy {
168
169 line_xy() = default;
171 : start(start), end(end) {}
172
173 line_xy(T start_x, T start_y, T end_x, T end_y)
174 : start(start_x, start_y), end(end_x, end_y) {}
175
176 bool empty() const { return (start == end); }
177
178 float distance_to(const vec2<T> &p,
179 vec2<T> *out_projected = nullptr) const {
180 return distance_to_line_with_point(p, start, end, out_projected);
181 }
182
183 private:
184 // Computes the closest distance from `p` to the line through `a` and `b`,
185 // and writes the projected point.
187 vec2<T> *out_projected) {
188 vec2<T> maybe;
189 vec2<T> &out_proj = out_projected ? *out_projected : maybe;
190 float dx = b.x - a.x;
191 float dy = b.y - a.y;
192 float len_sq = dx * dx + dy * dy;
193
194#pragma GCC diagnostic push
195#pragma GCC diagnostic ignored "-Wfloat-equal"
196 const bool is_zero = (len_sq == 0.0f);
197#pragma GCC diagnostic pop
198
199 if (is_zero) {
200 // a == b, the segment is a point
201 out_proj = a;
202 dx = p.x - a.x;
203 dy = p.y - a.y;
204 return sqrt(dx * dx + dy * dy);
205 }
206
207 // Project point p onto the line segment, computing parameter t
208 float t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / len_sq;
209
210 // Clamp t to [0,1] to stay within the segment
211 if (t < 0.0f)
212 t = 0.0f;
213 else if (t > 1.0f)
214 t = 1.0f;
215
216 // Find the closest point
217 out_proj.x = a.x + t * dx;
218 out_proj.y = a.y + t * dy;
219
220 dx = p.x - out_proj.x;
221 dy = p.y - out_proj.y;
222 return sqrt(dx * dx + dy * dy);
223 }
224};
225
226template <typename T> struct rect {
229
230 rect() = default;
231 rect(const vec2<T> &min, const vec2<T> &max) : mMin(min), mMax(max) {}
232
233 rect(T min_x, T min_y, T max_x, T max_y)
234 : mMin(min_x, min_y), mMax(max_x, max_y) {}
235
236 uint16_t width() const { return mMax.x - mMin.x; }
237
238 uint16_t height() const { return mMax.y - mMin.y; }
239
240 bool empty() const { return (mMin.x == mMax.x && mMin.y == mMax.y); }
241
242 void expand(const vec2<T> &p) { expand(p.x, p.y); }
243
244 void expand(const rect &r) {
245 expand(r.mMin);
246 expand(r.mMax);
247 }
248
249 void expand(T x, T y) {
250 mMin.x = MIN(mMin.x, x);
251 mMin.y = MIN(mMin.y, y);
252 mMax.x = MAX(mMax.x, x);
253 mMax.y = MAX(mMax.y, y);
254 }
255
256 bool contains(const vec2<T> &p) const {
257 return (p.x >= mMin.x && p.x < mMax.x && p.y >= mMin.y && p.y < mMax.y);
258 }
259
260 bool contains(const T &x, const T &y) const {
261 return contains(vec2<T>(x, y));
262 }
263
264 bool operator==(const rect &r) const {
265 return (mMin == r.mMin && mMax == r.mMax);
266 }
267
268 bool operator!=(const rect &r) const { return !(*this == r); }
269
270 template <typename U> bool operator==(const rect<U> &r) const {
271 return (mMin == r.mMin && mMax == r.mMax);
272 }
273
274 template <typename U> bool operator!=(const rect<U> &r) const {
275 return !(*this == r);
276 }
277};
278
279} // namespace fl
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
unsigned int xy(unsigned int x, unsigned int y)
#define MIN(a, b)
Definition math_macros.h:15
#define MAX(a, b)
Definition math_macros.h:11
vec2< float > pair_xy_float
Definition geometry.h:155
vec2< float > vec2f
Definition geometry.h:151
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
static FASTLED_NAMESPACE_BEGIN uint8_t const p[]
Definition noise.cpp:30
line_xy()=default
static float distance_to_line_with_point(vec2< T > p, vec2< T > a, vec2< T > b, vec2< T > *out_projected)
Definition geometry.h:186
vec2< T > end
Definition geometry.h:167
bool empty() const
Definition geometry.h:176
vec2< T > start
Definition geometry.h:166
float distance_to(const vec2< T > &p, vec2< T > *out_projected=nullptr) const
Definition geometry.h:178
line_xy(const vec2< T > &start, const vec2< T > &end)
Definition geometry.h:170
line_xy(T start_x, T start_y, T end_x, T end_y)
Definition geometry.h:173
pair_xy(const vec2< T > &p)
Definition geometry.h:162
pair_xy()=default
vec2< T > mMax
Definition geometry.h:228
bool operator==(const rect &r) const
Definition geometry.h:264
uint16_t width() const
Definition geometry.h:236
rect(const vec2< T > &min, const vec2< T > &max)
Definition geometry.h:231
rect()=default
uint16_t height() const
Definition geometry.h:238
bool operator!=(const rect< U > &r) const
Definition geometry.h:274
void expand(const rect &r)
Definition geometry.h:244
rect(T min_x, T min_y, T max_x, T max_y)
Definition geometry.h:233
bool contains(const vec2< T > &p) const
Definition geometry.h:256
void expand(const vec2< T > &p)
Definition geometry.h:242
bool empty() const
Definition geometry.h:240
bool operator==(const rect< U > &r) const
Definition geometry.h:270
vec2< T > mMin
Definition geometry.h:227
bool operator!=(const rect &r) const
Definition geometry.h:268
void expand(T x, T y)
Definition geometry.h:249
bool contains(const T &x, const T &y) const
Definition geometry.h:260
bool operator==(const vec2< U > &p) const
Definition geometry.h:118
constexpr vec2(U xy)
Definition geometry.h:16
vec2 operator/(const vec2 &p) const
Definition geometry.h:90
vec2 operator/(const NumberT &p) const
Definition geometry.h:108
constexpr vec2(T x, T y)
Definition geometry.h:14
vec2 & operator/=(const double &f)
Definition geometry.h:36
vec2 operator-(const NumberT &p) const
Definition geometry.h:100
vec2 & operator/=(const float &f)
Definition geometry.h:24
vec2 getMin(const vec2 &p) const
Definition geometry.h:128
T distance(const vec2 &p) const
Definition geometry.h:142
vec2 operator*(const vec2 &p) const
Definition geometry.h:88
vec2 operator+(const NumberT &p) const
Definition geometry.h:92
vec2 & operator=(const vec2 &p)
Definition geometry.h:78
vec2 getMax(const vec2< U > &p) const
Definition geometry.h:134
vec2 getMax(const vec2 &p) const
Definition geometry.h:126
vec2 & operator/=(const vec2 &p)
Definition geometry.h:57
vec2 operator-(const vec2 &p) const
Definition geometry.h:84
bool is_zero() const
Definition geometry.h:148
constexpr vec2(const vec2 &p)
Definition geometry.h:18
vec2 operator+(const vec2 &p) const
Definition geometry.h:86
bool operator==(const vec2 &p) const
Definition geometry.h:114
bool operator!=(const vec2 &p) const
Definition geometry.h:116
vec2 & operator*=(const double &f)
Definition geometry.h:30
vec2 & operator/=(const uint16_t &d)
Definition geometry.h:43
vec2 & operator+=(const vec2 &p)
Definition geometry.h:64
constexpr vec2()=default
vec2 operator*(const NumberT &p) const
Definition geometry.h:104
vec2 getMin(const vec2< U > &p) const
Definition geometry.h:138
vec2 & operator-=(const vec2 &p)
Definition geometry.h:71
vec2 & operator*=(const float &f)
Definition geometry.h:19
vec2< U > cast() const
Definition geometry.h:130
vec2 operator+(const vec2< U > &p) const
Definition geometry.h:96
vec2 & operator/=(const int &d)
Definition geometry.h:50
T value_type
Definition geometry.h:10
bool operator!=(const vec2< U > &p) const
Definition geometry.h:122