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

Last change on this file since 167 was 167, checked in by Sanahuja Guillaume, 5 years ago

modifs pour template vectors

File size: 4.0 KB
RevLine 
[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]22namespace flair {
23namespace core {
[167]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>&);
[2]32
[167]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>&);
[2]40
[167]41template <typename T> Vector2D<T>::Vector2D(T inX, T inY) : x(inX), y(inY) {}
[2]42
[167]43template <typename T> Vector2D<T>::~Vector2D() {}
[2]44
[167]45template <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]51template <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]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);
[2]59}
60
[167]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);
[2]63}
64
[167]65template <typename T> Vector2D<T> operator-(const Vector2D<T> &vectorA) {
66 return Vector2D<T>(-vectorA.x, -vectorA.y);
[161]67}
68
[167]69template <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]77template <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]81template <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]85template <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]92template <typename T> void Vector2D<T>::RotateDeg(float value) { Rotate(Euler::ToRadian(value)); }
[2]93
[167]94template <typename T> float Vector2D<T>::GetNorm(void) const { return sqrt(x * x + y * y); }
[2]95
[167]96template <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]104template <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]116template <typename T> void Vector2D<T>::Saturate(float min, float max) {
[15]117  Saturate(Vector2D(min, min), Vector2D(max, max));
[2]118}
119
[167]120template <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]126template <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
Note: See TracBrowser for help on using the repository browser.