Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------
  2. //
  3. // JGE++ is a hardware accelerated 2D game SDK for PSP/Windows.
  4. //
  5. // Licensed under the BSD license, see LICENSE in JGE root for details.
  6. //
  7. // Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
  8. //
  9. //-------------------------------------------------------------------------------------
  10. // 14/05/2011 : optimised + Matrix22 structure added by Akabane87
  11. //-------------------------------------------------------------------------------------
  12.  
  13.  
  14. #ifndef _VECTOR2D_H
  15. #define _VECTOR2D_H
  16.  
  17. #ifdef WIN32
  18. #include <math.h>
  19. #else
  20. #include <fastmath.h>
  21. #endif
  22.  
  23. #ifdef PSP
  24. #include <pspmath.h>
  25. #endif
  26.  
  27.  
  28. struct Vector2D
  29. {
  30.  
  31.     float x, y;
  32.     static const Vector2D& Zero() { static const Vector2D V(0, 0); return V; }
  33.  
  34.     inline Vector2D(void)   {}
  35.  
  36.     inline Vector2D(const float& _x, const float& _y) : x(_x), y(_y)    {}
  37.  
  38.     inline void Set(const float& _x, const float& _y) {x = _x; y = _y;}
  39.  
  40.     inline Vector2D &operator /=(const float& scalar)   { x /= scalar; y /= scalar;     return *this; }
  41.  
  42.     inline Vector2D &operator *=(const float& scalar)   { x *= scalar; y *= scalar;     return *this; }
  43.    
  44.     inline Vector2D &operator +=(const Vector2D &v) { x += v.x; y += v.y;   return *this; }
  45.  
  46.     inline Vector2D &operator -=(const Vector2D &v) { x -= v.x; y -= v.y;   return *this;   }
  47.  
  48.     inline bool operator ==(const Vector2D &v) const { return x == v.x && y == v.y; }
  49.     inline bool operator !=(const Vector2D &v) const { return x != v.x || y != v.y; }
  50.  
  51.  
  52.     // cross product
  53.     inline float operator ^ (const Vector2D &v) const   { return (x * v.y) - (y * v.x); }
  54.  
  55.     // dot product
  56.     inline float operator * (const Vector2D &v) const   { return (x*v.x) + (y*v.y); }
  57.  
  58.     inline float Dot(const Vector2D &v) const   { return (x * v.x) + (y * v.y); }
  59.     inline float Cross(const Vector2D &v) const { return (x * v.y) - (y * v.x); }
  60.    
  61.  
  62.     inline Vector2D operator * (const float& s)         const   {   return Vector2D(x*s, y*s); }
  63.     inline Vector2D operator / (const float& s)         const   {   return Vector2D(x/s, y/s); }
  64.     inline Vector2D operator + (const Vector2D &v)  const   {   return Vector2D(x+v.x, y+v.y); }
  65.     inline Vector2D operator - (const Vector2D &v)  const   {   return Vector2D(x-v.x, y-v.y); }
  66.     friend Vector2D operator * (const float& k, const Vector2D& v) {    return Vector2D(v.x*k, v.y*k); }
  67.     inline Vector2D operator -(void) const { return Vector2D(-x, -y); }
  68.  
  69.     float Length(void) const
  70.     {
  71. #ifdef PSP
  72.         return vfpu_sqrtf(x*x + y*y);
  73. #else
  74.         return sqrtf(x*x + y*y);
  75. #endif
  76.     }
  77.  
  78.     float Normalize(void)
  79.     {  
  80.         float fLength = Length();  
  81.  
  82.         if (fLength == 0.0f)
  83.             return 0.0f;
  84.  
  85.         (*this) *= (1.0f / fLength);
  86.  
  87.         return fLength;
  88.     }
  89.  
  90.  
  91.     Vector2D Direction(void) const
  92.     {
  93.         Vector2D temp(*this);
  94.  
  95.         temp.Normalize();
  96.  
  97.         return temp;
  98.     }
  99.  
  100.     float Angle(const Vector2D& xE)// slow
  101.     {
  102.         float dot = (*this) * xE;
  103.         float cross = (*this) ^ xE;
  104.  
  105.         // angle between segments
  106. #ifdef PSP
  107.         float angle = vfpu_atan2f(cross, dot);
  108. #else
  109.         float angle = atan2f(cross, dot);
  110. #endif
  111.  
  112.         return angle;
  113.     }
  114.  
  115.     float Angle() const // slow
  116.     {
  117. #ifdef PSP
  118.         return vfpu_atan2f(y, x);
  119. #else
  120.         return atan2f(y, x);
  121. #endif
  122.     }
  123.  
  124.  
  125.     Vector2D& Rotate(const float& angle)// slow : use this only if you can't rotate from a matrix
  126.     {
  127. #ifdef PSP
  128.         float c, s;
  129.         vfpu_sincos(angle, &s, &c);
  130. #else
  131.         float c, s;
  132.         c = cosf(angle);
  133.         s = sinf(angle);
  134. #endif
  135.  
  136.         float tx = x;
  137.         x = tx * c - y * s;
  138.         y = tx * s + y * c;
  139.  
  140.         return *this;
  141.     }
  142.  
  143.  
  144.  
  145.     Vector2D& Rotate(const Vector2D& xCentre, const float& fAngle)
  146.     {
  147.         Vector2D D = *this - xCentre;
  148.         D.Rotate(fAngle);
  149.  
  150.         *this = xCentre + D;
  151.  
  152.         return *this;
  153.     }
  154.  
  155.  
  156.     void Clamp(const Vector2D& min, const Vector2D& max)
  157.     {
  158.         x = (x < min.x)? min.x : (x > max.x)? max.x : x;
  159.         y = (y < min.y)? min.y : (y > max.y)? max.y : y;
  160.     }
  161.  
  162. };
  163.  
  164.  
  165. struct Matrix22
  166. {
  167.     Matrix22() {}
  168.     Matrix22(const Vector2D& c1, const Vector2D& c2)
  169.     {
  170.         col1 = c1;
  171.         col2 = c2;
  172.     }
  173.  
  174.     Matrix22(const float& angle)
  175.     {
  176. #ifdef PSP
  177.         float c, s;
  178.         vfpu_sincos(angle, &s, &c);
  179. #else
  180.         float c = cosf(angle), s = sinf(angle);
  181. #endif
  182.         col1.x = c; col2.x = -s;
  183.         col1.y = s; col2.y = c;
  184.     }
  185.  
  186.     void Set(const Vector2D& c1, const Vector2D& c2)
  187.     {
  188.         col1 = c1;
  189.         col2 = c2;
  190.     }
  191.  
  192.     void Set(const float& angle)
  193.     {
  194. #ifdef PSP
  195.         float c, s;
  196.         vfpu_sincos(angle, &s, &c);
  197. #else
  198.         float c = cosf(angle), s = sinf(angle);
  199. #endif
  200.         col1.x = c; col2.x = -s;
  201.         col1.y = s; col2.y = c;
  202.     }
  203.  
  204.     static const Matrix22& Identity()
  205.     {
  206.         static const Matrix22 M(Vector2D (1, 0), Vector2D (0, 1));
  207.         return M;
  208.     }
  209.  
  210.     void SetIdentity()
  211.     {
  212.         col1.x = 1.0f; col2.x = 0.0f;
  213.         col1.y = 0.0f; col2.y = 1.0f;
  214.     }
  215.  
  216.     static const Matrix22& Zero()
  217.     {
  218.         static const Matrix22 M(Vector2D (0, 0), Vector2D (0, 0));
  219.         return M;
  220.     }
  221.  
  222.     void SetZero()
  223.     {
  224.         col1.x = 0.0f; col2.x = 0.0f;
  225.         col1.y = 0.0f; col2.y = 0.0f;
  226.     }
  227.  
  228.     Matrix22 Invert() const
  229.     {
  230.         float a = col1.x, b = col2.x, c = col1.y, d = col2.y;
  231.         Matrix22 B;
  232.         float det = a * d - b * c;
  233.         if(det == 0.0f) return Zero();
  234.         det = 1.0f / det;
  235.         B.col1.x =  det * d;    B.col2.x = -det * b;
  236.         B.col1.y = -det * c;    B.col2.y =  det * a;
  237.         return B;
  238.     }
  239.  
  240.     inline Matrix22 operator -(void) const
  241.     {
  242.         float a = col1.x, b = col2.x, c = col1.y, d = col2.y;
  243.         Matrix22 B;
  244.         float det = a * d - b * c;
  245.         if(det == 0.0f) return Zero();
  246.         det = 1.0f / det;
  247.         B.col1.x =  det * d;    B.col2.x = -det * b;
  248.         B.col1.y = -det * c;    B.col2.y =  det * a;
  249.         return B;
  250.     }
  251.  
  252.     // Solve A * x = b
  253.     Vector2D Solve(const Vector2D& b) const
  254.     {
  255.         float a11 = col1.x, a12 = col2.x, a21 = col1.y, a22 = col2.y;
  256.         float det = a11 * a22 - a12 * a21;
  257.         if(det == 0.0f) return Vector2D::Zero();
  258.         det = 1.0f / det;
  259.         Vector2D x;
  260.         x.x = det * (a22 * b.x - a12 * b.y);
  261.         x.y = det * (a11 * b.y - a21 * b.x);
  262.         return x;
  263.     }
  264.  
  265.     inline Vector2D Mul (const Vector2D& v) const
  266.     {
  267.         Vector2D u(col1.x * v.x + col2.x * v.y, col1.y * v.x + col2.y * v.y);
  268.         return u;
  269.     }
  270.  
  271.     // A * B
  272.     inline Matrix22 Mul(const Matrix22& A)
  273.     {
  274.         Matrix22 C(this->Mul(A.col1), this->Mul(A.col2));
  275.         return C;
  276.     }
  277.  
  278.     inline Vector2D operator * (const Vector2D& v) const
  279.     {
  280.         Vector2D u(col1.x * v.x + col2.x * v.y, col1.y * v.x + col2.y * v.y);
  281.         return u;
  282.     }
  283.  
  284.     inline Vector2D MulT (const Vector2D& v) const
  285.     {
  286.         Vector2D u(v*col1, v*col2);
  287.         return u;
  288.     }
  289.  
  290.     // A^T * B
  291.     inline Matrix22 MulT(const Matrix22& A)
  292.     {
  293.         Vector2D c1(col1*A.col1, col2*A.col1);
  294.         Vector2D c2(col1*A.col2, col2*A.col2);
  295.         Matrix22 C(c1, c2);
  296.         return C;
  297.     }
  298.  
  299.     inline Vector2D operator / (const Vector2D& v) const
  300.     {
  301.         Vector2D u(v*col1, v*col2);
  302.         return u;
  303.     }
  304.  
  305.     inline Matrix22 operator + (const Matrix22& A) const
  306.     {
  307.         Matrix22 C(col1 + A.col1, col2 + A.col2);
  308.         return C;
  309.     }
  310.  
  311.     inline bool operator == (const Matrix22& A) const { return col1 == A.col1 && col2 == A.col2; }
  312.     inline bool operator != (const Matrix22& A) const { return col1 != A.col1 || col2 != A.col2; }
  313.  
  314.  
  315.     Vector2D col1, col2;
  316. };
  317.  
  318. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement