[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 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 |
|
---|
[15] | 22 | namespace flair {
|
---|
| 23 | namespace core {
|
---|
[167] | 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>&);
|
---|
[2] | 32 |
|
---|
[167] | 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>&);
|
---|
[2] | 40 |
|
---|
[167] | 41 | template <typename T> Vector2D<T>::Vector2D(T inX, T inY) : x(inX), y(inY) {}
|
---|
[2] | 42 |
|
---|
[167] | 43 | template <typename T> Vector2D<T>::~Vector2D() {}
|
---|
[2] | 44 |
|
---|
[167] | 45 | template <typename T> Vector2D<T> &Vector2D<T>::operator+=(const Vector2D<T> &vector) {
|
---|
[161] | 46 | x += vector.x;
|
---|
| 47 | y += vector.y;
|
---|
| 48 | return (*this);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[167] | 51 | template <typename T> Vector2D<T> &Vector2D<T>::operator-=(const Vector2D<T> &vector) {
|
---|
[161] | 52 | x -= vector.x;
|
---|
| 53 | y -= vector.y;
|
---|
| 54 | return (*this);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[167] | 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);
|
---|
[2] | 59 | }
|
---|
| 60 |
|
---|
[167] | 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);
|
---|
[2] | 63 | }
|
---|
| 64 |
|
---|
[167] | 65 | template <typename T> Vector2D<T> operator-(const Vector2D<T> &vectorA) {
|
---|
| 66 | return Vector2D<T>(-vectorA.x, -vectorA.y);
|
---|
[161] | 67 | }
|
---|
| 68 |
|
---|
[167] | 69 | template <typename T> Vector2D<T> operator/(const Vector2D<T> &vector, float coeff) {
|
---|
[15] | 70 | if (coeff != 0) {
|
---|
[167] | 71 | return Vector2D<T>(vector.x / coeff, vector.y / coeff);
|
---|
[15] | 72 | } else {
|
---|
[167] | 73 | return Vector2D<T>(0, 0);
|
---|
[15] | 74 | }
|
---|
[2] | 75 | }
|
---|
| 76 |
|
---|
[167] | 77 | template <typename T> Vector2D<T> operator*(const Vector2D<T> &vector, float coeff) {
|
---|
| 78 | return Vector2D<T>(vector.x * coeff, vector.y * coeff);
|
---|
[2] | 79 | }
|
---|
| 80 |
|
---|
[167] | 81 | template <typename T> Vector2D<T> operator*(float coeff, const Vector2D<T> &vector) {
|
---|
| 82 | return Vector2D<T>(vector.x * coeff, vector.y * coeff);
|
---|
[2] | 83 | }
|
---|
| 84 |
|
---|
[167] | 85 | template <typename T> void Vector2D<T>::Rotate(float value) {
|
---|
[15] | 86 | float xTmp;
|
---|
| 87 | xTmp = x * cosf(value) - y * sinf(value);
|
---|
| 88 | y = x * sinf(value) + y * cosf(value);
|
---|
| 89 | x = xTmp;
|
---|
[2] | 90 | }
|
---|
| 91 |
|
---|
[167] | 92 | template <typename T> void Vector2D<T>::RotateDeg(float value) { Rotate(Euler::ToRadian(value)); }
|
---|
[2] | 93 |
|
---|
[167] | 94 | template <typename T> float Vector2D<T>::GetNorm(void) const { return sqrt(x * x + y * y); }
|
---|
[2] | 95 |
|
---|
[167] | 96 | template <typename T> void Vector2D<T>::Normalize(void) {
|
---|
[15] | 97 | float n = GetNorm();
|
---|
| 98 | if (n != 0) {
|
---|
| 99 | x = x / n;
|
---|
| 100 | y = y / n;
|
---|
| 101 | }
|
---|
[2] | 102 | }
|
---|
| 103 |
|
---|
[167] | 104 | template <typename T> void Vector2D<T>::Saturate(Vector2D min, Vector2D max) {
|
---|
[15] | 105 | if (x < min.x)
|
---|
| 106 | x = min.x;
|
---|
| 107 | if (x > max.x)
|
---|
| 108 | x = max.x;
|
---|
[2] | 109 |
|
---|
[15] | 110 | if (y < min.y)
|
---|
| 111 | y = min.y;
|
---|
| 112 | if (y > max.y)
|
---|
| 113 | y = max.y;
|
---|
[2] | 114 | }
|
---|
| 115 |
|
---|
[167] | 116 | template <typename T> void Vector2D<T>::Saturate(float min, float max) {
|
---|
[15] | 117 | Saturate(Vector2D(min, min), Vector2D(max, max));
|
---|
[2] | 118 | }
|
---|
| 119 |
|
---|
[167] | 120 | template <typename T> void Vector2D<T>::Saturate(const Vector2D &value) {
|
---|
[15] | 121 | float x = fabs(value.x);
|
---|
| 122 | float y = fabs(value.y);
|
---|
| 123 | Saturate(Vector2D(-x, -y), Vector2D(x, y));
|
---|
[2] | 124 | }
|
---|
| 125 |
|
---|
[167] | 126 | template <typename T> void Vector2D<T>::Saturate(float value) {
|
---|
[15] | 127 | float sat = fabs(value);
|
---|
| 128 | Saturate(Vector2D(-sat, -sat), Vector2D(sat, sat));
|
---|
[2] | 129 | }
|
---|
| 130 |
|
---|
| 131 | } // end namespace core
|
---|
| 132 | } // end namespace flair
|
---|