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