Przeglądaj źródła

Cleaned up Vector2

Ivan Safrin 14 lat temu
rodzic
commit
9359018eee

+ 123 - 10
Core/Contents/Include/PolyVector2.h

@@ -22,28 +22,141 @@
 
 #pragma once
 #include "PolyGlobals.h"
-#include "PolyVector3.h"
-
+#include <math.h>
+#include <assert.h>
+	
 namespace Polycode {
 	
 	/**
 	* 2D Vector (convenience wrapper around Vector3). 
 	*/
-	class _PolyExport Vector2 : public Vector3 {
+	class _PolyExport Vector2 {
 		public:
-		
+				
+			/**
+			* Create from x,y,z coordinates.
+			* @param x X coordinate.
+			* @param y Y coordinate.									
+			*/					
+			Vector2(Number x,Number y);		
+			
 			/**
 			* Default constructor.
-			*/
+			*/ 
 			Vector2();
-			
+			~Vector2();
+
 			/**
-			* Create from x,y coordinates.
+			* Sets the vector from x,y,z coordinates.
 			* @param x X coordinate.
-			* @param y Y coordinate.			
+			* @param y Y coordinate.												
+			*/
+			void set(Number x, Number y);
+
+			inline Vector2 operator - ( const Vector2& v2 ) const {
+				return Vector2(x - v2.x, y - v2.y);
+			}
+
+			/**
+			* Returns the distance from this vector to another one.
+			* @param vec2 Second vector.
+			* @return Distance to the other vector.
+			*/
+			inline Number distance(const Vector2& vec2) const {
+				return (*this - vec2).length();
+			}
+			
+			// ----------------------------------------------------------------------------------------------------------------
+			/** @name Operators
+			*  Available vector operators.
+			*/
+			//@{
+			
+
+			inline Vector2 operator * (const Number val) const {
+				return Vector2(x * val, y * val);
+			}
+
+			inline Vector2 operator / (const Number val) const {
+				assert( val != 0.0 );
+				return operator*(1/val);
+			}
+
+			inline Vector2& operator = ( const Vector2& v2)  {
+				x = v2.x;
+				y = v2.y;
+				return *this;
+			}
+
+			inline Vector2& operator += ( const Vector2& v2)  {
+				x += v2.x;
+				y += v2.y;
+				return *this;
+			}
+
+			inline Vector2& operator -= ( const Vector2& v2)  {
+				x -= v2.x;
+				y -= v2.y;
+				return *this;
+			}
+	
+			inline Vector2 operator + ( const Vector2& v2 ) const {
+				return Vector2(x + v2.x, y + v2.y);
+			}		
+
+
+			inline bool operator == ( const Vector2& v2)  {
+				return (v2.x == x && v2.y == y);
+			}		
+
+			inline bool operator != ( const Vector2& v2)  {
+				return (v2.x != x || v2.y != y);
+			}				
+		
+			//@}
+			// ----------------------------------------------------------------------------------------------------------------
+	
+
+			/**
+			* Returns the vector length.
+			* @return Length of the vector.
+			*/
+			inline Number length () const {
+				return sqrtf( x * x + y * y);
+			}
+			
+			/**
+			* Returns the dot product with another vector.
+			* @return Dor product with the vector.
 			*/			
-			Vector2(Number x, Number y);
-			~Vector2();
+			inline Number dot(const Vector2 &u) const {
+				return x * u.x + y * u.y;
+			}
+
+			/**
+			* Returns the cross product with another vector.
+			* @param vec2 Second vector.
+			* @return Cross product with the vector.
+			*/
+			inline Number crossProduct( const Vector2& vec2 ) const {
+				return x * vec2.y - y * vec2.x;
+			}			
+
+			/**
+			* Normalizes the vector.
+			*/
+			void Normalize();
+		
+			/**
+			* X coordinate.
+			*/
+			Number x;
+			
+			/**
+			* Y coordinate.
+			*/			
+			Number y;
+			
 					
 		protected:
 

+ 22 - 5
Core/Contents/Source/PolyVector2.cpp

@@ -24,12 +24,29 @@
 
 using namespace Polycode;
 	
-Vector2::Vector2() : Vector3() {
+Vector2::Vector2() : x(0),y(0) {
+
+
 }
-	
-Vector2::Vector2(Number x, Number y) : Vector3(x,y,0) {
+
+void Vector2::set(Number x, Number y) {
+	this->x = x;
+	this->y = y;
 }
-	
-	
+
+Vector2::Vector2(Number x,Number y) {
+	set(x,y);
+}
+
+void Vector2::Normalize() {
+	Number tL = sqrtf( x * x + y * y);
+	if(tL > 1e-08 ) {
+		Number invTl = 1.0 / tL;
+		x *= invTl;
+		y *= invTl;
+	}
+}
+
 Vector2::~Vector2() {
+
 }