src/proto2/Vector.hh
author terom
Tue, 18 Nov 2008 22:58:50 +0000
branchno-netsession
changeset 35 e21cfda0edde
child 41 ca80cd67785d
permissions -rw-r--r--
Merge from at r31:36
#ifndef COOR_H
#define COOR_H

#include <iostream>

/**
 * 2D Vector class. Implements standard vector operations.
 */
template <typename T>
class _Vector {
public:
    T x;
    T y;
    
    _Vector() : x(0), y(0){}
    /**
     * @param x Initial x-coordinate.
     * @param y Initial y-coordinate.
     */
    _Vector(T x, T y) : x(x), y(y) {}
    /**
     * @param v Other vector to be copied.
     */
    _Vector(const _Vector &v) : x(v.x), y(v.y) {}

    void operator=(const _Vector &v) {
        this->x = v.x;
        this->y = v.y;
    }
    _Vector operator+(const _Vector &v) const {
        return _Vector(this->x+v.x, this->y+v.y);
    }
    _Vector operator-(const _Vector &v) const {
        return _Vector(this->x-v.x, this->y-v.y);
    }
    _Vector operator*(const T &d) const {
        return _Vector(this->x*d, this->y*d);
    }
    _Vector operator/(const T &d) const {
        return _Vector(this->x/d, this->y/d);
    } 
    void operator+=(const _Vector &v) {
        this->x += v.x;
        this->y += v.y;
    }
    void operator-=(const _Vector &v) {
        this->x -= v.x;
        this->y -= v.y;
    }
    void operator*=(const T &f) {
        this->x *= f;
        this->y *= f;
    }
    void operator/=(const T &d) {
        this->x /= d;
        this->y /= d;
    }
};

template<typename T>
bool operator==(const _Vector<T> &v1, const _Vector<T> &v2) {
    return ((v1.x == v2.x) && (v1.y == v2.y));
}
template<typename T>
bool operator!=(const _Vector<T> &v1, const _Vector<T> &v2) {
    return !(v1 == v2);
}

template<typename T>
std::ostream& operator<<(std::ostream &s, const _Vector<T> &v) {
    return s<<"("<<v.x<<", "<<v.y<<")";
}

typedef _Vector<float> Vector;

#endif