Saturday, January 21, 2012

Bring on the Vectors...

Hey everybody. Today I finished up building a simple 2d Vector class. I will be using this class not only in this project, but also many other projects in the future. I ran it through several tests, making sure it didn't break. This is the class that I came up with:



/* 
 * File:   Vector2.h
 * Author: Eric
 *
 * Created on January 20, 2012, 8:33 AM
 */

#ifndef VECTOR2_H
#define VECTOR2_H

#include 
#include "math.h"

class Vector2 {
private:
    float x, y;
    
public:
    Vector2():x(0.0f), y(0.0f){};
    Vector2(float x, float y):x(x),y(y){};
        
    float getX(){return x;};
    float getY(){return y;};
    void setX(float a){x=a;};
    void setY(float a){y=a;};
    
    float length(){return sqrt(x*x + y*y);};
    float mag2(){return x*x + y*y;};
    void normalize(){
        float Length = length(); 
        if(Length != 0){
                x = x/Length;
                y = y/Length;
        }  
    };
    
    Vector2 normalized(){
        float length = this->length();
        if(length != 0){
            return Vector2(x/length, y/length);
        } 
        return Vector2();
    };
    
    bool operator==(const Vector2 a){return (x == a.x) && (y == a.y);};
    
    Vector2 operator+(const Vector2 a){return Vector2(x + a.x, y + a.y);};
    Vector2 operator-(const Vector2 a){return Vector2(x - a.x, y - a.y);};
    Vector2 operator-(){return Vector2(-x,-y);};
    
    template Vector2 operator+(const T a){return Vector2(a + x, a + y);};
    template Vector2 operator-(const T a){return Vector2(a - x, a - y);};
    template Vector2 operator*(const T a){return Vector2(a * x, a * y);};
    template Vector2 operator/(const T a){return Vector2(x/a, y/a);};
    
    friend std::ostream& operator<< (std::ostream &out, const Vector2 &c){ return out << "(" << c.x << "," << c.y << ")"; };
    
    float dot(const Vector2 a){return x * a.x + y * a.y;};
    
    static const Vector2 zero;
    static const Vector2 unit_x;
    static const Vector2 unit_y;
};

 const Vector2 Vector2::zero = Vector2();
 const Vector2 Vector2::unit_x = Vector2(1.0f, 0.0f); 
 const Vector2 Vector2::unit_y = Vector2(0.0f, 1.0f);

#endif /* VECTOR2_H */



I am quite content with this vector class. I should do everything I need and more. Here are some useful features:

Mathematical Operators We should be able to do the most common operations on this vectors (add, subtract, negate, multiply by any constant, dot product). Also, there are functions to normalize the current vector, or return a normalized copy of the vector.


Static Unit Vectors included are some pre-defined vectors; the Zero Vector, unit X vector, and unit Y vector. 


Simplicity This vector class is simple and fits into one header file. So including it in a project is as simple as coping the Vector2.h file into your header file directory.

Display included is an override for the stream operator (<<), making printing a vector a snap. This will be very useful in debugging.

No comments:

Post a Comment