My Project
Vec2d.hpp
1 /*
2  * prjsv 2015
3  * 2013, 2014
4  * Marco Antognini
5  */
6 
7 #ifndef INFOSV_VEC2D_HPP
8 #define INFOSV_VEC2D_HPP
9 
10 #include <JSON/JSON.hpp>
11 #include <Utility/Constants.hpp>
12 
13 #include <SFML/System.hpp>
14 
23 class Vec2d
24 {
25 public:
32  static Vec2d fromAngle(double rad);
33 
42  static Vec2d fromRandomAngle(double min = 0, double max = TAU);
43 
51  static Vec2d fromJSON(j::Value json);
52 
53 public:
54  Vec2d();
55  Vec2d(double x, double y);
56 
57  Vec2d(sf::Vector2f const& sfvect);
58  Vec2d(sf::Vector2i const& sfvect);
59 
60  Vec2d(Vec2d const& other) = default;
61 
62  // We don't need:
63  // Vec2d& operator=(sf::Vector2f const& sfvect);
64  // Vec2d& operator=(sf::Vector2i const& sfvect);
65  // Because C++ will automatically construct a Vec2d
66  // with the appropriate constructor and the following
67  // assignment operator will be called:
68 
69  Vec2d& operator=(Vec2d const& other) = default;
70 
71  operator sf::Vector2f() const;
72  operator sf::Vector2i() const;
73 
77  j::Value save() const;
78 
84  double lengthSquared() const;
85 
91  double length() const;
92 
98  Vec2d normalised() const;
99 
105  Vec2d normal() const;
106 
112  double angle() const;
113 
120  double dot(Vec2d const& other) const;
121 
128  int sign(Vec2d const& other) const;
129 
130  Vec2d operator-() const;
131  Vec2d operator-(Vec2d const& b) const;
132  Vec2d operator+(Vec2d const& b) const;
133  Vec2d operator*(double c) const;
134  Vec2d operator/(double c) const;
135 
136  Vec2d& operator-=(Vec2d const& b);
137  Vec2d& operator+=(Vec2d const& b);
138  Vec2d& operator*=(double c);
139  Vec2d& operator/=(double c);
140 
141  bool operator==(Vec2d const& b) const;
142  bool operator!=(Vec2d const& b) const;
143 
144  // /*!
145  // * @brief Accesses the coordinates by dimension, read-write.
146  // *
147  // * @param axis only value 0 and 1 are allowed
148  // * @return x if axis is 0, y if axis if 1, undefined otherwise
149  // */
150  // double& operator[](int axis);
151 
158  double operator[](int axis) const;
159 
160  double x() const;
161  double y() const;
162 
163 
164 private:
165  double x_, y_;
166 };
167 
168 
169 Vec2d operator*(double c, Vec2d const& a);
170 
171 
179 double distance(Vec2d const& x, Vec2d const& y);
180 
188 Vec2d normal(Vec2d const& a, Vec2d const& b);
189 
190 std::ostream& operator<<(std::ostream& out, Vec2d const& v);
191 
192 #endif // INFOSV_VEC2D_HPP
int sign(Vec2d const &other) const
Compares the angle between another vector and this one.
Definition: Vec2d.cpp:223
static Vec2d fromRandomAngle(double min=0, double max=TAU)
Create a random vector.
Definition: Vec2d.cpp:19
double angle() const
Computes the angle of the vector in polar coordinates.
Definition: Vec2d.cpp:213
double operator[](int axis) const
Accesses the coordinates by dimension, read-write.
Definition: Vec2d.cpp:156
Bridge vector class to sf::Vector2i/f that provides some common math methods.
Definition: Vec2d.hpp:23
j::Value save() const
Save internal representation as JSON value.
Definition: Vec2d.cpp:64
static Vec2d fromJSON(j::Value json)
Construct a vector from its internal representation in JSON format.
Definition: Vec2d.cpp:24
double dot(Vec2d const &other) const
Computes the dot product between this and another vector.
Definition: Vec2d.cpp:218
Vec2d normalised() const
Computes the normalized vector.
Definition: Vec2d.cpp:192
Vec2d normal() const
Computes the normal (orthogonal) vector for this vector.
Definition: Vec2d.cpp:198
static Vec2d fromAngle(double rad)
Create a unitary Vec2d from an angle.
Definition: Vec2d.cpp:14
double length() const
Computes the length of the vector.
Definition: Vec2d.cpp:187
Vec2d operator-() const
Negation.
Definition: Vec2d.cpp:74
double lengthSquared() const
Computes the length of the vector (squared).
Definition: Vec2d.cpp:182