stackchan-display
Loading...
Searching...
No Matches
Vector.h
1#pragma once
2#include <M5Unified.h>
3
4// NOTE: This is wheel-reinventing, but for simplicity and to avoid extra dependencies, we define our own simple Vector2i class here.
5
6namespace m5
7{
8 class Vector2i
9 {
10 public:
11 int x;
12 int y;
13
14 Vector2i() : x(0), y(0) {}
15 Vector2i(int x, int y) : x(x), y(y) {}
16
17 Vector2i operator+(const Vector2i &other) const
18 {
19 return Vector2i(x + other.x, y + other.y);
20 }
21
22 Vector2i operator-(const Vector2i &other) const
23 {
24 return Vector2i(x - other.x, y - other.y);
25 }
26
27 template <typename T>
28 Vector2i operator*(T scalar) const
29 {
30 return Vector2i(x * scalar, y * scalar);
31 }
32
33 template <typename T>
34 Vector2i operator/(T scalar) const
35 {
36 return Vector2i(x / scalar, y / scalar);
37 }
38
39 Vector2i &operator+=(const Vector2i &other)
40 {
41 x += other.x;
42 y += other.y;
43 return *this;
44 }
45
46 Vector2i &operator-=(const Vector2i &other)
47 {
48 x -= other.x;
49 y -= other.y;
50 return *this;
51 }
52
53 Vector2i &operator*=(int scalar)
54 {
55 x *= scalar;
56 y *= scalar;
57 return *this;
58 }
59
60 Vector2i &operator/=(int scalar)
61 {
62 x /= scalar;
63 y /= scalar;
64 return *this;
65 }
66
67 bool operator==(const Vector2i &other) const
68 {
69 return x == other.x && y == other.y;
70 }
71
72 bool operator!=(const Vector2i &other) const
73 {
74 return !(*this == other);
75 }
76
77 int dot(const Vector2i &other) const
78 {
79 return x * other.x + y * other.y;
80 }
81
82 void clamp(const Vector2i &min, const Vector2i &max)
83 {
84 if (x < min.x)
85 {
86 x = min.x;
87 }
88 else if (x > max.x)
89 {
90 x = max.x;
91 }
92
93 if (y < min.y)
94 {
95 y = min.y;
96 }
97 else if (y > max.y)
98 {
99 y = max.y;
100 }
101 }
102 };
103
104 class Size2i
105 {
106 public:
107 int width;
108 int height;
109
110 Size2i() : width(0), height(0) {}
111 Size2i(int width, int height) : width(width), height(height) {}
112
113 void operator*=(int scalar)
114 {
115 width *= scalar;
116 height *= scalar;
117 }
118
119 template <typename T>
120 Size2i operator*(T scalar) const
121 {
122 return Size2i(width * scalar, height * scalar);
123 }
124
125 int area() const
126 {
127 return width * height;
128 }
129
130 int max() const
131 {
132 return width > height ? width : height;
133 }
134
135 int min() const
136 {
137 return width < height ? width : height;
138 }
139 };
140}
Definition Vector.h:105
Definition Vector.h:9