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  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 
72  FXVec2d& operator+=(const FXVec2d& v){ return set(x+v.x,y+v.y); }
73  FXVec2d& operator-=(const FXVec2d& v){ return set(x-v.x,y-v.y); }
74  FXVec2d& operator%=(const FXVec2d& v){ return set(x*v.x,y*v.y); }
75  FXVec2d& operator/=(const FXVec2d& v){ return set(x/v.x,y/v.y); }
76 
78  operator FXdouble*(){return &x;}
79  operator const FXdouble*() const {return &x;}
80 
82  FXbool operator!() const { return x==0.0 && y==0.0;}
83 
85  FXVec2d operator+() const { return *this; }
86  FXVec2d operator-() const { return FXVec2d(-x,-y); }
87 
89  FXdouble length2() const { return y*y+x*x; }
90  FXdouble length() const { return Math::sqrt(length2()); }
91 
94  };
95 
96 
98 static inline FXdouble operator*(const FXVec2d& a,const FXVec2d& b){ return a.x*b.x+a.y*b.y; }
99 
101 static inline FXVec2d operator*(const FXVec2d& a,FXdouble n){return FXVec2d(a.x*n,a.y*n);}
102 static inline FXVec2d operator*(FXdouble n,const FXVec2d& a){return FXVec2d(n*a.x,n*a.y);}
103 static inline FXVec2d operator/(const FXVec2d& a,FXdouble n){return FXVec2d(a.x/n,a.y/n);}
104 static inline FXVec2d operator/(FXdouble n,const FXVec2d& a){return FXVec2d(n/a.x,n/a.y);}
105 
107 static inline FXVec2d operator+(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x+b.x,a.y+b.y); }
108 static inline FXVec2d operator-(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x-b.x,a.y-b.y); }
109 
111 static inline FXVec2d operator%(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x*b.x,a.y*b.y); }
112 static inline FXVec2d operator/(const FXVec2d& a,const FXVec2d& b){ return FXVec2d(a.x/b.x,a.y/b.y); }
113 
115 static inline FXbool operator==(const FXVec2d& a,FXdouble n){return a.x==n && a.y==n;}
116 static inline FXbool operator!=(const FXVec2d& a,FXdouble n){return a.x!=n || a.y!=n;}
117 static inline FXbool operator==(FXdouble n,const FXVec2d& a){return n==a.x && n==a.y;}
118 static inline FXbool operator!=(FXdouble n,const FXVec2d& a){return n!=a.x || n!=a.y;}
119 
121 static inline FXbool operator==(const FXVec2d& a,const FXVec2d& b){ return a.x==b.x && a.y==b.y; }
122 static inline FXbool operator!=(const FXVec2d& a,const FXVec2d& b){ return a.x!=b.x || a.y!=b.y; }
123 
125 static inline FXbool operator<(const FXVec2d& a,FXdouble n){return a.x<n && a.y<n;}
126 static inline FXbool operator<=(const FXVec2d& a,FXdouble n){return a.x<=n && a.y<=n;}
127 static inline FXbool operator>(const FXVec2d& a,FXdouble n){return a.x>n && a.y>n;}
128 static inline FXbool operator>=(const FXVec2d& a,FXdouble n){return a.x>=n && a.y>=n;}
129 
131 static inline FXbool operator<(FXdouble n,const FXVec2d& a){return n<a.x && n<a.y;}
132 static inline FXbool operator<=(FXdouble n,const FXVec2d& a){return n<=a.x && n<=a.y;}
133 static inline FXbool operator>(FXdouble n,const FXVec2d& a){return n>a.x && n>a.y;}
134 static inline FXbool operator>=(FXdouble n,const FXVec2d& a){return n>=a.x && n>=a.y;}
135 
137 static inline FXbool operator<(const FXVec2d& a,const FXVec2d& b){ return a.x<b.x && a.y<b.y; }
138 static inline FXbool operator<=(const FXVec2d& a,const FXVec2d& b){ return a.x<=b.x && a.y<=b.y; }
139 static inline FXbool operator>(const FXVec2d& a,const FXVec2d& b){ return a.x>b.x && a.y>b.y; }
140 static inline FXbool operator>=(const FXVec2d& a,const FXVec2d& b){ return a.x>=b.x && a.y>=b.y; }
141 
143 static inline FXVec2d lo(const FXVec2d& a,const FXVec2d& b){return FXVec2d(Math::fmin(a.x,b.x),Math::fmin(a.y,b.y));}
144 static inline FXVec2d lo(const FXVec2d& a,FXdouble n){return FXVec2d(Math::fmin(a.x,n),Math::fmin(a.y,n));}
145 static inline FXVec2d lo(FXdouble n,const FXVec2d& b){return FXVec2d(Math::fmin(n,b.x),Math::fmin(n,b.y));}
146 
148 static inline FXVec2d hi(const FXVec2d& a,const FXVec2d& b){return FXVec2d(Math::fmax(a.x,b.x),Math::fmax(a.y,b.y));}
149 static inline FXVec2d hi(const FXVec2d& a,FXdouble n){return FXVec2d(Math::fmax(a.x,n),Math::fmax(a.y,n));}
150 static inline FXVec2d hi(FXdouble n,const FXVec2d& b){return FXVec2d(Math::fmax(n,b.x),Math::fmax(n,b.y));}
151 
153 static inline FXVec2d clamp(FXdouble lower,const FXVec2d& x,FXdouble upper){return hi(lo(x,upper),lower);}
154 
156 static inline FXVec2d clamp(const FXVec2d& x,FXdouble limit){return hi(lo(x,limit),-limit);}
157 
159 static inline FXVec2d clamp(const FXVec2d& lower,const FXVec2d& x,const FXVec2d& upper){return hi(lo(x,upper),lower);}
160 
162 static inline FXVec2d clamp(const FXVec2d& x,const FXVec2d& limit){return hi(lo(x,limit),-limit);}
163 
165 static inline FXVec2d abs(const FXVec2d& a){return FXVec2d(Math::fabs(a.x),Math::fabs(a.y));}
166 
168 static inline FXdouble max(const FXVec2d& a){ return Math::fmax(a.x,a.y); }
169 
171 static inline FXdouble min(const FXVec2d& a){ return Math::fmin(a.x,a.y); }
172 
174 static inline FXVec2d lerp(const FXVec2d& u,const FXVec2d& v,FXdouble f){return (v-u)*f+u;}
175 
177 extern FXAPI FXVec2d normalize(const FXVec2d& v);
178 
180 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2d& v);
181 
183 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2d& v);
184 
185 }
186 
187 #endif
FXVec2d & operator*=(FXdouble n)
Assigning operators.
Definition: FXVec2d.h:68
FXVec2d & operator=(const FXdouble v[])
Assignment from array of doubles.
Definition: FXVec2d.h:56
FXVec2d operator+() const
Unary.
Definition: FXVec2d.h:85
FXVec2d & operator+=(const FXVec2d &v)
Element-wise assigning operators.
Definition: FXVec2d.h:72
FXVec2d(const FXVec2d &v)
Initialize from another vector.
Definition: FXVec2d.h:38
FXVec2d(FXdouble xx, FXdouble yy)
Initialize from components.
Definition: FXVec2d.h:44
FXVec2d(const FXdouble v[])
Initialize from array of doubles.
Definition: FXVec2d.h:41
Definition: FX4Splitter.h:28
FXdouble length2() const
Length and square of length.
Definition: FXVec2d.h:89
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec2d.h:50
Double-precision 2-element vector.
Definition: FXVec2d.h:28
~FXVec2d()
Destructor.
Definition: FXVec2d.h:93
FXbool operator!() const
Test if zero.
Definition: FXVec2d.h:82
FXVec2d & operator=(const FXVec2d &v)
Assignment.
Definition: FXVec2d.h:53
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:47

Copyright © 1997-2022 Jeroen van der Zijp