1 | #ifndef GFXMATH_VEC2_INCLUDED // -*- C++ -*-
|
---|
2 | #define GFXMATH_VEC2_INCLUDED
|
---|
3 |
|
---|
4 | /************************************************************************
|
---|
5 |
|
---|
6 | 2D Vector class.
|
---|
7 |
|
---|
8 | $Id: Vec2.h,v 1.6 1997/03/17 22:52:25 garland Exp $
|
---|
9 |
|
---|
10 | ************************************************************************/
|
---|
11 |
|
---|
12 | namespace simplif
|
---|
13 | {
|
---|
14 | class Vec2 {
|
---|
15 | private:
|
---|
16 | real elt[2];
|
---|
17 |
|
---|
18 | protected:
|
---|
19 | inline void copy(const Vec2& v);
|
---|
20 |
|
---|
21 | public:
|
---|
22 | //
|
---|
23 | // Standard constructors
|
---|
24 | //
|
---|
25 | Vec2(real x=0, real y=0) { elt[0]=x; elt[1]=y; }
|
---|
26 | Vec2(const Vec2& v) { copy(v); }
|
---|
27 | Vec2(const real *v) { elt[0]=v[0]; elt[1]=v[1]; }
|
---|
28 |
|
---|
29 | //
|
---|
30 | // Access methods
|
---|
31 | //
|
---|
32 | #ifdef SAFETY
|
---|
33 | real& operator()(int i) { assert(i>=0 && i<2); return elt[i]; }
|
---|
34 | real operator()(int i) const { assert(i>=0 && i<2); return elt[i]; }
|
---|
35 | #else
|
---|
36 | real& operator()(int i) { return elt[i]; }
|
---|
37 | real operator()(int i) const { return elt[i]; }
|
---|
38 | #endif
|
---|
39 | real& operator[](int i) { return elt[i]; }
|
---|
40 | real operator[](int i) const { return elt[i]; }
|
---|
41 |
|
---|
42 | real *raw() { return elt; }
|
---|
43 | const real *raw() const { return elt; }
|
---|
44 |
|
---|
45 |
|
---|
46 | //
|
---|
47 | // Comparison operators
|
---|
48 | //
|
---|
49 | inline bool operator==(const Vec2& v) const;
|
---|
50 | inline bool operator!=(const Vec2& v) const;
|
---|
51 |
|
---|
52 | //
|
---|
53 | // Assignment and in-place arithmetic methods
|
---|
54 | //
|
---|
55 | inline void set(real x, real y) { elt[0]=x; elt[1]=y; }
|
---|
56 | inline Vec2& operator=(const Vec2& v);
|
---|
57 | inline Vec2& operator+=(const Vec2& v);
|
---|
58 | inline Vec2& operator-=(const Vec2& v);
|
---|
59 | inline Vec2& operator*=(real s);
|
---|
60 | inline Vec2& operator/=(real s);
|
---|
61 |
|
---|
62 | //
|
---|
63 | // Binary arithmetic methods
|
---|
64 | //
|
---|
65 | inline Vec2 operator+(const Vec2& v) const;
|
---|
66 | inline Vec2 operator-(const Vec2& v) const;
|
---|
67 | inline Vec2 operator-() const;
|
---|
68 |
|
---|
69 | inline Vec2 operator*(real s) const;
|
---|
70 | inline Vec2 operator/(real s) const;
|
---|
71 | inline real operator*(const Vec2& v) const;
|
---|
72 | };
|
---|
73 | }
|
---|
74 |
|
---|
75 | ////////////////////////////////////////////////////////////////////////
|
---|
76 | //
|
---|
77 | // Method definitions
|
---|
78 | //
|
---|
79 |
|
---|
80 | inline void simplif::Vec2::copy(const simplif::Vec2& v)
|
---|
81 | {
|
---|
82 | elt[0]=v.elt[0]; elt[1]=v.elt[1];
|
---|
83 | }
|
---|
84 |
|
---|
85 | inline bool simplif::Vec2::operator==(const simplif::Vec2& v) const
|
---|
86 | {
|
---|
87 | real dx=elt[X]-v[X], dy=elt[Y]-v[Y];
|
---|
88 | return (dx*dx + dy*dy) < FEQ_EPS2;
|
---|
89 | }
|
---|
90 |
|
---|
91 | inline bool simplif::Vec2::operator!=(const simplif::Vec2& v) const
|
---|
92 | {
|
---|
93 | real dx=elt[X]-v[X], dy=elt[Y]-v[Y];
|
---|
94 | return (dx*dx + dy*dy) > FEQ_EPS2;
|
---|
95 | }
|
---|
96 |
|
---|
97 | inline simplif::Vec2& simplif::Vec2::operator=(const simplif::Vec2& v)
|
---|
98 | {
|
---|
99 | copy(v);
|
---|
100 | return *this;
|
---|
101 | }
|
---|
102 |
|
---|
103 | inline simplif::Vec2& simplif::Vec2::operator+=(const simplif::Vec2& v)
|
---|
104 | {
|
---|
105 | elt[0] += v[0]; elt[1] += v[1];
|
---|
106 | return *this;
|
---|
107 | }
|
---|
108 |
|
---|
109 | inline simplif::Vec2& simplif::Vec2::operator-=(const simplif::Vec2& v)
|
---|
110 | {
|
---|
111 | elt[0] -= v[0]; elt[1] -= v[1];
|
---|
112 | return *this;
|
---|
113 | }
|
---|
114 |
|
---|
115 | inline simplif::Vec2& simplif::Vec2::operator*=(simplif::real s)
|
---|
116 | {
|
---|
117 | elt[0] *= s; elt[1] *= s;
|
---|
118 | return *this;
|
---|
119 | }
|
---|
120 |
|
---|
121 | inline simplif::Vec2& simplif::Vec2::operator/=(simplif::real s)
|
---|
122 | {
|
---|
123 | elt[0] /= s; elt[1] /= s;
|
---|
124 | return *this;
|
---|
125 | }
|
---|
126 |
|
---|
127 | inline simplif::Vec2 simplif::Vec2::operator+(const simplif::Vec2& v) const
|
---|
128 | {
|
---|
129 | return Vec2(elt[0]+v[0], elt[1]+v[1]);
|
---|
130 | }
|
---|
131 |
|
---|
132 | inline simplif::Vec2 simplif::Vec2::operator-(const simplif::Vec2& v) const
|
---|
133 | {
|
---|
134 | return Vec2(elt[0]-v[0], elt[1]-v[1]);
|
---|
135 | }
|
---|
136 |
|
---|
137 | inline simplif::Vec2 simplif::Vec2::operator-() const
|
---|
138 | {
|
---|
139 | return simplif::Vec2(-elt[0], -elt[1]);
|
---|
140 | }
|
---|
141 |
|
---|
142 | inline simplif::Vec2 simplif::Vec2::operator*(simplif::real s) const
|
---|
143 | {
|
---|
144 | return Vec2(elt[0]*s, elt[1]*s);
|
---|
145 | }
|
---|
146 |
|
---|
147 | inline simplif::Vec2 simplif::Vec2::operator/(simplif::real s) const
|
---|
148 | {
|
---|
149 | return Vec2(elt[0]/s, elt[1]/s);
|
---|
150 | }
|
---|
151 |
|
---|
152 | inline simplif::real simplif::Vec2::operator*(const simplif::Vec2& v) const
|
---|
153 | {
|
---|
154 | return elt[0]*v[0] + elt[1]*v[1];
|
---|
155 | }
|
---|
156 |
|
---|
157 | // Make scalar multiplication commutative
|
---|
158 | inline simplif::Vec2 operator*(simplif::real s, const simplif::Vec2& v) { return v*s; }
|
---|
159 |
|
---|
160 |
|
---|
161 | ////////////////////////////////////////////////////////////////////////
|
---|
162 | //
|
---|
163 | // Primitive function definitions
|
---|
164 | //
|
---|
165 |
|
---|
166 | namespace simplif
|
---|
167 | {
|
---|
168 | inline real norm(const simplif::Vec2& v) { return sqrt(v[0]*v[0] + v[1]*v[1]); }
|
---|
169 | inline real norm2(const simplif::Vec2& v) { return v[0]*v[0] + v[1]*v[1]; }
|
---|
170 | inline real length(const simplif::Vec2& v) { return norm(v); }
|
---|
171 |
|
---|
172 | inline real unitize(simplif::Vec2& v)
|
---|
173 | {
|
---|
174 | simplif::real l=norm2(v);
|
---|
175 | if( l!=1.0 && l!=0.0 )
|
---|
176 | {
|
---|
177 | l = sqrt(l);
|
---|
178 | v /= l;
|
---|
179 | }
|
---|
180 | return l;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | ////////////////////////////////////////////////////////////////////////
|
---|
185 | //
|
---|
186 | // Misc. function definitions
|
---|
187 | //
|
---|
188 |
|
---|
189 | /* inline ostream& operator<<(ostream& out, const Vec2& v)
|
---|
190 | {
|
---|
191 | return out << "[" << v[0] << " " << v[1] << "]";
|
---|
192 | }
|
---|
193 |
|
---|
194 | inline istream& operator>>(istream& in, Vec2& v)
|
---|
195 | {
|
---|
196 | return in >> "[" >> v[0] >> v[1] >> "]";
|
---|
197 | }
|
---|
198 | */
|
---|
199 | #ifdef GFXGL_INCLUDED
|
---|
200 | inline void glV(const Vec2& v) { glVertex(v[X], v[Y]); }
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | // GFXMATH_VEC2_INCLUDED
|
---|
204 | #endif
|
---|
205 |
|
---|