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,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 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  FXVec2f& operator+=(const FXVec2f& v){ return set(x+v.x,y+v.y); }
71  FXVec2f& operator-=(const FXVec2f& v){ return set(x-v.x,y-v.y); }
72 
74  operator FXfloat*(){return &x;}
75  operator const FXfloat*() const {return &x;}
76 
78  FXbool operator!() const { return x==0.0f && y==0.0f; }
79 
81  FXVec2f operator+() const { return *this; }
82  FXVec2f operator-() const { return FXVec2f(-x,-y); }
83 
85  FXfloat length2() const { return x*x+y*y; }
86  FXfloat length() const { return Math::sqrt(length2()); }
87 
89  FXVec2f& clamp(FXfloat lo,FXfloat hi){ return set(Math::fclamp(lo,x,hi),Math::fclamp(lo,y,hi)); }
90 
93  };
94 
95 
97 inline FXfloat operator*(const FXVec2f& a,const FXVec2f& b){ return a.x*b.x+a.y*b.y; }
98 
100 inline FXVec2f operator*(const FXVec2f& a,FXfloat n){return FXVec2f(a.x*n,a.y*n);}
101 inline FXVec2f operator*(FXfloat n,const FXVec2f& a){return FXVec2f(n*a.x,n*a.y);}
102 inline FXVec2f operator/(const FXVec2f& a,FXfloat n){return FXVec2f(a.x/n,a.y/n);}
103 inline FXVec2f operator/(FXfloat n,const FXVec2f& a){return FXVec2f(n/a.x,n/a.y);}
104 
106 inline FXVec2f operator+(const FXVec2f& a,const FXVec2f& b){ return FXVec2f(a.x+b.x,a.y+b.y); }
107 inline FXVec2f operator-(const FXVec2f& a,const FXVec2f& b){ return FXVec2f(a.x-b.x,a.y-b.y); }
108 
110 inline FXbool operator==(const FXVec2f& a,FXfloat n){return a.x==n && a.y==n;}
111 inline FXbool operator!=(const FXVec2f& a,FXfloat n){return a.x!=n || a.y!=n;}
112 inline FXbool operator==(FXfloat n,const FXVec2f& a){return n==a.x && n==a.y;}
113 inline FXbool operator!=(FXfloat n,const FXVec2f& a){return n!=a.x || n!=a.y;}
114 
116 inline FXbool operator==(const FXVec2f& a,const FXVec2f& b){ return a.x==b.x && a.y==b.y; }
117 inline FXbool operator!=(const FXVec2f& a,const FXVec2f& b){ return a.x!=b.x || a.y!=b.y; }
118 
120 inline FXbool operator<(const FXVec2f& a,FXfloat n){return a.x<n && a.y<n;}
121 inline FXbool operator<=(const FXVec2f& a,FXfloat n){return a.x<=n && a.y<=n;}
122 inline FXbool operator>(const FXVec2f& a,FXfloat n){return a.x>n && a.y>n;}
123 inline FXbool operator>=(const FXVec2f& a,FXfloat n){return a.x>=n && a.y>=n;}
124 
126 inline FXbool operator<(FXfloat n,const FXVec2f& a){return n<a.x && n<a.y;}
127 inline FXbool operator<=(FXfloat n,const FXVec2f& a){return n<=a.x && n<=a.y;}
128 inline FXbool operator>(FXfloat n,const FXVec2f& a){return n>a.x && n>a.y;}
129 inline FXbool operator>=(FXfloat n,const FXVec2f& a){return n>=a.x && n>=a.y;}
130 
132 inline FXbool operator<(const FXVec2f& a,const FXVec2f& b){ return a.x<b.x && a.y<b.y; }
133 inline FXbool operator<=(const FXVec2f& a,const FXVec2f& b){ return a.x<=b.x && a.y<=b.y; }
134 inline FXbool operator>(const FXVec2f& a,const FXVec2f& b){ return a.x>b.x && a.y>b.y; }
135 inline FXbool operator>=(const FXVec2f& a,const FXVec2f& b){ return a.x>=b.x && a.y>=b.y; }
136 
138 inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b){return FXVec2f(Math::fmin(a.x,b.x),Math::fmin(a.y,b.y));}
139 inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b){return FXVec2f(Math::fmax(a.x,b.x),Math::fmax(a.y,b.y));}
140 
142 extern FXAPI FXVec2f normalize(const FXVec2f& v);
143 
145 extern FXAPI FXVec2f lerp(const FXVec2f& u,const FXVec2f& v,FXfloat f);
146 
148 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
149 
151 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
152 
153 }
154 
155 #endif

Copyright © 1997-2016 Jeroen van der Zijp