83 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #pragma once
 | |
| #include <cfloat>
 | |
| #define _USE_MATH_DEFINES
 | |
| #include <cmath>
 | |
| 
 | |
| namespace KapitanGame {
 | |
| 	struct KVector2D {
 | |
| 		float X;
 | |
| 		float Y;
 | |
| 
 | |
| 		KVector2D(const float x, const float y) : X(x), Y(y) {}
 | |
| 
 | |
| 		float Length() const { return sqrt(X * X + Y * Y); }
 | |
| 		float Length2() const { return X * X + Y * Y; }
 | |
| 
 | |
| 		bool operator==(const KVector2D& v2) const {
 | |
| 			return (*this - v2).Length2() < FLT_EPSILON * FLT_EPSILON;
 | |
| 		}
 | |
| 
 | |
| 		KVector2D operator+(const KVector2D& v2) const {
 | |
| 			return { X + v2.X, Y + v2.Y };
 | |
| 		}
 | |
| 
 | |
| 		friend KVector2D& operator+=(KVector2D& v1, const KVector2D& v2) {
 | |
| 			v1.X += v2.X;
 | |
| 			v1.Y += v2.Y;
 | |
| 			return v1;
 | |
| 		}
 | |
| 
 | |
| 		KVector2D operator*(const float scalar) const
 | |
| 		{
 | |
| 			return { X * scalar, Y * scalar };
 | |
| 		}
 | |
| 
 | |
| 		KVector2D& operator*=(const float scalar) {
 | |
| 			X *= scalar;
 | |
| 			Y *= scalar;
 | |
| 			return *this;
 | |
| 		}
 | |
| 
 | |
| 		KVector2D operator-(const KVector2D& v2) const {
 | |
| 			return { X - v2.X, Y - v2.Y };
 | |
| 		}
 | |
| 		friend KVector2D& operator-=(KVector2D& v1, const KVector2D& v2) {
 | |
| 			v1.X -= v2.X;
 | |
| 			v1.Y -= v2.Y;
 | |
| 			return v1;
 | |
| 		}
 | |
| 
 | |
| 		KVector2D operator/(const float scalar) const
 | |
| 		{
 | |
| 			return { X / scalar, Y / scalar };
 | |
| 		}
 | |
| 		KVector2D& operator/=(const float scalar) {
 | |
| 			X /= scalar;
 | |
| 			Y /= scalar;
 | |
| 			return *this;
 | |
| 		}
 | |
| 		float DotProduct(const KVector2D& v2) const {
 | |
| 			return DotProduct(*this, v2);
 | |
| 		}
 | |
| 		KVector2D Reflect(const KVector2D& normal) const {
 | |
| 			return Reflect(normal, *this);
 | |
| 		}
 | |
| 
 | |
| 		void Normalize() {
 | |
| 			const float l = Length();
 | |
| 			if (l > 1.f) {
 | |
| 				(*this) *= 1 / l;
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		static KVector2D Reflect(const KVector2D& normal, const KVector2D& vector) {
 | |
| 			const float dn = 2 * vector.DotProduct(normal);
 | |
| 			return vector - normal * dn;
 | |
| 		}
 | |
| 		static float DotProduct(const KVector2D& v1, const KVector2D& v2) {
 | |
| 			return  v1.X * v2.X + v1.Y * v2.Y ;
 | |
| 		}
 | |
| 	};
 | |
| }
 | |
| 
 |