2d vector handling class

2d vector handling class

Fast 2d vector handling class by templated operators overload. Useful for game development, or math programming.

  • Language: C/C++
  • Released: May 14, 2011
    Last Update: May 25, 2011

This class manages 2d vectors by templated , overloaded operators. Common operations included are scaling, sum, subtraction, dotproduct, modulus, distance , squared distance and others, angle beetween vectors , also templated types for the most used numeric formats are included.

Usage examples :

CVec2f v1,v2,v3;

float d,l;

v1.set( 10,20 );            // initialise vectors

v2.set( 30,40 );            // initialise vectors

v3=v1-v2;                    // subtraction

v3=v1+v2;                   // addition

v3=-v1-v2;                  // sign before vector argument

v3+=v1;                      // increment

v3-=v1;                       // decrement

v1=v2*10.0f;               // scaling

v1*=10.0f;                   // scaling

v2=v3/10.0f;               // inverse scaling

v3/=5.0f;                    // inverse scaling

v3=lerp( v1,v2,0.5 );  // linear interpolation between vectors

v3= project ( v1,v2 ); // Calculates the projection of  v1 onto v2

v3=perpendicular( v1,v2 ); // Calculates the component of v1 perpendicular to v2

v3=orthogonalize( v1,v2 );   // orthogonalization

v3 = cross ( v2,v1 );  // vectorial cross product

v3=reflect( v1, v2 ); // reflect vector v1 respect v2

d= dot( v1,v2 );         // dot product

v3.normalize();         // normalize vector

l=v1.length();            // lenght of vector

l=v1.squaredlength();   // squared lenght

v2.zero();          // sets all vector components to zero

v1.fabsf(); // absoulte values for each vector components

// logic comparisons

if ( v2!=v1 ) cout << "vectors are different";
if ( v2==v1 ) cout << "vectors are identical";
if ( v2>=v1 ) cout << "vectors v2 is greater or equal than v1";
if ( v2<=v1 ) cout << "vectors v2 is smaller or equal than v1";
if ( v2>v1 ) cout << "vectors v2 is greater than v1";
if ( v2
You need to log-in or create an account
  • Create an account
  • Log-in
Please use your real name.
Activation link will be sent to this address.
Minimum 8 characters
Enter your password again

Clicking this button confirms you read and agreed to the terms of use and privacy policy.


Save your watchlist

Fill your details below to receive project updates from your watch list - including new versions, price changes and discounts.

I agree to the terms of use and privacy policy.

1 license From » $4.99 14 day money-back guarantee View Licenses
or Get a quote

for customization or integration services

Post a comment

Or enter your name and Email
No comments have been posted yet.