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,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (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 GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXVec2f.h,v 1.21 2006/01/22 17:58:12 fox Exp $                           *
00023 ********************************************************************************/
00024 #ifndef FXVEC2F_H
00025 #define FXVEC2F_H
00026 
00027 
00028 namespace FX {
00029 
00030 
00031 class FXMat3f;
00032 
00033 
00034 /// Single-precision 2-element vector
00035 class FXAPI FXVec2f {
00036 public:
00037   FXfloat x;
00038   FXfloat y;
00039 public:
00040 
00041   /// Default constructor
00042   FXVec2f(){}
00043 
00044   /// Initialize from another vector
00045   FXVec2f(const FXVec2f& v){x=v.x;y=v.y;}
00046 
00047   /// Initialize from array of floats
00048   FXVec2f(const FXfloat v[]){x=v[0];y=v[1];}
00049 
00050   /// Initialize from components
00051   FXVec2f(FXfloat xx,FXfloat yy){x=xx;y=yy;}
00052 
00053   /// Return a non-const reference to the ith element
00054   FXfloat& operator[](FXint i){return (&x)[i];}
00055 
00056   /// Return a const reference to the ith element
00057   const FXfloat& operator[](FXint i) const {return (&x)[i];}
00058 
00059   /// Assignment
00060   FXVec2f& operator=(const FXVec2f& v){x=v.x;y=v.y;return *this;}
00061 
00062   /// Assignment from array of floats
00063   FXVec2f& operator=(const FXfloat v[]){x=v[0];y=v[1];return *this;}
00064 
00065   /// Set value from another vector
00066   FXVec2f& set(const FXVec2f& v){x=v.x;y=v.y;return *this;}
00067 
00068   /// Set value from array of floats
00069   FXVec2f& set(const FXfloat v[]){x=v[0];y=v[1];return *this;}
00070 
00071   /// Set value from components
00072   FXVec2f& set(FXfloat xx,FXfloat yy){x=xx;y=yy;return *this;}
00073 
00074   /// Assigning operators
00075   FXVec2f& operator*=(FXfloat n){x*=n;y*=n;return *this;}
00076   FXVec2f& operator/=(FXfloat n){x/=n;y/=n;return *this;}
00077   FXVec2f& operator+=(const FXVec2f& v){x+=v.x;y+=v.y;return *this;}
00078   FXVec2f& operator-=(const FXVec2f& v){x-=v.x;y-=v.y;return *this;}
00079 
00080   /// Conversions
00081   operator FXfloat*(){return &x;}
00082   operator const FXfloat*() const {return &x;}
00083 
00084   /// Unary
00085   FXVec2f operator+() const { return *this; }
00086   FXVec2f operator-() const { return FXVec2f(-x,-y); }
00087 
00088   /// Vector and vector
00089   FXVec2f operator+(const FXVec2f& v) const { return FXVec2f(x+v.x,y+v.y); }
00090   FXVec2f operator-(const FXVec2f& v) const { return FXVec2f(x-v.x,y-v.y); }
00091 
00092   /// Vector and matrix
00093   FXVec2f operator*(const FXMat3f& m) const;
00094 
00095   /// Scaling
00096   friend inline FXVec2f operator*(const FXVec2f& a,FXfloat n);
00097   friend inline FXVec2f operator*(FXfloat n,const FXVec2f& a);
00098   friend inline FXVec2f operator/(const FXVec2f& a,FXfloat n);
00099   friend inline FXVec2f operator/(FXfloat n,const FXVec2f& a);
00100 
00101   /// Dot product
00102   FXfloat operator*(const FXVec2f& v) const { return x*v.x+y*v.y; }
00103 
00104   /// Test if zero
00105   bool operator!() const { return x==0.0f && y==0.0f; }
00106 
00107   /// Equality tests
00108   bool operator==(const FXVec2f& v) const { return x==v.x && y==v.y; }
00109   bool operator!=(const FXVec2f& v) const { return x!=v.x || y!=v.y; }
00110 
00111   friend inline bool operator==(const FXVec2f& a,FXfloat n);
00112   friend inline bool operator!=(const FXVec2f& a,FXfloat n);
00113   friend inline bool operator==(FXfloat n,const FXVec2f& a);
00114   friend inline bool operator!=(FXfloat n,const FXVec2f& a);
00115 
00116   /// Inequality tests
00117   bool operator<(const FXVec2f& v) const { return x<v.x && y<v.y; }
00118   bool operator<=(const FXVec2f& v) const { return x<=v.x && y<=v.y; }
00119   bool operator>(const FXVec2f& v) const { return x>v.x && y>v.y; }
00120   bool operator>=(const FXVec2f& v) const { return x>=v.x && y>=v.y; }
00121 
00122   friend inline bool operator<(const FXVec2f& a,FXfloat n);
00123   friend inline bool operator<=(const FXVec2f& a,FXfloat n);
00124   friend inline bool operator>(const FXVec2f& a,FXfloat n);
00125   friend inline bool operator>=(const FXVec2f& a,FXfloat n);
00126 
00127   friend inline bool operator<(FXfloat n,const FXVec2f& a);
00128   friend inline bool operator<=(FXfloat n,const FXVec2f& a);
00129   friend inline bool operator>(FXfloat n,const FXVec2f& a);
00130   friend inline bool operator>=(FXfloat n,const FXVec2f& a);
00131 
00132   /// Length and square of length
00133   FXfloat length2() const { return x*x+y*y; }
00134   FXfloat length() const { return sqrtf(length2()); }
00135 
00136   /// Clamp values of vector between limits
00137   FXVec2f& clamp(FXfloat lo,FXfloat hi){x=FXCLAMP(lo,x,hi);y=FXCLAMP(lo,y,hi);return *this;}
00138 
00139   /// Lowest or highest components
00140   friend inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b);
00141   friend inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b);
00142 
00143   /// Normalize vector
00144   friend FXAPI FXVec2f normalize(const FXVec2f& v);
00145 
00146   /// Save vector to a stream
00147   friend FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
00148 
00149   /// Load vector from a stream
00150   friend FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
00151   };
00152 
00153 
00154 inline FXVec2f operator*(const FXVec2f& a,FXfloat n){return FXVec2f(a.x*n,a.y*n);}
00155 inline FXVec2f operator*(FXfloat n,const FXVec2f& a){return FXVec2f(n*a.x,n*a.y);}
00156 inline FXVec2f operator/(const FXVec2f& a,FXfloat n){return FXVec2f(a.x/n,a.y/n);}
00157 inline FXVec2f operator/(FXfloat n,const FXVec2f& a){return FXVec2f(n/a.x,n/a.y);}
00158 
00159 inline bool operator==(const FXVec2f& a,FXfloat n){return a.x==n && a.y==n;}
00160 inline bool operator!=(const FXVec2f& a,FXfloat n){return a.x!=n || a.y!=n;}
00161 inline bool operator==(FXfloat n,const FXVec2f& a){return n==a.x && n==a.y;}
00162 inline bool operator!=(FXfloat n,const FXVec2f& a){return n!=a.x || n!=a.y;}
00163 
00164 inline bool operator<(const FXVec2f& a,FXfloat n){return a.x<n && a.y<n;}
00165 inline bool operator<=(const FXVec2f& a,FXfloat n){return a.x<=n && a.y<=n;}
00166 inline bool operator>(const FXVec2f& a,FXfloat n){return a.x>n && a.y>n;}
00167 inline bool operator>=(const FXVec2f& a,FXfloat n){return a.x>=n && a.y>=n;}
00168 
00169 inline bool operator<(FXfloat n,const FXVec2f& a){return n<a.x && n<a.y;}
00170 inline bool operator<=(FXfloat n,const FXVec2f& a){return n<=a.x && n<=a.y;}
00171 inline bool operator>(FXfloat n,const FXVec2f& a){return n>a.x && n>a.y;}
00172 inline bool operator>=(FXfloat n,const FXVec2f& a){return n>=a.x && n>=a.y;}
00173 
00174 inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b){return FXVec2f(FXMIN(a.x,b.x),FXMIN(a.y,b.y));}
00175 inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b){return FXVec2f(FXMAX(a.x,b.x),FXMAX(a.y,b.y));}
00176 
00177 extern FXAPI FXVec2f normalize(const FXVec2f& v);
00178 
00179 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
00180 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
00181 
00182 }
00183 
00184 #endif

Copyright © 1997-2005 Jeroen van der Zijp