source: flair-src/trunk/lib/FlairCore/src/Vector2D.cpp@ 465

Last change on this file since 465 was 336, checked in by Sanahuja Guillaume, 3 years ago

use float

File size: 4.0 KB
Line 
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
22namespace flair {
23namespace core {
24
25template class Vector2D<double>;
26template Vector2D<double> operator+(const Vector2D<double>&, const Vector2D<double>&);
27template Vector2D<double> operator-(const Vector2D<double>&, const Vector2D<double>&);
28template Vector2D<double> operator-(const Vector2D<double>&);
29template Vector2D<double> operator/(const Vector2D<double>&, float);
30template Vector2D<double> operator*(const Vector2D<double>&, float);
31template Vector2D<double> operator*(float, const Vector2D<double>&);
32
33template class Vector2D<float>;
34template Vector2D<float> operator+(const Vector2D<float>&, const Vector2D<float>&);
35template Vector2D<float> operator-(const Vector2D<float>&, const Vector2D<float>&);
36template Vector2D<float> operator-(const Vector2D<float>&);
37template Vector2D<float> operator/(const Vector2D<float>&, float);
38template Vector2D<float> operator*(const Vector2D<float>&, float);
39template Vector2D<float> operator*(float, const Vector2D<float>&);
40
41template <typename T> Vector2D<T>::Vector2D(T inX, T inY) : x(inX), y(inY) {}
42
43template <typename T> Vector2D<T>::~Vector2D() {}
44
45template <typename T> Vector2D<T> &Vector2D<T>::operator+=(const Vector2D<T> &vector) {
46 x += vector.x;
47 y += vector.y;
48 return (*this);
49}
50
51template <typename T> Vector2D<T> &Vector2D<T>::operator-=(const Vector2D<T> &vector) {
52 x -= vector.x;
53 y -= vector.y;
54 return (*this);
55}
56
57template <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
61template <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
65template <typename T> Vector2D<T> operator-(const Vector2D<T> &vectorA) {
66 return Vector2D<T>(-vectorA.x, -vectorA.y);
67}
68
69template <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
77template <typename T> Vector2D<T> operator*(const Vector2D<T> &vector, float coeff) {
78 return Vector2D<T>(vector.x * coeff, vector.y * coeff);
79}
80
81template <typename T> Vector2D<T> operator*(float coeff, const Vector2D<T> &vector) {
82 return Vector2D<T>(vector.x * coeff, vector.y * coeff);
83}
84
85template <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
92template <typename T> void Vector2D<T>::RotateDeg(float value) { Rotate(Euler::ToRadian(value)); }
93
94template <typename T> float Vector2D<T>::GetNorm(void) const { return sqrtf(x * x + y * y); }
95
96template <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
104template <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
116template <typename T> void Vector2D<T>::Saturate(float min, float max) {
117 Saturate(Vector2D(min, min), Vector2D(max, max));
118}
119
120template <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
126template <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
Note: See TracBrowser for help on using the repository browser.