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

FXVec2d.h

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

Copyright © 1997-2005 Jeroen van der Zijp