libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/base/vector_2d.hpp
Go to the documentation of this file.
00001 
00011 /*
00012  *  Copyright (C) 2007, 2009 Juan Pedro BolĂ­var Puente
00013  *
00014  *  This file is part of Psychosynth.
00015  *   
00016  *  Psychosynth is free software: you can redistribute it and/or modify
00017  *  it under the terms of the GNU General Public License as published by
00018  *  the Free Software Foundation, either version 3 of the License, or
00019  *  (at your option) any later version.
00020  *
00021  *  Psychosynth is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  You should have received a copy of the GNU General Public License
00027  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028  *
00029  */
00030 
00031 #ifndef PSYNTH_VECTOR2D_H
00032 #define PSYNTH_VECTOR2D_H
00033 
00034 #include <iostream>
00035 
00036 namespace psynth
00037 {
00038 namespace base
00039 {
00040 
00046 template<class T>
00047 class vector_2d
00048 {
00049     static const int NUM_D = 2;
00050 public:
00051     T x;   
00052     T y;   
00059     vector_2d(T _x = 0, T _y = 0) :
00060         x(_x), y(_y)
00061         {}
00062 
00068     void set(T _x, T _y) {
00069         x = _x;
00070         y = _y;
00071     }
00072 
00077     T& operator[](size_t index) {
00078         return *(&x + index);
00079     }
00080 
00085     const T& operator[](size_t index) const {
00086         return *(&x + index);
00087     }
00088 
00093     T distance(const vector_2d& v) const {
00094         return sqrt (sqr_distance (v));
00095     }
00096 
00103     T sqr_distance(const vector_2d& v) const {
00104         return
00105             (v.x-x) * (v.x-x) +
00106             (v.y-y) * (v.y-y);
00107     }
00108 
00112     T length() const {
00113         return sqrt(x*x + y*y);
00114     }
00115 
00121     T sqr_length() const {
00122         return x*x + y*y;
00123     }
00124 };
00125 
00130 template<class T>
00131 std::ostream& operator<< (std::ostream& os,
00132                           const psynth::base::vector_2d<T> v)
00133 {
00134     os << v.x << " " << v.y;
00135     return os;
00136 }
00137 
00141 typedef vector_2d<float> vector_2f;
00142 
00143 } /* namespace base */
00144 } /* namespace psynth */
00145 
00146 #endif /* PSYNTH_VECTOR2D */
00147