The Allegro Wiki is migrating to github at https://github.com/liballeg/allegro_wiki/wiki

Vector2D

From Allegro Wiki
Jump to: navigation, search

Just include vec2.h in your C++ programs and be happy. Scroll down for a test and example program. I hope this is fairly self explaining.

Someone should provide an overloaded operator<< for formatted output.

vec2.h

<highlightSyntax language="cpp">//A 2d-vector class

  1. ifndef __VEC2__
  2. define __VEC2__
  1. include <cmath>

template<typename Val> class Vec2 {

   Val mx,my;

public:

   Vec2(Val xx = Val(), Val yy = Val())
       : mx(xx), my(yy)
   { }
   

/* Vec2(const Vec2& v)

*         : mx(v.mx), my(v.my)
*     { }
*     
*     Vec2& operator=(Vec2 v)
*     {
*         mx = v.mx;
*         my = v.my;
*         return *this;
*     }
*/
   
   void setPolar(Val radians, Val distance)
   {
       mx = distance*cos(radians);
       my = distance*sin(radians);
   }
   
   Val x() const { return mx; }
   Val y() const { return my; }
   
   Val radians() const
   {
       if(mx == my == 0.0)
           return 0;
       return atan2(my, mx);
   }


   Val magnitudeSquared() const
   {
       return mx*mx+my*my;
   }
   
   Val magnitude() const
   {
       return sqrt(magnitudeSquared());
   }
   
   Vec2& normalize()
   {
       Val m = magnitude();
       if(m != 0.0)
       {
           mx /= m;
           my /= m;
       }
       return *this;
   }
   
   Vec2& operator+=(const Vec2& rhs)
   {
       mx += rhs.mx;
       my += rhs.my;
       return *this;
   }
   
   Vec2& operator-=(const Vec2& rhs)
   {
       mx -= rhs.mx;
       my -= rhs.my;
       return *this;
   }
   Vec2& operator*=(const Vec2& rhs)
   {
       mx *= rhs;
       my *= rhs;
       return *this;
   }
   Vec2& operator/=(const Vec2& rhs)
   {
       mx /= rhs;
       my /= rhs;
       return *this;
   }
   
   //Vector2DAddition / Subtraction

Vec2 operator+ (const Vec2 &rhs) const { return Vec2(mx + rhs.mx, my + rhs.my); }

Vec2 operator- (const Vec2 &rhs) const { return Vec2(mx - rhs.mx, my - rhs.my); }

Vec2 operator* (Val rhs) const { return Vec2(mx * rhs, my * rhs); }

Vec2 operator/ (Val rhs) const { return Vec2(mx / rhs, my / rhs); }

//Vector2DDot product Val dotproduct (const Vec2 &rhs) { return mx*rhs.mx + my*rhs.my; }

//Vector2DSign operators

Vec2 operator- () const { return Vec2(-mx, -my); }

Vec2 operator+ () const { return Vec2(+mx, +my); }

//Vector2DComparison

bool operator== (const Vec2 &rhs) const { return (mx == rhs.mx) && (my == rhs.my); }

bool operator!= (const Vec2 &rhs) const { return !(*this == rhs); } };

//Vector2DScalar product template<typename Val> inline Vec2<Val> operator*(Val lhs, Vec2<Val> rhs) {

   return rhs * lhs;

}

//Vector2DAbsolute value / magnitude

template<typename Val> inline Val abs(const Vec2<Val> &v) {

   return v.magnitude();

}


//A couple of handy typedefs

typedef Vec2<float> Vec2f; typedef Vec2<double> Vec2d;

typedef Vec2<int> Vec2i;

  1. endif
</highlightSyntax>

test.cpp

<highlightSyntax language="cpp">#include "vec2.h"

  1. include <iostream>
  2. include <string>
  3. include <sstream>

using namespace std;

template <typename Val> string s(Vec2<Val> v) {

   stringstream s;
   s << "(" << v.x() << ", " << v.y() << ") ";
   return s.str();

}

int main() {

   Vec2d v1, v2(1,1), v3(-3,1), v4(2,-4), v5(-2,-2);
   cout << s(v1) << s(v2) << s(v3) << s(v4) << s(v5) << endl;
   cout << s(v1) << "+ " << s(v3) << "= " << s(v3+v1) << endl;
   cout << s(v2) << "- " << s(v4) << "= " << s(v2-v4) << endl;
   cout << "3 * " << s(v5) << "= " << s(3.0*v5) << endl;
   cout << s(v4) << "* 7.3" << "= " << s(v4*7.3) << endl;
   cout << "Sum of all vectors: " << s(v1+v2+v3+v4+v5) << endl;
   cout << s(v3) << "/ 1/2 = " << s(v3/0.5) << endl;
   cout << "Magnitude of " << s(v5) << " = " << v5.magnitude() << endl;
   v5 *= 132409;
   cout << "Normalized vector of " << s(v5) << " = " << s(~v5) << endl;
   cout << "Polar form of " << s(v3) << ": angle = " << v3.radians() << "; distance = " << v3.magnitude() << endl;
   cout << "Dot-Product of " << s(Vec2d(-2,2)) << "and " << s(v5) << " = " << Vec2d(-2,2) * v5 << endl;
   cout << "Dot-Product of " << s(v1) << "and " << s(v5) << " = " << v1 * v5 << endl;
   cout << "Dot-Product of " << s(v2) << "and " << s(v5) << " = " << v2 * v5 << endl;
   cout << "v2 == v3 returns " << v2==v3 << " and v2 != v3 returns " << v2!=v3 << endl;

}

</highlightSyntax>