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

FXVec2f.h
Go to the documentation of this file.
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,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (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 GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXVec2f.h,v 1.21 2006/01/22 17:58:12 fox Exp $ *
23 ********************************************************************************/
24 #ifndef FXVEC2F_H
25 #define FXVEC2F_H
26 
27 
28 namespace FX {
29 
30 
31 class FXMat3f;
32 
33 
34 /// Single-precision 2-element vector
35 class FXAPI FXVec2f {
36 public:
39 public:
40 
41  /// Default constructor
42  FXVec2f(){}
43 
44  /// Initialize from another vector
45  FXVec2f(const FXVec2f& v){x=v.x;y=v.y;}
46 
47  /// Initialize from array of floats
48  FXVec2f(const FXfloat v[]){x=v[0];y=v[1];}
49 
50  /// Initialize from components
51  FXVec2f(FXfloat xx,FXfloat yy){x=xx;y=yy;}
52 
53  /// Return a non-const reference to the ith element
54  FXfloat& operator[](FXint i){return (&x)[i];}
55 
56  /// Return a const reference to the ith element
57  const FXfloat& operator[](FXint i) const {return (&x)[i];}
58 
59  /// Assignment
60  FXVec2f& operator=(const FXVec2f& v){x=v.x;y=v.y;return *this;}
61 
62  /// Assignment from array of floats
63  FXVec2f& operator=(const FXfloat v[]){x=v[0];y=v[1];return *this;}
64 
65  /// Set value from another vector
66  FXVec2f& set(const FXVec2f& v){x=v.x;y=v.y;return *this;}
67 
68  /// Set value from array of floats
69  FXVec2f& set(const FXfloat v[]){x=v[0];y=v[1];return *this;}
70 
71  /// Set value from components
72  FXVec2f& set(FXfloat xx,FXfloat yy){x=xx;y=yy;return *this;}
73 
74  /// Assigning operators
75  FXVec2f& operator*=(FXfloat n){x*=n;y*=n;return *this;}
76  FXVec2f& operator/=(FXfloat n){x/=n;y/=n;return *this;}
77  FXVec2f& operator+=(const FXVec2f& v){x+=v.x;y+=v.y;return *this;}
78  FXVec2f& operator-=(const FXVec2f& v){x-=v.x;y-=v.y;return *this;}
79 
80  /// Conversions
81  operator FXfloat*(){return &x;}
82  operator const FXfloat*() const {return &x;}
83 
84  /// Unary
85  FXVec2f operator+() const { return *this; }
86  FXVec2f operator-() const { return FXVec2f(-x,-y); }
87 
88  /// Vector and vector
89  FXVec2f operator+(const FXVec2f& v) const { return FXVec2f(x+v.x,y+v.y); }
90  FXVec2f operator-(const FXVec2f& v) const { return FXVec2f(x-v.x,y-v.y); }
91 
92  /// Vector and matrix
93  FXVec2f operator*(const FXMat3f& m) const;
94 
95  /// Scaling
96  friend inline FXVec2f operator*(const FXVec2f& a,FXfloat n);
97  friend inline FXVec2f operator*(FXfloat n,const FXVec2f& a);
98  friend inline FXVec2f operator/(const FXVec2f& a,FXfloat n);
99  friend inline FXVec2f operator/(FXfloat n,const FXVec2f& a);
100 
101  /// Dot product
102  FXfloat operator*(const FXVec2f& v) const { return x*v.x+y*v.y; }
103 
104  /// Test if zero
105  bool operator!() const { return x==0.0f && y==0.0f; }
106 
107  /// Equality tests
108  bool operator==(const FXVec2f& v) const { return x==v.x && y==v.y; }
109  bool operator!=(const FXVec2f& v) const { return x!=v.x || y!=v.y; }
110 
111  friend inline bool operator==(const FXVec2f& a,FXfloat n);
112  friend inline bool operator!=(const FXVec2f& a,FXfloat n);
113  friend inline bool operator==(FXfloat n,const FXVec2f& a);
114  friend inline bool operator!=(FXfloat n,const FXVec2f& a);
115 
116  /// Inequality tests
117  bool operator<(const FXVec2f& v) const { return x<v.x && y<v.y; }
118  bool operator<=(const FXVec2f& v) const { return x<=v.x && y<=v.y; }
119  bool operator>(const FXVec2f& v) const { return x>v.x && y>v.y; }
120  bool operator>=(const FXVec2f& v) const { return x>=v.x && y>=v.y; }
121 
122  friend inline bool operator<(const FXVec2f& a,FXfloat n);
123  friend inline bool operator<=(const FXVec2f& a,FXfloat n);
124  friend inline bool operator>(const FXVec2f& a,FXfloat n);
125  friend inline bool operator>=(const FXVec2f& a,FXfloat n);
126 
127  friend inline bool operator<(FXfloat n,const FXVec2f& a);
128  friend inline bool operator<=(FXfloat n,const FXVec2f& a);
129  friend inline bool operator>(FXfloat n,const FXVec2f& a);
130  friend inline bool operator>=(FXfloat n,const FXVec2f& a);
131 
132  /// Length and square of length
133  FXfloat length2() const { return x*x+y*y; }
134  FXfloat length() const { return sqrtf(length2()); }
135 
136  /// Clamp values of vector between limits
137  FXVec2f& clamp(FXfloat lo,FXfloat hi){x=FXCLAMP(lo,x,hi);y=FXCLAMP(lo,y,hi);return *this;}
138 
139  /// Lowest or highest components
140  friend inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b);
141  friend inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b);
142 
143  /// Normalize vector
144  friend FXAPI FXVec2f normalize(const FXVec2f& v);
145 
146  /// Save vector to a stream
147  friend FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
148 
149  /// Load vector from a stream
150  friend FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
151  };
152 
153 
154 inline FXVec2f operator*(const FXVec2f& a,FXfloat n){return FXVec2f(a.x*n,a.y*n);}
155 inline FXVec2f operator*(FXfloat n,const FXVec2f& a){return FXVec2f(n*a.x,n*a.y);}
156 inline FXVec2f operator/(const FXVec2f& a,FXfloat n){return FXVec2f(a.x/n,a.y/n);}
157 inline FXVec2f operator/(FXfloat n,const FXVec2f& a){return FXVec2f(n/a.x,n/a.y);}
158 
159 inline bool operator==(const FXVec2f& a,FXfloat n){return a.x==n && a.y==n;}
160 inline bool operator!=(const FXVec2f& a,FXfloat n){return a.x!=n || a.y!=n;}
161 inline bool operator==(FXfloat n,const FXVec2f& a){return n==a.x && n==a.y;}
162 inline bool operator!=(FXfloat n,const FXVec2f& a){return n!=a.x || n!=a.y;}
163 
164 inline bool operator<(const FXVec2f& a,FXfloat n){return a.x<n && a.y<n;}
165 inline bool operator<=(const FXVec2f& a,FXfloat n){return a.x<=n && a.y<=n;}
166 inline bool operator>(const FXVec2f& a,FXfloat n){return a.x>n && a.y>n;}
167 inline bool operator>=(const FXVec2f& a,FXfloat n){return a.x>=n && a.y>=n;}
168 
169 inline bool operator<(FXfloat n,const FXVec2f& a){return n<a.x && n<a.y;}
170 inline bool operator<=(FXfloat n,const FXVec2f& a){return n<=a.x && n<=a.y;}
171 inline bool operator>(FXfloat n,const FXVec2f& a){return n>a.x && n>a.y;}
172 inline bool operator>=(FXfloat n,const FXVec2f& a){return n>=a.x && n>=a.y;}
173 
174 inline FXVec2f lo(const FXVec2f& a,const FXVec2f& b){return FXVec2f(FXMIN(a.x,b.x),FXMIN(a.y,b.y));}
175 inline FXVec2f hi(const FXVec2f& a,const FXVec2f& b){return FXVec2f(FXMAX(a.x,b.x),FXMAX(a.y,b.y));}
176 
177 extern FXAPI FXVec2f normalize(const FXVec2f& v);
178 
179 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec2f& v);
180 extern FXAPI FXStream& operator>>(FXStream& store,FXVec2f& v);
181 
182 }
183 
184 #endif
FXVec2d lo(const FXVec2d &a, const FXVec2d &b)
Definition: FXVec2d.h:174
FXMat3d operator/(const FXMat3d &a, FXdouble x)
FXVec2f & operator-=(const FXVec2f &v)
Definition: FXVec2f.h:78
FXfloat length2() const
Length and square of length.
Definition: FXVec2f.h:133
FXStream & operator>>(FXStream &store, FXDate &d)
Single-precision 2-element vector.
Definition: FXVec2f.h:35
FXfloat operator*(const FXVec2f &v) const
Dot product.
Definition: FXVec2f.h:102
#define FXCLAMP(lo, x, hi)
Clamp value x to range [lo..hi].
Definition: fxdefs.h:509
FXVec2f & operator*=(FXfloat n)
Assigning operators.
Definition: FXVec2f.h:75
FXVec2f(const FXVec2f &v)
Initialize from another vector.
Definition: FXVec2f.h:45
FXVec2f & operator/=(FXfloat n)
Definition: FXVec2f.h:76
FXfloat length() const
Definition: FXVec2f.h:134
#define FXAPI
Definition: fxdefs.h:122
bool operator==(const FXVec2f &v) const
Equality tests.
Definition: FXVec2f.h:108
Single-precision 3x3 matrix.
Definition: FXMat3f.h:35
bool operator>(const FXVec2f &v) const
Definition: FXVec2f.h:119
FXVec2f & set(const FXfloat v[])
Set value from array of floats.
Definition: FXVec2f.h:69
FXVec2f(FXfloat xx, FXfloat yy)
Initialize from components.
Definition: FXVec2f.h:51
bool operator>=(const FXVec2f &v) const
Definition: FXVec2f.h:120
bool operator<=(const FXString &s1, const FXString &s2)
bool operator>=(const FXString &s1, const FXString &s2)
FXVec2f(const FXfloat v[])
Initialize from array of floats.
Definition: FXVec2f.h:48
FXVec2f & operator=(const FXVec2f &v)
Assignment.
Definition: FXVec2f.h:60
FXfloat & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec2f.h:54
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:99
FXString normalize(const FXString &s)
#define FXMAX(a, b)
Return the maximum of a or b.
Definition: fxdefs.h:488
bool operator<=(const FXVec2f &v) const
Definition: FXVec2f.h:118
int FXint
Definition: fxdefs.h:397
FXVec2f operator-(const FXVec2f &v) const
Definition: FXVec2f.h:90
FXVec2f()
Default constructor.
Definition: FXVec2f.h:42
FXVec2d hi(const FXVec2d &a, const FXVec2d &b)
Definition: FXVec2d.h:175
FXVec2f & operator+=(const FXVec2f &v)
Definition: FXVec2f.h:77
FXVec2f & operator=(const FXfloat v[])
Assignment from array of floats.
Definition: FXVec2f.h:63
bool operator!=(const FXVec2f &v) const
Definition: FXVec2f.h:109
#define FXMIN(a, b)
Return the minimum of a or b.
Definition: fxdefs.h:491
FXVec2f & set(FXfloat xx, FXfloat yy)
Set value from components.
Definition: FXVec2f.h:72
FXVec2f operator+(const FXVec2f &v) const
Vector and vector.
Definition: FXVec2f.h:89
bool operator<(const FXString &s1, const FXString &s2)
bool operator!() const
Test if zero.
Definition: FXVec2f.h:105
bool operator>(const FXString &s1, const FXString &s2)
float FXfloat
Definition: fxdefs.h:398
FXVec2f & clamp(FXfloat lo, FXfloat hi)
Clamp values of vector between limits.
Definition: FXVec2f.h:137
FXVec2f & set(const FXVec2f &v)
Set value from another vector.
Definition: FXVec2f.h:66
FXfloat y
Definition: FXVec2f.h:38
FXfloat x
Definition: FXVec2f.h:37
bool operator<(const FXVec2f &v) const
Inequality tests.
Definition: FXVec2f.h:117
#define sqrtf(x)
Definition: fxdefs.h:696
FXStream & operator<<(FXStream &store, const FXDate &d)
const FXfloat & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec2f.h:57
FXVec2f operator-() const
Definition: FXVec2f.h:86
bool operator!=(const FXString &s1, const FXString &s2)
FXVec2f operator+() const
Unary.
Definition: FXVec2f.h:85
bool operator==(const FXString &s1, const FXString &s2)
FXMat3d operator*(FXdouble x, const FXMat3d &a)

Copyright © 1997-2005 Jeroen van der Zijp