zRayzHaze

Vector.h

Dec 11th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | Source Code | 0 0
  1. // .h
  2. class Vector2
  3. {
  4. public:
  5.     float X, Y;
  6.  
  7.     Vector2();
  8.  
  9.     Vector2(float x, float y);
  10. };
  11.  
  12. class Vector3
  13. {
  14. public:
  15.     float X, Y, Z;
  16.  
  17.     Vector3();
  18.  
  19.     Vector3(float x, float y, float z);
  20.  
  21.     float *Convert();
  22.  
  23.     Vector3 Cross(const Vector3 vec);
  24.  
  25.     Vector3 Scale(float scale);
  26.  
  27.     Vector3 Lerp(Vector3 end, float fraction);
  28.  
  29.     Vector3 Negate();
  30.  
  31.     Vector3 operator()(float x, float y, float z);
  32.  
  33.     bool operator==(const Vector3 vec);
  34.  
  35.     bool operator!=(const Vector3 vec);
  36.  
  37.     Vector3 operator+(const Vector3 vec);
  38.  
  39.     Vector3 operator+(const float value);
  40.  
  41.     Vector3 operator-(const Vector3 vec);
  42.  
  43.     Vector3 operator-(const float value);
  44.  
  45.     Vector3 operator*(const Vector3 vec);
  46.  
  47.     Vector3 operator*(const float value);
  48.  
  49.     Vector3 operator/(const float value);
  50.  
  51.     Vector3 operator=(const float* value);
  52.  
  53.     Vector3 operator*=(const Vector3 vec);
  54.  
  55.     Vector3 operator*=(const float value);
  56.  
  57.     Vector3 operator+=(const Vector3 vec);
  58.  
  59.     Vector3 operator-=(const Vector3 vec);
  60.  
  61.     Vector3 operator-=(const float value);
  62.  
  63.     float GetLength() const;
  64.  
  65.     float GetMagnitude() const;
  66.  
  67.     float DistanceTo(Vector3 pos);
  68.  
  69.     float Dot(Vector3& vec);
  70.  
  71.     void Copy(Vector3 result);
  72.  
  73.     void Normalize();
  74.  
  75.     void NormalizeAngles();
  76.  
  77.     void Zero();
  78. };
  79.  
  80.  
  81.  
  82.  
  83.  
  84. // .cpp
  85. Vector2::Vector2()
  86. {
  87.     X = Y = 0;
  88. }
  89.  
  90. Vector2::Vector2(float x, float y)
  91. {
  92.     X = x;
  93.     Y = y;
  94. }
  95.  
  96. Vector3::Vector3()
  97. {
  98.     X = Y = Z = 0;
  99. }
  100.  
  101. Vector3::Vector3(float x, float y, float z)
  102. {
  103.     X = x;
  104.     Y = y;
  105.     Z = z;
  106. }
  107.  
  108. float *Vector3::Convert()
  109. {
  110.     float pos[3] = { X, Y, Z };
  111.     return pos;
  112. }
  113.  
  114. Vector3 Vector3::Cross(const Vector3 vec)
  115. {
  116.     return Vector3(Y * vec.Z - Z * vec.Y, Z * vec.X - X * vec.Z, X * vec.Y - Y * vec.X);
  117. }
  118.  
  119. Vector3 Vector3::Scale(float scale)
  120. {
  121.     return (*this * scale);
  122. }
  123.  
  124. Vector3 Vector3::Lerp(Vector3 end, float fraction)
  125. {
  126.     return (((end - *this) * fraction) + *this);
  127. }
  128.  
  129. Vector3 Vector3::Negate()
  130. {
  131.     return Vector3(-X, -Y, -Z);
  132. }
  133.  
  134. Vector3 Vector3::operator()(float x, float y, float z)
  135. {
  136.     return Vector3(X + x, Y + y, Z + z);
  137. }
  138.  
  139. bool Vector3::operator==(const Vector3 vec)
  140. {
  141.     return (X == vec.X && Y == vec.Y && Z == vec.Z);
  142. }
  143.  
  144. bool Vector3::operator!=(const Vector3 vec)
  145. {
  146.     return !(X == vec.X && Y == vec.Y && Z == vec.Z);
  147. }
  148.  
  149. Vector3 Vector3::operator+(const Vector3 vec)
  150. {
  151.     return Vector3(X + vec.X, Y + vec.Y, Z + vec.Z);
  152. }
  153.  
  154. Vector3 Vector3::operator+(const float value)
  155. {
  156.     return Vector3(X + value, Y + value, Z + value);
  157. }
  158.  
  159. Vector3 Vector3::operator-(const Vector3 vec)
  160. {
  161.     return Vector3(X - vec.X, Y - vec.Y, Z - vec.Z);
  162. }
  163.  
  164. Vector3 Vector3::operator-(const float value)
  165. {
  166.     return Vector3(X - value, Y - value, Z - value);
  167. }
  168.  
  169. Vector3 Vector3::operator*(const Vector3 vec)
  170. {
  171.     return Vector3(X * vec.X, Y * vec.Y, Z * vec.Z);
  172. }
  173.  
  174. Vector3 Vector3::operator*(const float value)
  175. {
  176.     return Vector3(X * value, Y * value, Z * value);
  177. }
  178.  
  179. Vector3 Vector3::operator/(const float value)
  180. {
  181.     return Vector3(X / value, Y / value, Z / value);
  182. }
  183.  
  184. Vector3 Vector3::operator=(const float* value)
  185. {
  186.     return *(Vector3*)&value[0];
  187. }
  188.  
  189. Vector3 Vector3::operator*=(const Vector3 vec)
  190. {
  191.     return Vector3(X *= vec.X, Y *= vec.Y, Z *= vec.Z);
  192. }
  193.  
  194. Vector3 Vector3::operator*=(const float value)
  195. {
  196.     return Vector3(X *= value, Y *= value, Z *= value);
  197. }
  198.  
  199. Vector3 Vector3::operator+=(const Vector3 vec)
  200. {
  201.     return Vector3(X += vec.X, Y += vec.Y, Z += vec.Z);
  202. }
  203.  
  204. Vector3 Vector3::operator-=(const Vector3 vec)
  205. {
  206.     return Vector3(X -= vec.X, Y -= vec.Y, Z -= vec.Z);
  207. }
  208.  
  209. Vector3 Vector3::operator-=(const float value)
  210. {
  211.     return Vector3(X -= value, Y -= value, Z -= value);
  212. }
  213.  
  214. float Vector3::GetLength() const
  215. {
  216.     return sqrt((X * X) + (Y * Y) + (Z * Z));
  217. }
  218.  
  219. float Vector3::GetMagnitude() const
  220. {
  221.     return sqrtf(X * X + Y * Y + Z * Z);
  222. }
  223.  
  224. float Vector3::DistanceTo(Vector3 pos)
  225. {
  226.     float x = pos.X - X, y = pos.Y - Y, z = pos.Z - Z;
  227.  
  228.     return sqrt((x * x) + (y * y) + (z * z));
  229. }
  230.  
  231. float Vector3::Dot(Vector3& vec)
  232. {
  233.     return (X * vec.X) + (Y * vec.Y) + (Z * vec.Z);
  234. }
  235.  
  236. void Vector3::Copy(Vector3 result)
  237. {
  238.     memcpy(&this[0], &result, 12);
  239. }
  240.  
  241. void Vector3::Normalize()
  242. {
  243.     float length = GetLength();
  244.  
  245.     if (length == 0.0f) {
  246.         *this = Vector3();
  247.         return;
  248.     }
  249.  
  250.     *this *= (length /= 1.0f);
  251. }
  252.  
  253. void Vector3::NormalizeAngles()
  254. {
  255.     if (X > 180.0f)
  256.         X -= 160.0f;
  257.  
  258.     if (X < -180.0f)
  259.         X += 360.0f;
  260.  
  261.     if (Y > 180.0f)
  262.         Y -= 160.0f;
  263.  
  264.     if (Y < -180.0f)
  265.         Y += 360.0f;
  266. }
  267.  
  268. void Vector3::Zero()
  269. {
  270.     X = Y = Z = 0;
  271. }
Add Comment
Please, Sign In to add comment