Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXVec2f.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *       S i n g l e - P r e c i s i o n   2 - E l e m e n t   V e c t o r       *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1994,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXVec2f.h,v 1.27 2009/01/26 09:40:23 fox Exp $                           *
00022 ********************************************************************************/
00023 #ifndef FXVEC2F_H
00024 #define FXVEC2F_H
00025 
00026 
00027 namespace FX {
00028 
00029 
00030 class FXMat3f;
00031 
00032 
00033 /// Single-precision 2-element vector
00034 class FXAPI FXVec2f {
00035 public:
00036   FXfloat x;
00037   FXfloat y;
00038 public:
00039 
00040   /// Default constructor; value is not initialized
00041   FXVec2f(){}
00042 
00043   /// Initialize from another vector
00044   FXVec2f(const FXVec2f& v){x=v.x;y=v.y;}
00045 
00046   /// Initialize from array of floats
00047   FXVec2f(const FXfloat v[]){x=v[0];y=v[1];}
00048 
00049   /// Initialize from components
00050   FXVec2f(FXfloat xx,FXfloat yy){x=xx;y=yy;}
00051 
00052   /// Return a non-const reference to the ith element
00053   FXfloat& operator[](FXint i){return (&x)[i];}
00054 
00055   /// Return a const reference to the ith element
00056   const FXfloat& operator[](FXint i) const {return (&x)[i];}
00057 
00058   /// Assignment
00059   FXVec2f& operator=(const FXVec2f& v){x=v.x;y=v.y;return *this;}
00060 
00061   /// Assignment from array of floats
00062   FXVec2f& operator=(const FXfloat v[]){x=v[0];y=v[1];return *this;}
00063 
00064   /// Set value from another vector
00065   FXVec2f& set(const FXVec2f& v){x=v.x;y=v.y;return *this;}
00066 
00067   /// Set value from array of floats
00068   FXVec2f& set(const FXfloat v[]){x=v[0];y=v[1];return *this;}
00069 
00070   /// Set value from components
00071   FXVec2f& set(FXfloat xx,FXfloat yy){x=xx;y=yy;return *this;}
00072 
00073   /// Assigning operators
00074   FXVec2f& operator*=(FXfloat n){x*=n;y*=n;return *this;}
00075   FXVec2f& operator/=(FXfloat n){x/=n;y/=n;return *this;}
00076   FXVec2f& operator+=(const FXVec2f& v){x+=v.x;y+=v.y;return *this;}
00077   FXVec2f& operator-=(const FXVec2f& v){x-=v.x;y-=v.y;return *this;}
00078 
00079   /// Conversions
00080   operator FXfloat*(){return &x;}
00081   operator const FXfloat*() const {return &x;}
00082 
00083   /// Unary
00084   FXVec2f operator+() const { return *this; }
00085   FXVec2f operator-() const { return FXVec2f(-x,-y); }
00086 
00087   /// Vector and vector
00088   FXVec2f operator+(const FXVec2f& v) const { return FXVec2f(x+v.x,y+v.y); }
00089   FXVec2f operator-(const FXVec2f& v) const { return FXVec2f(x-v.x,y-v.y); }
00090 
00091   /// Vector and matrix
00092   FXVec2f operator*(const FXMat3f& m) const;
00093 
00094   /// Scaling
00095   friend inline FXVec2f operator*(const FXVec2f& a,FXfloat n);
00096   friend inline FXVec2f operator*(FXfloat n,const FXVec2f& a);
00097   friend inline FXVec2f operator/(const FXVec2f& a,FXfloat n);
00098   friend inline FXVec2f operator/(FXfloat n,const FXVec2f& a);
00099 
00100   /// Dot product
00101   FXfloat operator*(const FXVec2f& v) const { return x*v.x+y*v.y; }
00102 
00103   /// Test if zero
00104   FXbool operator!() const { return x==0.0f && y==0.0f; }
00105 
00106   /// Equality tests
00107   FXbool operator==(const FXVec2f& v) const { return x==v.x && y==v.y; }
00108   FXbool operator!=(const FXVec2f& v) const { return x!=v.x || y!=v.y; }
00109 
00110   friend inline FXbool operator==(const FXVec2f& a,FXfloat n);
00111   friend inline FXbool operator!=(const FXVec2f& a,FXfloat n);
00112   friend inline FXbool operator==(FXfloat n,const FXVec2f& a);
00113   friend inline FXbool operator!=(FXfloat n,const FXVec2f& a);
00114 
00115   /// Inequality tests
00116   FXbool operator<(const FXVec2f& v) const { return x<v.x && y<v.y; }
00117   FXbool operator<=(const FXVec2f& v) const { return x<=v.x && y<=v.y; }
00118   FXbool operator>(const FXVec2f& v) const { return x>v.x && y>v.y; }
00119   FXbool operator>=(const FXVec2f& v) const { return x>=v.x && y>=v.y; }
00120 
00121   friend inline FXbool operator<(const FXVec2f& a,FXfloat n);
00122   friend inline FXbool operator<=(const FXVec2f& a,FXfloat n);
00123   friend inline FXbool operator>(const FXVec2f& a,FXfloat n);
00124   friend inline FXbool operator>=(const FXVec2f& a,FXfloat n);
00125 
00126   friend inline FXbool operator<(FXfloat n,const FXVec2f& a);
00127   friend inline FXbool operator<=(FXfloat n,const FXVec2f& a);
00128   friend inline FXbool operator>(FXfloat n,const FXVec2f& a);
00129   friend inline FXbool operator>=(FXfloat n,const FXVec2f& a);
00130 
00131   /// Length and square of length
00132   FXfloat length2() const { return x*x+y*y; }
00133   FXfloat length() const { return sqrtf(length2()); }
00134 
00135   /// Clamp values of vector between limits
00136   FXVec2f& clamp(FXfloat lo,FXfloat hi){x=FXCLAMP(lo,x,hi);y=FXCLAMP(lo,y,hi);return *this;}
00137 
00138   /// Lowest or highest components
00139   friend inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b);
00140   friend inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b);
00141 
00142   /// Normalize vector
00143   friend FXAPI FXVec2f normalize(const FXVec2f& v);
00144 
00145   /// Save vector to a stream
00146   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
00147 
00148   /// Load vector from a stream
00149   friend FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
00150   };
00151 
00152 
00153 inline FXVec2f operator*(const FXVec2f& a,FXfloat n){return FXVec2f(a.x*n,a.y*n);}
00154 inline FXVec2f operator*(FXfloat n,const FXVec2f& a){return FXVec2f(n*a.x,n*a.y);}
00155 inline FXVec2f operator/(const FXVec2f& a,FXfloat n){return FXVec2f(a.x/n,a.y/n);}
00156 inline FXVec2f operator/(FXfloat n,const FXVec2f& a){return FXVec2f(n/a.x,n/a.y);}
00157 
00158 inline FXbool operator==(const FXVec2f& a,FXfloat n){return a.x==n && a.y==n;}
00159 inline FXbool operator!=(const FXVec2f& a,FXfloat n){return a.x!=n || a.y!=n;}
00160 inline FXbool operator==(FXfloat n,const FXVec2f& a){return n==a.x && n==a.y;}
00161 inline FXbool operator!=(FXfloat n,const FXVec2f& a){return n!=a.x || n!=a.y;}
00162 
00163 inline FXbool operator<(const FXVec2f& a,FXfloat n){return a.x<n && a.y<n;}
00164 inline FXbool operator<=(const FXVec2f& a,FXfloat n){return a.x<=n && a.y<=n;}
00165 inline FXbool operator>(const FXVec2f& a,FXfloat n){return a.x>n && a.y>n;}
00166 inline FXbool operator>=(const FXVec2f& a,FXfloat n){return a.x>=n && a.y>=n;}
00167 
00168 inline FXbool operator<(FXfloat n,const FXVec2f& a){return n<a.x && n<a.y;}
00169 inline FXbool operator<=(FXfloat n,const FXVec2f& a){return n<=a.x && n<=a.y;}
00170 inline FXbool operator>(FXfloat n,const FXVec2f& a){return n>a.x && n>a.y;}
00171 inline FXbool operator>=(FXfloat n,const FXVec2f& a){return n>=a.x && n>=a.y;}
00172 
00173 inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b){return FXVec2f(FXMIN(a.x,b.x),FXMIN(a.y,b.y));}
00174 inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b){return FXVec2f(FXMAX(a.x,b.x),FXMAX(a.y,b.y));}
00175 
00176 extern FXAPI FXVec2f normalize(const FXVec2f& v);
00177 
00178 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
00179 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
00180 
00181 }
00182 
00183 #endif

Copyright © 1997-2009 Jeroen van der Zijp