Logo
Vector3.h
1#pragma once
2
3#include "math/Math.h"
4#undef PI
5
6namespace SharpKmyMath{
7
8 public value struct Vector3{
9
10 public:
11 float x, y, z;
12
13 Vector3(float v)
14 {
15 x = y = z = v;
16 }
17
18 Vector3(float _x, float _y, float _z)
19 {
20 x = _x;
21 y = _y;
22 z = _z;
23 }
24
25 float length()
26 {
27 return sqrt(x*x + y*y + z*z);
28 }
29
31 {
32 float l = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
33 Vector3 ret;
34 ret.x = v.x / l;
35 ret.y = v.y / l;
36 ret.z = v.z / l;
37 return ret;
38 }
39
41 {
42 Vector3 ret;
43 ret.x = v1.x - v2.x;
44 ret.y = v1.y - v2.y;
45 ret.z = v1.z - v2.z;
46 return ret;
47 }
48
50 {
51 Vector3 ret;
52 ret.x = v1.x + v2.x;
53 ret.y = v1.y + v2.y;
54 ret.z = v1.z + v2.z;
55 return ret;
56 }
57
58 static Vector3 operator*(Vector3 v, float f)
59 {
60 Vector3 ret;
61 ret.x = v.x * f;
62 ret.y = v.y * f;
63 ret.z = v.z * f;
64 return ret;
65 }
66
68 {
69 Vector3 ret;
70 ret.x = v1.x * v2.x;
71 ret.y = v1.y * v2.y;
72 ret.z = v1.z * v2.z;
73 return ret;
74 }
75
76 static Vector3 operator/(Vector3 v, float f)
77 {
78 Vector3 ret;
79 ret.x = v.x / f;
80 ret.y = v.y / f;
81 ret.z = v.z / f;
82 return ret;
83 }
84
86 {
87 Vector3 ret;
88 ret.x = -v.x;
89 ret.y = -v.y;
90 ret.z = -v.z;
91 return ret;
92 }
93
94 static float dotProduct(Vector3 v1, Vector3 v2)
95 {
96 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
97 }
98
100 {
101 Vector3 tmp;
102 tmp.x = v1.y * v2.z - v1.z * v2.y;
103 tmp.y = v1.z * v2.x - v1.x * v2.z;
104 tmp.z = v1.x * v2.y - v1.y * v2.x;
105 return tmp;
106 }
107
108 static Vector3 one = Vector3(1, 1, 1);
109 static Vector3 zero = Vector3(0, 0, 0);
110 };
111
112 public value struct Vector4{
113
114 public:
115 float x, y, z, w;
116
117 Vector4(float v)
118 {
119 x = y = z = w = v;
120 }
121
122 Vector4(float _x, float _y, float _z, float _w)
123 {
124 x = _x;
125 y = _y;
126 z = _z;
127 w = _w;
128 }
129
130 static Vector4 operator * (Vector4 v, float f)
131 {
132 Vector4 ret;
133 ret.x = v.x * f;
134 ret.y = v.y * f;
135 ret.z = v.z * f;
136 ret.w = v.w * f;
137 return ret;
138 }
139
140 static Vector4 operator / (Vector4 v, float f)
141 {
142 Vector4 ret;
143 ret.x = v.x / f;
144 ret.y = v.y / f;
145 ret.z = v.z / f;
146 ret.w = v.w / f;
147 return ret;
148 }
149
151 {
152 Vector4 ret;
153 ret.x = v.x + v2.x;
154 ret.y = v.y + v2.y;
155 ret.z = v.z + v2.z;
156 ret.w = v.w + v2.w;
157 return ret;
158 }
159
161 {
162 Vector4 ret;
163 ret.x = v.x - v2.x;
164 ret.y = v.y - v2.y;
165 ret.z = v.z - v2.z;
166 ret.w = v.w - v2.w;
167 return ret;
168 }
169
171 {
172 Vector4 ret;
173 ret.x = -v.x;
174 ret.y = -v.y;
175 ret.z = -v.z;
176 ret.w = -v.w;
177 return ret;
178 }
179
181 {
182 return Vector3(x, y, z);
183 }
184 };
185
186 public value struct Vector2{
187
188 public:
189 float x, y;
190
191 Vector2(float v)
192 {
193 x = y = v;
194 }
195
196 Vector2(float _x, float _y)
197 {
198 x = _x;
199 y = _y;
200 }
201
203 {
204 Vector2 ret;
205 ret.x = v.x + v2.x;
206 ret.y = v.y + v2.y;
207 return ret;
208 }
209
211 {
212 Vector2 ret;
213 ret.x = v.x - v2.x;
214 ret.y = v.y - v2.y;
215 return ret;
216 }
217
218 static Vector2 operator / (Vector2 v, float f)
219 {
220 Vector2 ret;
221 ret.x = v.x/f;
222 ret.y = v.y/f;
223 return ret;
224 }
225
226 static Vector2 operator * (Vector2 v, float f)
227 {
228 Vector2 ret;
229 ret.x = v.x * f;
230 ret.y = v.y * f;
231 return ret;
232 }
233
235 {
236 Vector2 ret;
237 ret.x = -v.x;
238 ret.y = -v.y;
239 return ret;
240 }
241 };
242
243 public ref class Helper{
244
245 public:
246 static float ToDeg(float d){
247 return RAD_TO_DEG(d);
248 }
249
250 static float ToRad(float d){
251 return DEG_TO_RAD(d);
252 }
253
254 static float lerp(float start, float end, float rate)
255 {
256 return start * (1 - rate) + end * rate;
257 }
258
259 static const float PI=3.1415f;
260
261 };
262
263 public value class Rectangle
264 {
265 public:
266 float x, y, height, width;
267 Rectangle(float _x, float _y, float _w, float _h)
268 {
269 x = _x;
270 y = _y;
271 height = _h;
272 width = _w;
273 }
274 };
275
276
277}
Definition: Vector3.h:243
static const float PI
Definition: Vector3.h:259
static float lerp(float start, float end, float rate)
Definition: Vector3.h:254
static float ToDeg(float d)
Definition: Vector3.h:246
static float ToRad(float d)
Definition: Vector3.h:250
Definition: Vector3.h:264
float height
Definition: Vector3.h:266
Rectangle(float _x, float _y, float _w, float _h)
Definition: Vector3.h:267
float width
Definition: Vector3.h:266
float x
Definition: Vector3.h:266
float y
Definition: Vector3.h:266
Definition: RefCapture.h:3
Definition: Vector3.h:186
float y
Definition: Vector3.h:189
static Vector2 operator-(Vector2 v, Vector2 v2)
Definition: Vector3.h:210
Vector2(float v)
Definition: Vector3.h:191
Vector2(float _x, float _y)
Definition: Vector3.h:196
float x
Definition: Vector3.h:189
static Vector2 operator+(Vector2 v, Vector2 v2)
Definition: Vector3.h:202
static Vector2 operator/(Vector2 v, float f)
Definition: Vector3.h:218
static Vector2 operator*(Vector2 v, float f)
Definition: Vector3.h:226
Definition: Vector3.h:8
float y
Definition: Vector3.h:11
float z
Definition: Vector3.h:11
static Vector3 operator*(Vector3 v, float f)
Definition: Vector3.h:58
static Vector3 crossProduct(Vector3 v1, Vector3 v2)
Definition: Vector3.h:99
static Vector3 one
Definition: Vector3.h:108
float x
Definition: Vector3.h:11
static Vector3 zero
Definition: Vector3.h:109
Vector3(float v)
Definition: Vector3.h:13
static Vector3 operator/(Vector3 v, float f)
Definition: Vector3.h:76
static float dotProduct(Vector3 v1, Vector3 v2)
Definition: Vector3.h:94
static Vector3 normalize(Vector3 v)
Definition: Vector3.h:30
static Vector3 operator*(Vector3 v1, Vector3 v2)
Definition: Vector3.h:67
Vector3(float _x, float _y, float _z)
Definition: Vector3.h:18
static Vector3 operator+(Vector3 v1, Vector3 v2)
Definition: Vector3.h:49
static Vector3 operator-(Vector3 v1, Vector3 v2)
Definition: Vector3.h:40
float length()
Definition: Vector3.h:25
Definition: Vector3.h:112
float x
Definition: Vector3.h:115
float w
Definition: Vector3.h:115
float y
Definition: Vector3.h:115
static Vector4 operator/(Vector4 v, float f)
Definition: Vector3.h:140
float z
Definition: Vector3.h:115
static Vector4 operator+(Vector4 v, Vector4 v2)
Definition: Vector3.h:150
Vector4(float _x, float _y, float _z, float _w)
Definition: Vector3.h:122
static Vector4 operator*(Vector4 v, float f)
Definition: Vector3.h:130
Vector3 getXYZ()
Definition: Vector3.h:180
Vector4(float v)
Definition: Vector3.h:117
static Vector4 operator-(Vector4 v, Vector4 v2)
Definition: Vector3.h:160