| 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 { namespace core {
|
---|
| 23 |
|
---|
| 24 | Vector2D::Vector2D(float inX,float inY): x(inX),y(inY) {
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | Vector2D::~Vector2D() {
|
---|
| 28 |
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | Vector2D &Vector2D::operator=(const Vector2D &vector) {
|
---|
| 32 | x=vector.x;
|
---|
| 33 | y=vector.y;
|
---|
| 34 | return (*this);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | Vector2D operator+ (const Vector2D &vectorA,const Vector2D &vectorB) {
|
---|
| 38 | return Vector2D(vectorA.x + vectorB.x,vectorA.y + vectorB.y);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | Vector2D operator- (const Vector2D &vectorA,const Vector2D &vectorB) {
|
---|
| 42 | return Vector2D(vectorA.x - vectorB.x,vectorA.y - vectorB.y);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | Vector2D operator/ (const Vector2D &vector, float coeff) {
|
---|
| 46 | if(coeff!=0) {
|
---|
| 47 | return Vector2D(vector.x/ coeff,vector.y/ coeff);
|
---|
| 48 | } else {
|
---|
| 49 | return Vector2D(0,0);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | Vector2D operator * (const Vector2D &vector, float coeff) {
|
---|
| 54 | return Vector2D(vector.x * coeff,vector.y * coeff);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | Vector2D operator * (float coeff,const Vector2D &vector) {
|
---|
| 58 | return Vector2D(vector.x * coeff,vector.y * coeff);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void Vector2D::Rotate(float value) {
|
---|
| 62 | float xTmp;
|
---|
| 63 | xTmp=x*cosf(value)-y*sinf(value);
|
---|
| 64 | y=x*sinf(value)+y*cosf(value);
|
---|
| 65 | x=xTmp;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | void Vector2D::RotateDeg(float value) {
|
---|
| 69 | Rotate(Euler::ToRadian(value));
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | float Vector2D::GetNorm(void) const {
|
---|
| 73 | return sqrt(x*x+y*y);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void Vector2D::Normalize(void) {
|
---|
| 77 | float n=GetNorm();
|
---|
| 78 | if(n!=0) {
|
---|
| 79 | x=x/n;
|
---|
| 80 | y=y/n;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void Vector2D::Saturate(Vector2D min,Vector2D max) {
|
---|
| 85 | if(x<min.x) x=min.x;
|
---|
| 86 | if(x>max.x) x=max.x;
|
---|
| 87 |
|
---|
| 88 | if(y<min.y) y=min.y;
|
---|
| 89 | if(y>max.y) y=max.y;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | void Vector2D::Saturate(float min,float max) {
|
---|
| 93 | Saturate(Vector2D(min,min),Vector2D(max,max));
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | void Vector2D::Saturate(const Vector2D &value) {
|
---|
| 97 | float x=fabs(value.x);
|
---|
| 98 | float y=fabs(value.y);
|
---|
| 99 | Saturate(Vector2D(-x,-y),Vector2D(x,y));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void Vector2D::Saturate(float value) {
|
---|
| 103 | float sat=fabs(value);
|
---|
| 104 | Saturate(Vector2D(-sat,-sat),Vector2D(sat,sat));
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | } // end namespace core
|
---|
| 108 | } // end namespace flair
|
---|