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

FXVec2d.h

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

Copyright © 1997-2010 Jeroen van der Zijp