|
@@ -22,28 +22,141 @@
|
|
|
|
|
|
|
|
#pragma once
|
|
#pragma once
|
|
|
#include "PolyGlobals.h"
|
|
#include "PolyGlobals.h"
|
|
|
-#include "PolyVector3.h"
|
|
|
|
|
-
|
|
|
|
|
|
|
+#include <math.h>
|
|
|
|
|
+#include <assert.h>
|
|
|
|
|
+
|
|
|
namespace Polycode {
|
|
namespace Polycode {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 2D Vector (convenience wrapper around Vector3).
|
|
* 2D Vector (convenience wrapper around Vector3).
|
|
|
*/
|
|
*/
|
|
|
- class _PolyExport Vector2 : public Vector3 {
|
|
|
|
|
|
|
+ class _PolyExport Vector2 {
|
|
|
public:
|
|
public:
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Create from x,y,z coordinates.
|
|
|
|
|
+ * @param x X coordinate.
|
|
|
|
|
+ * @param y Y coordinate.
|
|
|
|
|
+ */
|
|
|
|
|
+ Vector2(Number x,Number y);
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Default constructor.
|
|
* Default constructor.
|
|
|
- */
|
|
|
|
|
|
|
+ */
|
|
|
Vector2();
|
|
Vector2();
|
|
|
-
|
|
|
|
|
|
|
+ ~Vector2();
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * Create from x,y coordinates.
|
|
|
|
|
|
|
+ * Sets the vector from x,y,z coordinates.
|
|
|
* @param x X coordinate.
|
|
* @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:
|
|
protected:
|
|
|
|
|
|