inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_vec2f 00005 #define inlib_vec2f 00006 00007 #include "a2" 00008 00009 #include "fmath" 00010 00011 namespace inlib { 00012 00013 class vec2f : public inlib::a2::vec<float> { 00014 public: 00015 vec2f(): inlib::a2::vec<float>() {} 00016 vec2f(const float a_vec[2]): inlib::a2::vec<float>(a_vec) {} 00017 vec2f(float a0,float a1) 00018 : inlib::a2::vec<float>(a0,a1){} 00019 virtual ~vec2f() {} 00020 public: 00021 vec2f(const vec2f& a_from): inlib::a2::vec<float>(a_from){} 00022 vec2f& operator=(const vec2f& a_from){ 00023 inlib::a2::vec<float>::operator=(a_from); 00024 return *this; 00025 } 00026 public: //operators 00027 float& operator[](unsigned int a_index) { 00028 //WARNING : no check on a_index. 00029 return m_vector[a_index]; 00030 } 00031 const float& operator[](unsigned int a_index) const { 00032 //WARNING : no check on a_index. 00033 return m_vector[a_index]; 00034 } 00035 vec2f operator*(float a_v) const { 00036 return vec2f(m_vector[0]*a_v, 00037 m_vector[1]*a_v); 00038 } 00039 vec2f operator+(const vec2f& a_v) const { 00040 return vec2f(m_vector[0]+a_v[0], 00041 m_vector[1]+a_v[1]); 00042 } 00043 vec2f operator-(const vec2f& a_v) const { 00044 return vec2f(m_vector[0]-a_v[0], 00045 m_vector[1]-a_v[1]); 00046 } 00047 vec2f& operator+=(const vec2f& a_v) { 00048 m_vector[0] += a_v[0]; 00049 m_vector[1] += a_v[1]; 00050 return *this; 00051 } 00052 vec2f& operator*=(float a_v) { 00053 m_vector[0] *= a_v; 00054 m_vector[1] *= a_v; 00055 return *this; 00056 } 00057 vec2f operator-() const { 00058 return vec2f(-m_vector[0],-m_vector[1]); 00059 } 00060 bool operator==(const vec2f& a_v) const {return equal(a_v);} 00061 bool operator!=(const vec2f& a_v) const {return !operator==(a_v);} 00062 public: 00063 float x() const {return m_vector[0];} 00064 float y() const {return m_vector[1];} 00065 float length() const { 00066 return fsqrt(m_vector[0] * m_vector[0] + 00067 m_vector[1] * m_vector[1]); 00068 } 00069 float normalize() { 00070 float norme = length(); 00071 if(norme==0) return 0; 00072 divide(norme); 00073 return norme; 00074 } 00075 float cross(const vec2f& aV) const { 00076 return (m_vector[0] * aV.m_vector[1] - m_vector[1] * aV.m_vector[0]); 00077 } 00078 float dot(const vec2f& aV) const { 00079 return (m_vector[0] * aV.m_vector[0] + 00080 m_vector[1] * aV.m_vector[1]); 00081 } 00082 public: 00083 //NOTE : don't handle a static object because of mem balance. 00084 //static const vec2f& x_axis() { 00085 // static const vec2f s_v(1,0); 00086 // return s_v; 00087 //} 00088 //static const vec2f& y_axis() { 00089 // static const vec2f s_v(0,1); 00090 // return s_v; 00091 //} 00092 private: 00093 static void check_instantiation() { 00094 vec2f dummy(0,0); 00095 dummy.set_value(1,1); 00096 } 00097 }; 00098 00099 inline vec2f operator*(float a_f,const vec2f& a_v) { 00100 vec2f res(a_v); 00101 res *= a_f; 00102 return res; 00103 } 00104 00105 inline std::ostream& operator<<(std::ostream& a_out,const vec2f& a_this){ 00106 a_out << "x = " << a_this[0] 00107 << ",y = " << a_this[1] 00108 << std::endl; 00109 return a_out; 00110 } 00111 00112 } 00113 00114 #endif