zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Vector2f.hxx
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef ZENI_VECTOR2F_HXX
19 #define ZENI_VECTOR2F_HXX
20 
21 #include <Zeni/Vector2f.h>
22 
23 // HXXed below
24 #include <Zeni/Coordinate.h>
25 #include <Zeni/Vector3f.h>
26 
27 // Not HXXed
28 #include <cassert>
29 #include <cmath>
30 
31 namespace Zeni {
32 
33  Vector2f::Vector2f(const bool &degenerate_)
34  : i(0.0f), j(0.0f), degenerate(degenerate_)
35  {
36  }
37 
38  Vector2f::Vector2f(const float &i_, const float &j_, const bool &degenerate_)
39  : i(i_), j(j_), degenerate(degenerate_)
40  {
41  }
42 
43  Vector2f::Vector2f(const Vector2f &rhs, const bool &degenerate_)
44  : i(rhs.i), j(rhs.j), degenerate(rhs.degenerate || degenerate_)
45  {
46  }
47 
49  : i(rhs.x), j(rhs.y)
50  {
51  }
52 
54  : i(rhs.i), j(rhs.j)
55  {
56  }
57 
59  return Vector2f(i + rhs.i,
60  j + rhs.j,
61  degenerate || rhs.degenerate);
62  }
63 
65  return Vector2f(i - rhs.i,
66  j - rhs.j,
67  degenerate || rhs.degenerate);
68  }
69 
71  i += rhs.i;
72  j += rhs.j;
73  degenerate |= rhs.degenerate;
74  return *this;
75  }
76 
78  i -= rhs.i;
79  j -= rhs.j;
80  degenerate |= rhs.degenerate;
81  return *this;
82  }
83 
84  float Vector2f::operator*(const Vector2f &rhs) const {
85  return
86  i * rhs.i +
87  j * rhs.j;
88  }
89 
90  Vector2f Vector2f::operator*(const float &rhs) const {
91  return Vector2f(i * rhs, j * rhs, degenerate);
92  }
93 
94  Vector2f Vector2f::operator/(const float &rhs) const {
95  return Vector2f(i / rhs, j / rhs, degenerate);
96  }
97 
98  Vector2f & Vector2f::operator*=(const float &rhs) {
99  i *= rhs;
100  j *= rhs;
101  return *this;
102  }
103 
104  Vector2f & Vector2f::operator/=(const float &rhs) {
105  i /= rhs;
106  j /= rhs;
107  return *this;
108  }
109 
111  return *this * -1;
112  }
113 
114  // Vector Scalar Multiplication Part II of II
115  Vector2f operator*(const float &lhs, const Vector2f &rhs) {
116  return rhs * lhs;
117  }
118 
119  float Vector2f::magnitude2() const {
120  return i * i + j * j;
121  }
122 
123  float Vector2f::magnitude() const {
124  return float(sqrt(magnitude2()));
125  }
126 
128  return Vector2f(i, 0.0f);
129  }
130 
132  return Vector2f(0.0f, j);
133  }
134 
136  return Vector2f(i*rhs.i, j*rhs.j, degenerate || rhs.degenerate);
137  }
138 
140  return Vector2f(i/rhs.i, j/rhs.j, degenerate || rhs.degenerate);
141  }
142 
143  float Vector2f::angle_between(const Vector2f &rhs) const {
144  const float a = magnitude();
145  const float b = rhs.magnitude();
146  const float c = (rhs - *this).magnitude();
147 
148  return float(acos((a * a + b * b - c * c) / (2 * a * b)));
149  }
150 
151  const float & Vector2f::operator[](const int &index) const {
152  assert(-1 < index && index < 2);
153  const float * const ptr = &i;
154  return ptr[index];
155  }
156 
157  float & Vector2f::operator[](const int &index) {
158  assert(-1 < index && index < 2);
159  float * const ptr = &i;
160  return ptr[index];
161  }
162 
163 }
164 
165 #include <Zeni/Coordinate.hxx>
166 #include <Zeni/Vector3f.hxx>
167 
168 #endif
Vector2f(const bool &degenerate_=false)
The best way to create a Vector3f.
Definition: Vector2f.hxx:33
Vector2f get_j() const
Get just the j-part.
Definition: Vector2f.hxx:131
GLclampf f
Definition: glew.h:3390
EGLSurface EGLint x
Definition: eglext.h:293
int32_t j
Definition: e_log.c:102
float operator*(const Vector2f &rhs) const
Get the dot-product.
Definition: Vector2f.hxx:84
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8736
#define assert(x)
Definition: SDL_malloc.c:1234
bool degenerate
Definition: Vector2f.h:95
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
Vector2f & operator*=(const float &rhs)
Set equal to the scalar multiple.
Definition: Vector2f.hxx:98
const GLfloat * c
Definition: glew.h:14913
Vector2f & operator-=(const Vector2f &rhs)
Set equal to the difference.
Definition: Vector2f.hxx:77
GLuint index
Definition: glew.h:1800
Quaternion operator*(const float &lhs, const Quaternion &rhs)
Definition: Quaternion.h:110
float magnitude() const
Get the magnitude of the vector.
Definition: Vector2f.hxx:123
Vector2f divide_by(const Vector2f &rhs) const
Divide corresponding members.
Definition: Vector2f.hxx:139
Vector2f operator-() const
Get the negation.
Definition: Vector2f.hxx:110
float angle_between(const Vector2f &rhs) const
Find the angle between the Vector3fs.
Definition: Vector2f.hxx:143
EGLSurface EGLint EGLint y
Definition: eglext.h:293
Vector2f multiply_by(const Vector2f &rhs) const
Multiply corresponding members.
Definition: Vector2f.hxx:135
const float & operator[](const int &index) const
Get &#39;index&#39;.
Definition: Vector2f.hxx:151
Vector2f operator+(const Vector2f &rhs) const
Get the sum.
Definition: Vector2f.hxx:58
Vector2f & operator/=(const float &rhs)
Set equal to the scalar something.
Definition: Vector2f.hxx:104
Vector2f get_i() const
Get just the i-part.
Definition: Vector2f.hxx:127
GLdouble GLdouble GLdouble b
Definition: glew.h:8383
int i
Definition: pngrutil.c:1377
float magnitude2() const
Get the &#39;magnitude squared&#39; of the vector.
Definition: Vector2f.hxx:119
A 2D Point represented with floats.
Definition: Coordinate.h:98
Vector2f & operator+=(const Vector2f &rhs)
Set equal to the sum.
Definition: Vector2f.hxx:70
A 2-Space Vector Class.
Definition: Vector2f.h:41
Vector2f operator/(const float &rhs) const
Get the scalar... something.
Definition: Vector2f.hxx:94