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

FXVec2d.h
1 /********************************************************************************
2 * *
3 * 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 *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1994,2017 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXVEC2D_H
22 #define FXVEC2D_H
23 
24 namespace FX {
25 
26 
28 class FXAPI FXVec2d {
29 public:
30  FXdouble x;
31  FXdouble y;
32 public:
33 
35  FXVec2d(){}
36 
38  FXVec2d(const FXVec2d& v):x(v.x),y(v.y){}
39 
41  FXVec2d(const FXdouble v[]):x(v[0]),y(v[1]){}
42 
44  FXVec2d(FXdouble xx,FXdouble yy):x(xx),y(yy){}
45 
47  FXdouble& operator[](FXint i){return (&x)[i];}
48 
50  const FXdouble& operator[](FXint i) const {return (&x)[i];}
51 
53  FXVec2d& operator=(const FXVec2d& v){x=v.x;y=v.y;return *this;}
54 
56  FXVec2d& operator=(const FXdouble v[]){x=v[0];y=v[1];return *this;}
57 
59  FXVec2d& set(const FXVec2d& v){x=v.x;y=v.y;return *this;}
60 
62  FXVec2d& set(const FXdouble v[]){x=v[0];y=v[1];return *this;}
63 
65  FXVec2d& set(FXdouble xx,FXdouble yy){x=xx;y=yy;return *this;}
66 
68  FXVec2d& operator*=(FXdouble n){ return set(x*n,y*n); }
69  FXVec2d& operator/=(FXdouble n){ return set(x/n,y/n); }
70  FXVec2d& operator+=(const FXVec2d& v){ return set(x+v.x,y+v.y); }
71  FXVec2d& operator-=(const FXVec2d& v){ return set(x-v.x,y-v.y); }
72 
74  operator FXdouble*(){return &x;}
75  operator const FXdouble*() const {return &x;}
76 
78  FXbool operator!() const { return x==0.0 && y==0.0;}
79 
81  FXVec2d operator+() const { return *this; }
82  FXVec2d operator-() const { return FXVec2d(-x,-y); }
83 
85  FXdouble length2() const { return x*x+y*y; }
86  FXdouble length() const { return Math::sqrt(length2()); }
87 
89  FXVec2d& clamp(FXdouble lo,FXdouble hi){ return set(Math::fclamp(lo,x,hi),Math::fclamp(lo,y,hi)); }
90 
93  };
94 
95 
97 inline FXdouble operator*(const FXVec2d& a,const FXVec2d& b){ return a.x*b.x+a.y*b.y; }
98 
100 inline FXVec2d operator*(const FXVec2d& a,FXdouble n){return FXVec2d(a.x*n,a.y*n);}
101 inline FXVec2d operator*(FXdouble n,const FXVec2d& a){return FXVec2d(n*a.x,n*a.y);}
102 inline FXVec2d operator/(const FXVec2d& a,FXdouble n){return FXVec2d(a.x/n,a.y/n);}
103 inline FXVec2d operator/(FXdouble n,const FXVec2d& a){return FXVec2d(n/a.x,n/a.y);}
104 
106 inline FXVec2d operator+(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x+b.x,a.y+b.y); }
107 inline FXVec2d operator-(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x-b.x,a.y-b.y); }
108 
110 inline FXbool operator==(const FXVec2d& a,FXdouble n){return a.x==n && a.y==n;}
111 inline FXbool operator!=(const FXVec2d& a,FXdouble n){return a.x!=n || a.y!=n;}
112 inline FXbool operator==(FXdouble n,const FXVec2d& a){return n==a.x && n==a.y;}
113 inline FXbool operator!=(FXdouble n,const FXVec2d& a){return n!=a.x || n!=a.y;}
114 
116 inline FXbool operator==(const FXVec2d& a,const FXVec2d& b){ return a.x==b.x && a.y==b.y; }
117 inline FXbool operator!=(const FXVec2d& a,const FXVec2d& b){ return a.x!=b.x || a.y!=b.y; }
118 
120 inline FXbool operator<(const FXVec2d& a,FXdouble n){return a.x<n && a.y<n;}
121 inline FXbool operator<=(const FXVec2d& a,FXdouble n){return a.x<=n && a.y<=n;}
122 inline FXbool operator>(const FXVec2d& a,FXdouble n){return a.x>n && a.y>n;}
123 inline FXbool operator>=(const FXVec2d& a,FXdouble n){return a.x>=n && a.y>=n;}
124 
126 inline FXbool operator<(FXdouble n,const FXVec2d& a){return n<a.x && n<a.y;}
127 inline FXbool operator<=(FXdouble n,const FXVec2d& a){return n<=a.x && n<=a.y;}
128 inline FXbool operator>(FXdouble n,const FXVec2d& a){return n>a.x && n>a.y;}
129 inline FXbool operator>=(FXdouble n,const FXVec2d& a){return n>=a.x && n>=a.y;}
130 
132 inline FXbool operator<(const FXVec2d& a,const FXVec2d& b){ return a.x<b.x && a.y<b.y; }
133 inline FXbool operator<=(const FXVec2d& a,const FXVec2d& b){ return a.x<=b.x && a.y<=b.y; }
134 inline FXbool operator>(const FXVec2d& a,const FXVec2d& b){ return a.x>b.x && a.y>b.y; }
135 inline FXbool operator>=(const FXVec2d& a,const FXVec2d& b){ return a.x>=b.x && a.y>=b.y; }
136 
138 inline FXVec2d lo(const FXVec2d& a,const FXVec2d& b){return FXVec2d(Math::fmin(a.x,b.x),Math::fmin(a.y,b.y));}
139 inline FXVec2d hi(const FXVec2d& a,const FXVec2d& b){return FXVec2d(Math::fmax(a.x,b.x),Math::fmax(a.y,b.y));}
140 
142 extern FXAPI FXVec2d normalize(const FXVec2d& v);
143 
145 extern FXAPI FXVec2d lerp(const FXVec2d& u,const FXVec2d& v,FXdouble f);
146 
148 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2d& v);
149 
151 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2d& v);
152 
153 }
154 
155 #endif

Copyright © 1997-2016 Jeroen van der Zijp