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

FXVec2f.h

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

Copyright © 1997-2004 Jeroen van der Zijp