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

FXVec2f.h
1 /********************************************************************************
2 * *
3 * 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 *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1994,2022 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 FXVEC2F_H
22 #define FXVEC2F_H
23 
24 namespace FX {
25 
26 
28 class FXAPI FXVec2f {
29 public:
30  FXfloat x;
31  FXfloat y;
32 public:
33 
35  FXVec2f(){}
36 
38  FXVec2f(const FXVec2f& v):x(v.x),y(v.y){}
39 
41  FXVec2f(const FXfloat v[]):x(v[0]),y(v[1]){}
42 
44  FXVec2f(FXfloat xx,FXfloat yy):x(xx),y(yy){}
45 
47  FXfloat& operator[](FXint i){return (&x)[i];}
48 
50  const FXfloat& operator[](FXint i) const {return (&x)[i];}
51 
53  FXVec2f& operator=(const FXVec2f& v){x=v.x;y=v.y;return *this;}
54 
56  FXVec2f& operator=(const FXfloat v[]){x=v[0];y=v[1];return *this;}
57 
59  FXVec2f& set(const FXVec2f& v){x=v.x;y=v.y;return *this;}
60 
62  FXVec2f& set(const FXfloat v[]){x=v[0];y=v[1];return *this;}
63 
65  FXVec2f& set(FXfloat xx,FXfloat yy){x=xx;y=yy;return *this;}
66 
68  FXVec2f& operator*=(FXfloat n){ return set(x*n,y*n); }
69  FXVec2f& operator/=(FXfloat n){ return set(x/n,y/n); }
70 
72  FXVec2f& operator+=(const FXVec2f& v){ return set(x+v.x,y+v.y); }
73  FXVec2f& operator-=(const FXVec2f& v){ return set(x-v.x,y-v.y); }
74  FXVec2f& operator%=(const FXVec2f& v){ return set(x*v.x,y*v.y); }
75  FXVec2f& operator/=(const FXVec2f& v){ return set(x/v.x,y/v.y); }
76 
78  operator FXfloat*(){return &x;}
79  operator const FXfloat*() const {return &x;}
80 
82  FXbool operator!() const { return x==0.0f && y==0.0f; }
83 
85  FXVec2f operator+() const { return *this; }
86  FXVec2f operator-() const { return FXVec2f(-x,-y); }
87 
89  FXfloat length2() const { return y*y+x*x; }
90  FXfloat length() const { return Math::sqrt(length2()); }
91 
94  };
95 
96 
98 inline FXfloat operator*(const FXVec2f& a,const FXVec2f& b){ return a.x*b.x+a.y*b.y; }
99 
101 inline FXVec2f operator*(const FXVec2f& a,FXfloat n){return FXVec2f(a.x*n,a.y*n);}
102 inline FXVec2f operator*(FXfloat n,const FXVec2f& a){return FXVec2f(n*a.x,n*a.y);}
103 inline FXVec2f operator/(const FXVec2f& a,FXfloat n){return FXVec2f(a.x/n,a.y/n);}
104 inline FXVec2f operator/(FXfloat n,const FXVec2f& a){return FXVec2f(n/a.x,n/a.y);}
105 
107 inline FXVec2f operator+(const FXVec2f& a,const FXVec2f& b){ return FXVec2f(a.x+b.x,a.y+b.y); }
108 inline FXVec2f operator-(const FXVec2f& a,const FXVec2f& b){ return FXVec2f(a.x-b.x,a.y-b.y); }
109 
111 inline FXVec2f operator%(const FXVec2f& a,const FXVec2f& b){ return FXVec2f(a.x*b.x,a.y*b.y); }
112 inline FXVec2f operator/(const FXVec2f& a,const FXVec2f& b){ return FXVec2f(a.x/b.x,a.y/b.y); }
113 
115 inline FXbool operator==(const FXVec2f& a,FXfloat n){return a.x==n && a.y==n;}
116 inline FXbool operator!=(const FXVec2f& a,FXfloat n){return a.x!=n || a.y!=n;}
117 inline FXbool operator==(FXfloat n,const FXVec2f& a){return n==a.x && n==a.y;}
118 inline FXbool operator!=(FXfloat n,const FXVec2f& a){return n!=a.x || n!=a.y;}
119 
121 inline FXbool operator==(const FXVec2f& a,const FXVec2f& b){ return a.x==b.x && a.y==b.y; }
122 inline FXbool operator!=(const FXVec2f& a,const FXVec2f& b){ return a.x!=b.x || a.y!=b.y; }
123 
125 inline FXbool operator<(const FXVec2f& a,FXfloat n){return a.x<n && a.y<n;}
126 inline FXbool operator<=(const FXVec2f& a,FXfloat n){return a.x<=n && a.y<=n;}
127 inline FXbool operator>(const FXVec2f& a,FXfloat n){return a.x>n && a.y>n;}
128 inline FXbool operator>=(const FXVec2f& a,FXfloat n){return a.x>=n && a.y>=n;}
129 
131 inline FXbool operator<(FXfloat n,const FXVec2f& a){return n<a.x && n<a.y;}
132 inline FXbool operator<=(FXfloat n,const FXVec2f& a){return n<=a.x && n<=a.y;}
133 inline FXbool operator>(FXfloat n,const FXVec2f& a){return n>a.x && n>a.y;}
134 inline FXbool operator>=(FXfloat n,const FXVec2f& a){return n>=a.x && n>=a.y;}
135 
137 inline FXbool operator<(const FXVec2f& a,const FXVec2f& b){ return a.x<b.x && a.y<b.y; }
138 inline FXbool operator<=(const FXVec2f& a,const FXVec2f& b){ return a.x<=b.x && a.y<=b.y; }
139 inline FXbool operator>(const FXVec2f& a,const FXVec2f& b){ return a.x>b.x && a.y>b.y; }
140 inline FXbool operator>=(const FXVec2f& a,const FXVec2f& b){ return a.x>=b.x && a.y>=b.y; }
141 
143 inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b){return FXVec2f(Math::fmin(a.x,b.x),Math::fmin(a.y,b.y));}
144 inline FXVec2f lo(const FXVec2f& a,FXfloat n){return FXVec2f(Math::fmin(a.x,n),Math::fmin(a.y,n));}
145 inline FXVec2f lo(FXfloat n,const FXVec2f& b){return FXVec2f(Math::fmin(n,b.x),Math::fmin(n,b.y));}
146 
148 inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b){return FXVec2f(Math::fmax(a.x,b.x),Math::fmax(a.y,b.y));}
149 inline FXVec2f hi(const FXVec2f& a,FXfloat n){return FXVec2f(Math::fmax(a.x,n),Math::fmax(a.y,n));}
150 inline FXVec2f hi(FXfloat n,const FXVec2f& b){return FXVec2f(Math::fmax(n,b.x),Math::fmax(n,b.y));}
151 
153 inline FXVec2f clamp(FXfloat lower,const FXVec2f& x,FXfloat upper){return hi(lo(x,upper),lower);}
154 
156 inline FXVec2f clamp(const FXVec2f& lower,const FXVec2f& x,const FXVec2f& upper){return hi(lo(x,upper),lower);}
157 
159 inline FXVec2f abs(const FXVec2f& a){return FXVec2f(Math::fabs(a.x),Math::fabs(a.y));}
160 
162 inline FXfloat max(const FXVec2f& a){ return Math::fmax(a.x,a.y); }
163 
165 inline FXfloat min(const FXVec2f& a){ return Math::fmin(a.x,a.y); }
166 
168 inline FXVec2f lerp(const FXVec2f& u,const FXVec2f& v,FXfloat f){return (v-u)*f+u;}
169 
171 extern FXAPI FXVec2f normalize(const FXVec2f& v);
172 
174 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
175 
177 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
178 
179 }
180 
181 #endif
const FXfloat & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec2f.h:50
Single-precision 2-element vector.
Definition: FXVec2f.h:28
FXfloat length2() const
Length and square of length.
Definition: FXVec2f.h:89
FXVec2f & operator*=(FXfloat n)
Assigning operators.
Definition: FXVec2f.h:68
FXVec2f(const FXVec2f &v)
Initialize from another vector.
Definition: FXVec2f.h:38
FXVec2f(FXfloat xx, FXfloat yy)
Initialize from components.
Definition: FXVec2f.h:44
FXbool operator!() const
Test if zero.
Definition: FXVec2f.h:82
FXVec2f(const FXfloat v[])
Initialize from array of floats.
Definition: FXVec2f.h:41
FXVec2f & operator=(const FXVec2f &v)
Assignment.
Definition: FXVec2f.h:53
FXfloat & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec2f.h:47
Definition: FX4Splitter.h:28
~FXVec2f()
Destructor.
Definition: FXVec2f.h:93
FXVec2f()
Default constructor; value is not initialized.
Definition: FXVec2f.h:35
FXVec2f & operator+=(const FXVec2f &v)
Element-wise assigning operators.
Definition: FXVec2f.h:72
FXVec2f & operator=(const FXfloat v[])
Assignment from array of floats.
Definition: FXVec2f.h:56
FXVec2f operator+() const
Unary.
Definition: FXVec2f.h:85

Copyright © 1997-2022 Jeroen van der Zijp