zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Vector2f.cpp
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 #include <zeni.h>
19 
20 #include <cmath>
21 
22 #include <Zeni/Define.h>
23 
24 namespace Zeni {
25 
27  float mplier = magnitude();
28 
29  if(INFINTESSIMAL(mplier)) {
30  degenerate = true;
31  return *this;
32  }
33  else
34  degenerate = false;
35 
36  mplier = 1.0f / mplier;
37 
38  i *= mplier;
39  j *= mplier;
40 
41  return *this;
42  }
43 
45  float mplier = magnitude();
46 
47  if(INFINTESSIMAL(mplier))
48  return Vector2f(*this, true);
49 
50  mplier = 1.0f / mplier;
51 
52  return Vector2f(i * mplier, j * mplier);
53  }
54 
55  float Vector2f::theta() const {
56  if(i > 0)
57  return float(atan(j/i));
58  else if(i < 0)
59  return float(atan(j/i)) + Global::pi;
60  else if(j > 0)
61  return Global::pi_over_two;
62  else if(j < 0)
64  return 0;
65  }
66 
67  void Vector2f::set_spherical(const float &theta, const float &magnitude) {
68  i = magnitude;
69  j = float(sin(theta)) * i;
70  i *= float(cos(theta));
71  degenerate = false;
72  }
73 
74  std::ostream & serialize(std::ostream &os, const Vector2f &value) {
75  return serialize(serialize(os, value.i), value.j);
76  }
77 
78  std::istream & unserialize(std::istream &is, Vector2f &value) {
79  return unserialize(unserialize(is, value.i), value.j);
80  }
81 
82 }
83 
84 #include <Zeni/Undefine.h>
Vector2f(const bool &degenerate_=false)
The best way to create a Vector3f.
Definition: Vector2f.hxx:33
void set_spherical(const float &theta, const float &magnitude=1)
Set the vector using spherical coordinates.
Definition: Vector2f.cpp:67
std::istream & unserialize(std::istream &is, Color &value)
Definition: Color.cpp:72
std::ostream & serialize(std::ostream &os, const Color &value)
Definition: Color.cpp:68
bool degenerate
Definition: Vector2f.h:95
const float pi
pi == 3.1415926...
Definition: Vector3f.cpp:27
Vector2f normalized() const
Get the normalized vector.
Definition: Vector2f.cpp:44
float magnitude() const
Get the magnitude of the vector.
Definition: Vector2f.hxx:123
const float pi_over_two
pi/23
Definition: Vector3f.cpp:28
double sin(double x)
Definition: s_sin.c:56
EGLSurface EGLint void ** value
Definition: eglext.h:301
Vector2f & normalize()
Normalize the vector.
Definition: Vector2f.cpp:26
const float three_pi_over_two
3*pi/2
Definition: Vector3f.cpp:29
double atan(double x)
Definition: s_atan.c:67
double cos(double x)
Definition: s_cos.c:56
float theta() const
theta == radians north of vector i
Definition: Vector2f.cpp:55
#define INFINTESSIMAL(x)
Definition: Define.h:134
A 2-Space Vector Class.
Definition: Vector2f.h:41