Vector.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef VECTOR_H
  2. #define VECTOR_H
  3. #include "utils/StringBuffer.h"
  4. template<int N>
  5. class Vector final {
  6. float values[N];
  7. public:
  8. Vector() {
  9. for(int i = 0; i < N; i++) {
  10. values[i] = 0.0f;
  11. }
  12. }
  13. Vector(float, float) = delete;
  14. Vector(float, float, float) = delete;
  15. Vector& set(float, float) = delete;
  16. Vector& set(float, float, float) = delete;
  17. Vector& setAngles(float, float) = delete;
  18. Vector cross(const Vector&) const = delete;
  19. Vector& operator+=(const Vector& other) {
  20. for(int i = 0; i < N; i++) {
  21. values[i] += other.values[i];
  22. }
  23. return *this;
  24. }
  25. Vector operator+(const Vector& other) const {
  26. Vector v = *this;
  27. v += other;
  28. return v;
  29. }
  30. Vector& operator-=(const Vector& other) {
  31. for(int i = 0; i < N; i++) {
  32. values[i] -= other.values[i];
  33. }
  34. return *this;
  35. }
  36. Vector operator-() const {
  37. Vector v = *this;
  38. for(int i = 0; i < N; i++) {
  39. v.values[i] = -v.values[i];
  40. }
  41. return v;
  42. }
  43. Vector operator-(const Vector& other) const {
  44. Vector v = *this;
  45. v -= other;
  46. return v;
  47. }
  48. Vector& operator*=(float factor) {
  49. for(int i = 0; i < N; i++) {
  50. values[i] *= factor;
  51. }
  52. return *this;
  53. }
  54. Vector operator*(float factor) const {
  55. Vector v = *this;
  56. v *= factor;
  57. return v;
  58. }
  59. float dot(const Vector& v) const {
  60. float length = 0.0f;
  61. for(int i = 0; i < N; i++) {
  62. length += values[i] * v.values[i];
  63. }
  64. return length;
  65. }
  66. float squareLength() const {
  67. return dot(*this);
  68. }
  69. float length() const {
  70. return sqrtf(squareLength());
  71. }
  72. Vector& normalize() {
  73. *this *= 1.0f / length();
  74. return *this;
  75. }
  76. const float& operator[](int index) const {
  77. return values[index];
  78. }
  79. template<int L>
  80. void toString(StringBuffer<L>& s) const {
  81. s.append("[");
  82. for(int i = 0; i < N - 1; i++) {
  83. s.append(values[i]);
  84. s.append(", ");
  85. }
  86. if(N > 0) {
  87. s.append(values[N - 1]);
  88. }
  89. s.append("]");
  90. }
  91. };
  92. template<int N>
  93. Vector<N> operator*(float factor, const Vector<N>& v) {
  94. return v * factor;
  95. }
  96. template<> Vector<3>::Vector(float x, float y, float z);
  97. template<> Vector<2>::Vector(float x, float y);
  98. template<> Vector<3>& Vector<3>::set(float x, float y, float z);
  99. template<> Vector<2>& Vector<2>::set(float x, float y);
  100. template<> Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle);
  101. template<> Vector<3> Vector<3>::cross(const Vector<3>& other) const;
  102. typedef Vector<3> Vector3;
  103. typedef Vector<2> Vector2;
  104. #endif