1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2013/05/02
|
---|
6 | // filename: Vector2D.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: Class defining a 2D vector
|
---|
14 | //
|
---|
15 | //
|
---|
16 | /*********************************************************************/
|
---|
17 |
|
---|
18 | #include "Vector2D.h"
|
---|
19 | #include "Euler.h"
|
---|
20 | #include <math.h>
|
---|
21 |
|
---|
22 | namespace flair {
|
---|
23 | namespace core {
|
---|
24 |
|
---|
25 | template class Vector2D<double>;
|
---|
26 | template Vector2D<double> operator+(const Vector2D<double>&, const Vector2D<double>&);
|
---|
27 | template Vector2D<double> operator-(const Vector2D<double>&, const Vector2D<double>&);
|
---|
28 | template Vector2D<double> operator-(const Vector2D<double>&);
|
---|
29 | template Vector2D<double> operator/(const Vector2D<double>&, float);
|
---|
30 | template Vector2D<double> operator*(const Vector2D<double>&, float);
|
---|
31 | template Vector2D<double> operator*(float, const Vector2D<double>&);
|
---|
32 |
|
---|
33 | template class Vector2D<float>;
|
---|
34 | template Vector2D<float> operator+(const Vector2D<float>&, const Vector2D<float>&);
|
---|
35 | template Vector2D<float> operator-(const Vector2D<float>&, const Vector2D<float>&);
|
---|
36 | template Vector2D<float> operator-(const Vector2D<float>&);
|
---|
37 | template Vector2D<float> operator/(const Vector2D<float>&, float);
|
---|
38 | template Vector2D<float> operator*(const Vector2D<float>&, float);
|
---|
39 | template Vector2D<float> operator*(float, const Vector2D<float>&);
|
---|
40 |
|
---|
41 | template <typename T> Vector2D<T>::Vector2D(T inX, T inY) : x(inX), y(inY) {}
|
---|
42 |
|
---|
43 | template <typename T> Vector2D<T>::~Vector2D() {}
|
---|
44 |
|
---|
45 | template <typename T> Vector2D<T> &Vector2D<T>::operator+=(const Vector2D<T> &vector) {
|
---|
46 | x += vector.x;
|
---|
47 | y += vector.y;
|
---|
48 | return (*this);
|
---|
49 | }
|
---|
50 |
|
---|
51 | template <typename T> Vector2D<T> &Vector2D<T>::operator-=(const Vector2D<T> &vector) {
|
---|
52 | x -= vector.x;
|
---|
53 | y -= vector.y;
|
---|
54 | return (*this);
|
---|
55 | }
|
---|
56 |
|
---|
57 | template <typename T> Vector2D<T> operator+(const Vector2D<T> &vectorA, const Vector2D<T> &vectorB) {
|
---|
58 | return Vector2D<T>(vectorA.x + vectorB.x, vectorA.y + vectorB.y);
|
---|
59 | }
|
---|
60 |
|
---|
61 | template <typename T> Vector2D<T> operator-(const Vector2D<T> &vectorA, const Vector2D<T> &vectorB) {
|
---|
62 | return Vector2D<T>(vectorA.x - vectorB.x, vectorA.y - vectorB.y);
|
---|
63 | }
|
---|
64 |
|
---|
65 | template <typename T> Vector2D<T> operator-(const Vector2D<T> &vectorA) {
|
---|
66 | return Vector2D<T>(-vectorA.x, -vectorA.y);
|
---|
67 | }
|
---|
68 |
|
---|
69 | template <typename T> Vector2D<T> operator/(const Vector2D<T> &vector, float coeff) {
|
---|
70 | if (coeff != 0) {
|
---|
71 | return Vector2D<T>(vector.x / coeff, vector.y / coeff);
|
---|
72 | } else {
|
---|
73 | return Vector2D<T>(0, 0);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | template <typename T> Vector2D<T> operator*(const Vector2D<T> &vector, float coeff) {
|
---|
78 | return Vector2D<T>(vector.x * coeff, vector.y * coeff);
|
---|
79 | }
|
---|
80 |
|
---|
81 | template <typename T> Vector2D<T> operator*(float coeff, const Vector2D<T> &vector) {
|
---|
82 | return Vector2D<T>(vector.x * coeff, vector.y * coeff);
|
---|
83 | }
|
---|
84 |
|
---|
85 | template <typename T> void Vector2D<T>::Rotate(float value) {
|
---|
86 | float xTmp;
|
---|
87 | xTmp = x * cosf(value) - y * sinf(value);
|
---|
88 | y = x * sinf(value) + y * cosf(value);
|
---|
89 | x = xTmp;
|
---|
90 | }
|
---|
91 |
|
---|
92 | template <typename T> void Vector2D<T>::RotateDeg(float value) { Rotate(Euler::ToRadian(value)); }
|
---|
93 |
|
---|
94 | template <typename T> float Vector2D<T>::GetNorm(void) const { return sqrt(x * x + y * y); }
|
---|
95 |
|
---|
96 | template <typename T> void Vector2D<T>::Normalize(void) {
|
---|
97 | float n = GetNorm();
|
---|
98 | if (n != 0) {
|
---|
99 | x = x / n;
|
---|
100 | y = y / n;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | template <typename T> void Vector2D<T>::Saturate(Vector2D min, Vector2D max) {
|
---|
105 | if (x < min.x)
|
---|
106 | x = min.x;
|
---|
107 | if (x > max.x)
|
---|
108 | x = max.x;
|
---|
109 |
|
---|
110 | if (y < min.y)
|
---|
111 | y = min.y;
|
---|
112 | if (y > max.y)
|
---|
113 | y = max.y;
|
---|
114 | }
|
---|
115 |
|
---|
116 | template <typename T> void Vector2D<T>::Saturate(float min, float max) {
|
---|
117 | Saturate(Vector2D(min, min), Vector2D(max, max));
|
---|
118 | }
|
---|
119 |
|
---|
120 | template <typename T> void Vector2D<T>::Saturate(const Vector2D &value) {
|
---|
121 | float x = fabs(value.x);
|
---|
122 | float y = fabs(value.y);
|
---|
123 | Saturate(Vector2D(-x, -y), Vector2D(x, y));
|
---|
124 | }
|
---|
125 |
|
---|
126 | template <typename T> void Vector2D<T>::Saturate(float value) {
|
---|
127 | float sat = fabs(value);
|
---|
128 | Saturate(Vector2D(-sat, -sat), Vector2D(sat, sat));
|
---|
129 | }
|
---|
130 |
|
---|
131 | } // end namespace core
|
---|
132 | } // end namespace flair
|
---|