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,2010 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 #ifndef FXVEC2F_H
00022 #define FXVEC2F_H
00023 
00024 
00025 namespace FX {
00026 
00027 
00028 /// Single-precision 2-element vector
00029 class FXAPI FXVec2f {
00030 public:
00031   FXfloat x;
00032   FXfloat y;
00033 public:
00034 
00035   /// Default constructor; value is not initialized
00036   FXVec2f(){}
00037 
00038   /// Initialize from another vector
00039   FXVec2f(const FXVec2f& v):x(v.x),y(v.y){}
00040 
00041   /// Initialize from array of floats
00042   FXVec2f(const FXfloat v[]):x(v[0]),y(v[1]){}
00043 
00044   /// Initialize from components
00045   FXVec2f(FXfloat xx,FXfloat yy):x(xx),y(yy){}
00046 
00047   /// Return a non-const reference to the ith element
00048   FXfloat& operator[](FXint i){return (&x)[i];}
00049 
00050   /// Return a const reference to the ith element
00051   const FXfloat& operator[](FXint i) const {return (&x)[i];}
00052 
00053   /// Assignment
00054   FXVec2f& operator=(const FXVec2f& v){x=v.x;y=v.y;return *this;}
00055 
00056   /// Assignment from array of floats
00057   FXVec2f& operator=(const FXfloat v[]){x=v[0];y=v[1];return *this;}
00058 
00059   /// Set value from another vector
00060   FXVec2f& set(const FXVec2f& v){x=v.x;y=v.y;return *this;}
00061 
00062   /// Set value from array of floats
00063   FXVec2f& set(const FXfloat v[]){x=v[0];y=v[1];return *this;}
00064 
00065   /// Set value from components
00066   FXVec2f& set(FXfloat xx,FXfloat yy){x=xx;y=yy;return *this;}
00067 
00068   /// Assigning operators
00069   FXVec2f& operator*=(FXfloat n){ return set(x*n,y*n); }
00070   FXVec2f& operator/=(FXfloat n){ return set(x/n,y/n); }
00071   FXVec2f& operator+=(const FXVec2f& v){ return set(x+v.x,y+v.y); }
00072   FXVec2f& operator-=(const FXVec2f& v){ return set(x-v.x,y-v.y); }
00073 
00074   /// Conversions
00075   operator FXfloat*(){return &x;}
00076   operator const FXfloat*() const {return &x;}
00077 
00078   /// Test if zero
00079   FXbool operator!() const { return x==0.0f && y==0.0f; }
00080 
00081   /// Unary
00082   FXVec2f operator+() const { return *this; }
00083   FXVec2f operator-() const { return FXVec2f(-x,-y); }
00084 
00085   /// Length and square of length
00086   FXfloat length2() const { return x*x+y*y; }
00087   FXfloat length() const { return sqrtf(length2()); }
00088 
00089   /// Clamp values of vector between limits
00090   FXVec2f& clamp(FXfloat lo,FXfloat hi){ return set(FXCLAMP(lo,x,hi),FXCLAMP(lo,y,hi)); }
00091   };
00092 
00093 
00094 /// Dot product
00095 inline FXfloat operator*(const FXVec2f& a,const FXVec2f& b){ return a.x*b.x+a.y*b.y; }
00096 
00097 /// Scaling
00098 inline FXVec2f operator*(const FXVec2f& a,FXfloat n){return FXVec2f(a.x*n,a.y*n);}
00099 inline FXVec2f operator*(FXfloat n,const FXVec2f& a){return FXVec2f(n*a.x,n*a.y);}
00100 inline FXVec2f operator/(const FXVec2f& a,FXfloat n){return FXVec2f(a.x/n,a.y/n);}
00101 inline FXVec2f operator/(FXfloat n,const FXVec2f& a){return FXVec2f(n/a.x,n/a.y);}
00102 
00103 /// Vector and vector addition
00104 inline FXVec2f operator+(const FXVec2f& a,const FXVec2f& b){ return FXVec2f(a.x+b.x,a.y+b.y); }
00105 inline FXVec2f operator-(const FXVec2f& a,const FXVec2f& b){ return FXVec2f(a.x-b.x,a.y-b.y); }
00106 
00107 /// Equality tests
00108 inline FXbool operator==(const FXVec2f& a,FXfloat n){return a.x==n && a.y==n;}
00109 inline FXbool operator!=(const FXVec2f& a,FXfloat n){return a.x!=n || a.y!=n;}
00110 inline FXbool operator==(FXfloat n,const FXVec2f& a){return n==a.x && n==a.y;}
00111 inline FXbool operator!=(FXfloat n,const FXVec2f& a){return n!=a.x || n!=a.y;}
00112 
00113 /// Equality tests
00114 inline FXbool operator==(const FXVec2f& a,const FXVec2f& b){ return a.x==b.x && a.y==b.y; }
00115 inline FXbool operator!=(const FXVec2f& a,const FXVec2f& b){ return a.x!=b.x || a.y!=b.y; }
00116 
00117 /// Inequality tests
00118 inline FXbool operator<(const FXVec2f& a,FXfloat n){return a.x<n && a.y<n;}
00119 inline FXbool operator<=(const FXVec2f& a,FXfloat n){return a.x<=n && a.y<=n;}
00120 inline FXbool operator>(const FXVec2f& a,FXfloat n){return a.x>n && a.y>n;}
00121 inline FXbool operator>=(const FXVec2f& a,FXfloat n){return a.x>=n && a.y>=n;}
00122 
00123 /// Inequality tests
00124 inline FXbool operator<(FXfloat n,const FXVec2f& a){return n<a.x && n<a.y;}
00125 inline FXbool operator<=(FXfloat n,const FXVec2f& a){return n<=a.x && n<=a.y;}
00126 inline FXbool operator>(FXfloat n,const FXVec2f& a){return n>a.x && n>a.y;}
00127 inline FXbool operator>=(FXfloat n,const FXVec2f& a){return n>=a.x && n>=a.y;}
00128 
00129 /// Inequality tests
00130 inline FXbool operator<(const FXVec2f& a,const FXVec2f& b){ return a.x<b.x && a.y<b.y; }
00131 inline FXbool operator<=(const FXVec2f& a,const FXVec2f& b){ return a.x<=b.x && a.y<=b.y; }
00132 inline FXbool operator>(const FXVec2f& a,const FXVec2f& b){ return a.x>b.x && a.y>b.y; }
00133 inline FXbool operator>=(const FXVec2f& a,const FXVec2f& b){ return a.x>=b.x && a.y>=b.y; }
00134 
00135 /// Lowest or highest components
00136 inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b){return FXVec2f(FXMIN(a.x,b.x),FXMIN(a.y,b.y));}
00137 inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b){return FXVec2f(FXMAX(a.x,b.x),FXMAX(a.y,b.y));}
00138 
00139 /// Normalize vector
00140 extern FXAPI FXVec2f normalize(const FXVec2f& v);
00141 extern FXAPI FXVec2f fastnormalize(const FXVec2f& v);
00142 
00143 /// Save vector to a stream
00144 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
00145 
00146 /// Load vector from a stream
00147 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
00148 
00149 }
00150 
00151 #endif

Copyright © 1997-2010 Jeroen van der Zijp