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