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,2024 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  explicit FXVec2d(FXdouble d):x(d),y(d){}
39 
41  FXVec2d(FXdouble xx,FXdouble yy):x(xx),y(yy){}
42 
44  FXVec2d(const FXVec2d& v):x(v.x),y(v.y){}
45 
47  FXVec2d(const FXdouble v[]):x(v[0]),y(v[1]){}
48 
50  FXdouble& operator[](FXint i){return (&x)[i];}
51 
53  const FXdouble& operator[](FXint i) const {return (&x)[i];}
54 
56  FXVec2d& operator=(const FXVec2d& v){x=v.x;y=v.y;return *this;}
57 
59  FXVec2d& operator=(const FXdouble v[]){x=v[0];y=v[1];return *this;}
60 
62  FXVec2d& set(FXdouble dd){x=dd;y=dd;return *this;}
63 
65  FXVec2d& set(FXdouble xx,FXdouble yy){x=xx;y=yy;return *this;}
66 
68  FXVec2d& set(const FXVec2d& v){x=v.x;y=v.y;return *this;}
69 
71  FXVec2d& set(const FXdouble v[]){x=v[0];y=v[1];return *this;}
72 
74  FXVec2d& operator*=(FXdouble n){ return set(x*n,y*n); }
75  FXVec2d& operator/=(FXdouble n){ return set(x/n,y/n); }
76 
78  FXVec2d& operator+=(const FXVec2d& v){ return set(x+v.x,y+v.y); }
79  FXVec2d& operator-=(const FXVec2d& v){ return set(x-v.x,y-v.y); }
80  FXVec2d& operator%=(const FXVec2d& v){ return set(x*v.x,y*v.y); }
81  FXVec2d& operator/=(const FXVec2d& v){ return set(x/v.x,y/v.y); }
82 
84  operator FXdouble*(){return &x;}
85  operator const FXdouble*() const {return &x;}
86 
88  FXbool operator!() const { return x==0.0 && y==0.0;}
89 
91  FXVec2d operator+() const { return *this; }
92  FXVec2d operator-() const { return FXVec2d(-x,-y); }
93 
95  FXdouble length2() const { return y*y+x*x; }
96  FXdouble length() const { return Math::sqrt(length2()); }
97 
100  };
101 
102 
104 static inline FXdouble operator*(const FXVec2d& a,const FXVec2d& b){ return a.x*b.x+a.y*b.y; }
105 
107 static inline FXVec2d operator*(const FXVec2d& a,FXdouble n){return FXVec2d(a.x*n,a.y*n);}
108 static inline FXVec2d operator*(FXdouble n,const FXVec2d& a){return FXVec2d(n*a.x,n*a.y);}
109 static inline FXVec2d operator/(const FXVec2d& a,FXdouble n){return FXVec2d(a.x/n,a.y/n);}
110 static inline FXVec2d operator/(FXdouble n,const FXVec2d& a){return FXVec2d(n/a.x,n/a.y);}
111 
113 static inline FXVec2d operator+(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x+b.x,a.y+b.y); }
114 static inline FXVec2d operator-(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x-b.x,a.y-b.y); }
115 
117 static inline FXVec2d operator%(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x*b.x,a.y*b.y); }
118 static inline FXVec2d operator/(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x/b.x,a.y/b.y); }
119 
121 static inline FXbool operator==(const FXVec2d& a,FXdouble n){return a.x==n && a.y==n;}
122 static inline FXbool operator!=(const FXVec2d& a,FXdouble n){return a.x!=n || a.y!=n;}
123 static inline FXbool operator==(FXdouble n,const FXVec2d& a){return n==a.x && n==a.y;}
124 static inline FXbool operator!=(FXdouble n,const FXVec2d& a){return n!=a.x || n!=a.y;}
125 
127 static inline FXbool operator==(const FXVec2d& a,const FXVec2d& b){ return a.x==b.x && a.y==b.y; }
128 static inline FXbool operator!=(const FXVec2d& a,const FXVec2d& b){ return a.x!=b.x || a.y!=b.y; }
129 
131 static inline FXbool operator<(const FXVec2d& a,FXdouble n){return a.x<n && a.y<n;}
132 static inline FXbool operator<=(const FXVec2d& a,FXdouble n){return a.x<=n && a.y<=n;}
133 static inline FXbool operator>(const FXVec2d& a,FXdouble n){return a.x>n && a.y>n;}
134 static inline FXbool operator>=(const FXVec2d& a,FXdouble n){return a.x>=n && a.y>=n;}
135 
137 static inline FXbool operator<(FXdouble n,const FXVec2d& a){return n<a.x && n<a.y;}
138 static inline FXbool operator<=(FXdouble n,const FXVec2d& a){return n<=a.x && n<=a.y;}
139 static inline FXbool operator>(FXdouble n,const FXVec2d& a){return n>a.x && n>a.y;}
140 static inline FXbool operator>=(FXdouble n,const FXVec2d& a){return n>=a.x && n>=a.y;}
141 
143 static inline FXbool operator<(const FXVec2d& a,const FXVec2d& b){ return a.x<b.x && a.y<b.y; }
144 static inline FXbool operator<=(const FXVec2d& a,const FXVec2d& b){ return a.x<=b.x && a.y<=b.y; }
145 static inline FXbool operator>(const FXVec2d& a,const FXVec2d& b){ return a.x>b.x && a.y>b.y; }
146 static inline FXbool operator>=(const FXVec2d& a,const FXVec2d& b){ return a.x>=b.x && a.y>=b.y; }
147 
149 static inline FXVec2d lo(const FXVec2d& a,const FXVec2d& b){return FXVec2d(Math::fmin(a.x,b.x),Math::fmin(a.y,b.y));}
150 static inline FXVec2d lo(const FXVec2d& a,FXdouble n){return FXVec2d(Math::fmin(a.x,n),Math::fmin(a.y,n));}
151 static inline FXVec2d lo(FXdouble n,const FXVec2d& b){return FXVec2d(Math::fmin(n,b.x),Math::fmin(n,b.y));}
152 
154 static inline FXVec2d hi(const FXVec2d& a,const FXVec2d& b){return FXVec2d(Math::fmax(a.x,b.x),Math::fmax(a.y,b.y));}
155 static inline FXVec2d hi(const FXVec2d& a,FXdouble n){return FXVec2d(Math::fmax(a.x,n),Math::fmax(a.y,n));}
156 static inline FXVec2d hi(FXdouble n,const FXVec2d& b){return FXVec2d(Math::fmax(n,b.x),Math::fmax(n,b.y));}
157 
159 static inline FXVec2d clamp(FXdouble lower,const FXVec2d& x,FXdouble upper){return hi(lo(x,upper),lower);}
160 
162 static inline FXVec2d clamp(const FXVec2d& x,FXdouble limit){return hi(lo(x,limit),-limit);}
163 
165 static inline FXVec2d clamp(const FXVec2d& lower,const FXVec2d& x,const FXVec2d& upper){return hi(lo(x,upper),lower);}
166 
168 static inline FXVec2d clamp(const FXVec2d& x,const FXVec2d& limit){return hi(lo(x,limit),-limit);}
169 
171 static inline FXVec2d abs(const FXVec2d& a){return FXVec2d(Math::fabs(a.x),Math::fabs(a.y));}
172 
174 static inline FXdouble max(const FXVec2d& a){ return Math::fmax(a.x,a.y); }
175 
177 static inline FXdouble min(const FXVec2d& a){ return Math::fmin(a.x,a.y); }
178 
180 static inline FXVec2d lerp(const FXVec2d& u,const FXVec2d& v,FXdouble f){return (v-u)*f+u;}
181 
183 extern FXAPI FXVec2d normalize(const FXVec2d& v);
184 
186 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2d& v);
187 
189 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2d& v);
190 
191 }
192 
193 #endif
FXVec2d & operator*=(FXdouble n)
Assigning operators.
Definition: FXVec2d.h:74
FXVec2d(FXdouble d)
Initialize with constant.
Definition: FXVec2d.h:38
FXVec2d & operator=(const FXdouble v[])
Assignment from array of doubles.
Definition: FXVec2d.h:59
FXVec2d operator+() const
Unary.
Definition: FXVec2d.h:91
FXVec2d & operator+=(const FXVec2d &v)
Element-wise assigning operators.
Definition: FXVec2d.h:78
FXVec2d(const FXVec2d &v)
Initialize from another vector.
Definition: FXVec2d.h:44
FXVec2d(FXdouble xx, FXdouble yy)
Initialize from components.
Definition: FXVec2d.h:41
FXVec2d(const FXdouble v[])
Initialize from array of doubles.
Definition: FXVec2d.h:47
Definition: FX4Splitter.h:28
FXdouble length2() const
Length and square of length.
Definition: FXVec2d.h:95
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec2d.h:53
Double-precision 2-element vector.
Definition: FXVec2d.h:28
~FXVec2d()
Destructor.
Definition: FXVec2d.h:99
FXbool operator!() const
Test if zero.
Definition: FXVec2d.h:88
FXVec2d & operator=(const FXVec2d &v)
Assignment.
Definition: FXVec2d.h:56
FXVec2d()
Default constructor; value is not initialized.
Definition: FXVec2d.h:35
FXdouble & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec2d.h:50

Copyright © 1997-2022 Jeroen van der Zijp