Vector.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /**
  2. * Copyright (c) 2006-2023 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_VECTOR_H
  21. #define LOVE_VECTOR_H
  22. // STD
  23. #include <cmath>
  24. namespace love
  25. {
  26. // All math operators are component-wise.
  27. struct Vector2
  28. {
  29. float x, y;
  30. Vector2()
  31. : x(0.0f), y(0.0f)
  32. {}
  33. Vector2(float x, float y)
  34. : x(x), y(y)
  35. {}
  36. Vector2(const Vector2 &v)
  37. : x(v.x), y(v.y)
  38. {}
  39. float getLength() const;
  40. float getLengthSquare() const;
  41. /**
  42. * Normalizes the Vector.
  43. * @param length Desired length of the vector.
  44. **/
  45. void normalize(float length = 1.0f);
  46. /**
  47. * Gets a vector perpendicular to the Vector.
  48. * To get the true (normalized) normal, use v.getNormal(1.0f / v.getLength())
  49. **/
  50. Vector2 getNormal() const;
  51. /**
  52. * Gets a vector perpendicular to the Vector.
  53. * To get the true (normalized) normal, use v.getNormal(1.0f / v.getLength())
  54. **/
  55. Vector2 getNormal(float scale) const;
  56. static inline float dot(const Vector2 &a, const Vector2 &b);
  57. static inline float cross(const Vector2 &a, const Vector2 &b);
  58. Vector2 operator + (const Vector2 &v) const;
  59. Vector2 operator - (const Vector2 &v) const;
  60. Vector2 operator * (float s) const;
  61. Vector2 operator / (float s) const;
  62. Vector2 operator - () const;
  63. void operator += (const Vector2 &v);
  64. void operator -= (const Vector2 &v);
  65. void operator *= (float s);
  66. void operator /= (float s);
  67. bool operator == (const Vector2 &v) const;
  68. bool operator != (const Vector2 &v) const;
  69. }; // Vector2
  70. // All math operators are component-wise.
  71. struct Vector3
  72. {
  73. float x, y, z;
  74. Vector3()
  75. : x(0.0f), y(0.0f), z(0.0f)
  76. {}
  77. Vector3(float x, float y, float z)
  78. : x(x), y(y), z(z)
  79. {}
  80. Vector3(const Vector2 &v, float z = 0.0f)
  81. : x(v.x), y(v.y), z(z)
  82. {}
  83. float getLength() const;
  84. float getLengthSquare() const;
  85. /**
  86. * Normalizes the Vector.
  87. * @param length Desired length of the vector.
  88. **/
  89. void normalize(float length = 1.0);
  90. static inline float dot(const Vector3 &a, const Vector3 &b);
  91. static inline Vector3 cross(const Vector3 &a, const Vector3 &b);
  92. Vector3 operator + (const Vector3 &v) const;
  93. Vector3 operator - (const Vector3 &v) const;
  94. Vector3 operator * (float s) const;
  95. Vector3 operator / (float s) const;
  96. Vector3 operator - () const;
  97. void operator += (const Vector3 &v);
  98. void operator -= (const Vector3 &v);
  99. void operator *= (float s);
  100. void operator /= (float s);
  101. bool operator == (const Vector3 &v) const;
  102. bool operator != (const Vector3 &v) const;
  103. }; // Vector3
  104. // All math operators are component-wise.
  105. struct Vector4
  106. {
  107. float x, y, z, w;
  108. Vector4()
  109. : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
  110. {}
  111. Vector4(float x, float y, float z, float w)
  112. : x(x), y(y), z(z), w(w)
  113. {}
  114. Vector4(const Vector2 &v, float z = 0.0f, float w = 0.0f)
  115. : x(v.x), y(v.y), z(z), w(w)
  116. {}
  117. Vector4(const Vector3 &v, float w = 0.0f)
  118. : x(v.x), y(v.y), z(v.z), w(w)
  119. {}
  120. float getLength() const;
  121. float getLengthSquare() const;
  122. /**
  123. * Normalizes the Vector.
  124. * @param length Desired length of the vector.
  125. **/
  126. void normalize(float length = 1.0);
  127. static inline float dot(const Vector4 &a, const Vector4 &b);
  128. Vector4 operator + (const Vector4 &v) const;
  129. Vector4 operator - (const Vector4 &v) const;
  130. Vector4 operator * (float s) const;
  131. Vector4 operator / (float s) const;
  132. Vector4 operator - () const;
  133. void operator += (const Vector4 &v);
  134. void operator -= (const Vector4 &v);
  135. void operator *= (float s);
  136. void operator /= (float s);
  137. bool operator == (const Vector4 &v) const;
  138. bool operator != (const Vector4 &v) const;
  139. }; // Vector4
  140. inline float Vector2::getLength() const
  141. {
  142. return sqrtf(x*x + y*y);
  143. }
  144. inline float Vector2::getLengthSquare() const
  145. {
  146. return x*x + y*y;
  147. }
  148. inline Vector2 Vector2::getNormal() const
  149. {
  150. return Vector2(-y, x);
  151. }
  152. inline Vector2 Vector2::getNormal(float scale) const
  153. {
  154. return Vector2(-y * scale, x * scale);
  155. }
  156. inline float Vector2::dot(const Vector2 &a, const Vector2 &b)
  157. {
  158. return a.x * b.x + a.y * b.y;
  159. }
  160. inline float Vector2::cross(const Vector2 &a, const Vector2 &b)
  161. {
  162. return a.x * b.y - a.y * b.x;
  163. }
  164. inline void Vector2::normalize(float length)
  165. {
  166. float length_current = getLength();
  167. if (length_current > 0)
  168. {
  169. float m = length / length_current;
  170. x *= m;
  171. y *= m;
  172. }
  173. }
  174. inline Vector2 Vector2::operator + (const Vector2 &v) const
  175. {
  176. return Vector2(x + v.x, y + v.y);
  177. }
  178. inline Vector2 Vector2::operator - (const Vector2 &v) const
  179. {
  180. return Vector2(x - v.x, y - v.y);
  181. }
  182. inline Vector2 Vector2::operator * (float s) const
  183. {
  184. return Vector2(x*s, y*s);
  185. }
  186. inline Vector2 Vector2::operator / (float s) const
  187. {
  188. float invs = 1.0f / s;
  189. return Vector2(x*invs, y*invs);
  190. }
  191. inline Vector2 Vector2::operator - () const
  192. {
  193. return Vector2(-x, -y);
  194. }
  195. inline void Vector2::operator += (const Vector2 &v)
  196. {
  197. x += v.x;
  198. y += v.y;
  199. }
  200. inline void Vector2::operator -= (const Vector2 &v)
  201. {
  202. x -= v.x;
  203. y -= v.y;
  204. }
  205. inline void Vector2::operator *= (float s)
  206. {
  207. x *= s;
  208. y *= s;
  209. }
  210. inline void Vector2::operator /= (float s)
  211. {
  212. float invs = 1.0f / s;
  213. x *= invs;
  214. y *= invs;
  215. }
  216. inline bool Vector2::operator == (const Vector2 &v) const
  217. {
  218. return x == v.x && y == v.y;
  219. }
  220. inline bool Vector2::operator != (const Vector2 &v) const
  221. {
  222. return x != v.x || y != v.y;
  223. }
  224. inline float Vector3::getLength() const
  225. {
  226. return sqrtf(x*x + y*y + z*z);
  227. }
  228. inline float Vector3::getLengthSquare() const
  229. {
  230. return x*x + y*y + z*z;
  231. }
  232. inline float Vector3::dot(const Vector3 &a, const Vector3 &b)
  233. {
  234. return a.x * b.x + a.y * b.y + a.z * b.z;
  235. }
  236. inline Vector3 Vector3::cross(const Vector3 &a, const Vector3 &b)
  237. {
  238. return Vector3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
  239. }
  240. inline void Vector3::normalize(float length)
  241. {
  242. float length_current = getLength();
  243. if (length_current > 0)
  244. {
  245. float m = length / length_current;
  246. x *= m;
  247. y *= m;
  248. z *= m;
  249. }
  250. }
  251. inline Vector3 Vector3::operator + (const Vector3 &v) const
  252. {
  253. return Vector3(x + v.x, y + v.y, z + v.z);
  254. }
  255. inline Vector3 Vector3::operator - (const Vector3 &v) const
  256. {
  257. return Vector3(x - v.x, y - v.y, z - v.z);
  258. }
  259. inline Vector3 Vector3::operator * (float s) const
  260. {
  261. return Vector3(x*s, y*s, z*s);
  262. }
  263. inline Vector3 Vector3::operator / (float s) const
  264. {
  265. float invs = 1.0f / s;
  266. return Vector3(x*invs, y*invs, z*invs);
  267. }
  268. inline Vector3 Vector3::operator - () const
  269. {
  270. return Vector3(-x, -y, -z);
  271. }
  272. inline void Vector3::operator += (const Vector3 &v)
  273. {
  274. x += v.x;
  275. y += v.y;
  276. z += v.z;
  277. }
  278. inline void Vector3::operator -= (const Vector3 &v)
  279. {
  280. x -= v.x;
  281. y -= v.y;
  282. z -= v.z;
  283. }
  284. inline void Vector3::operator *= (float s)
  285. {
  286. x *= s;
  287. y *= s;
  288. z *= s;
  289. }
  290. inline void Vector3::operator /= (float s)
  291. {
  292. float invs = 1.0f / s;
  293. x *= invs;
  294. y *= invs;
  295. z *= invs;
  296. }
  297. inline bool Vector3::operator == (const Vector3 &v) const
  298. {
  299. return x == v.x && y == v.y && z == v.z;
  300. }
  301. inline bool Vector3::operator != (const Vector3 &v) const
  302. {
  303. return x != v.x || y != v.y || z != v.z;
  304. }
  305. inline float Vector4::getLength() const
  306. {
  307. return sqrtf(x*x + y*y + z*z + w*w);
  308. }
  309. inline float Vector4::getLengthSquare() const
  310. {
  311. return x*x + y*y + z*z + w*w;
  312. }
  313. inline float Vector4::dot(const Vector4 &a, const Vector4 &b)
  314. {
  315. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  316. }
  317. inline void Vector4::normalize(float length)
  318. {
  319. float length_current = getLength();
  320. if (length_current > 0)
  321. {
  322. float m = length / length_current;
  323. x *= m;
  324. y *= m;
  325. z *= m;
  326. w *= m;
  327. }
  328. }
  329. inline Vector4 Vector4::operator + (const Vector4 &v) const
  330. {
  331. return Vector4(x + v.x, y + v.y, z + v.z, w + v.w);
  332. }
  333. inline Vector4 Vector4::operator - (const Vector4 &v) const
  334. {
  335. return Vector4(x - v.x, y - v.y, z - v.z, w - v.w);
  336. }
  337. inline Vector4 Vector4::operator * (float s) const
  338. {
  339. return Vector4(x*s, y*s, z*s, w*s);
  340. }
  341. inline Vector4 Vector4::operator / (float s) const
  342. {
  343. float invs = 1.0f / s;
  344. return Vector4(x*invs, y*invs, z*invs, w*invs);
  345. }
  346. inline Vector4 Vector4::operator - () const
  347. {
  348. return Vector4(-x, -y, -z, -w);
  349. }
  350. inline void Vector4::operator += (const Vector4 &v)
  351. {
  352. x += v.x;
  353. y += v.y;
  354. z += v.z;
  355. w += v.w;
  356. }
  357. inline void Vector4::operator -= (const Vector4 &v)
  358. {
  359. x -= v.x;
  360. y -= v.y;
  361. z -= v.z;
  362. w -= v.w;
  363. }
  364. inline void Vector4::operator *= (float s)
  365. {
  366. x *= s;
  367. y *= s;
  368. z *= s;
  369. w *= s;
  370. }
  371. inline void Vector4::operator /= (float s)
  372. {
  373. float invs = 1.0f / s;
  374. x *= invs;
  375. y *= invs;
  376. z *= invs;
  377. w *= invs;
  378. }
  379. inline bool Vector4::operator == (const Vector4 &v) const
  380. {
  381. return x == v.x && y == v.y && z == v.z && w == v.w;
  382. }
  383. inline bool Vector4::operator != (const Vector4 &v) const
  384. {
  385. return x != v.x || y != v.y || z != v.z || w != v.w;
  386. }
  387. } //love
  388. #endif// LOVE_VECTOR_H