vector2D.h 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* Crowd Simulator Engine
** Copyright (C) 2018 - Inria Rennes - Rainbow - Julien Pettre
** 
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**
**
**
** Authors: Axel Lopez Gandia, Javad Amirian, Florian Berton,
** Julien Legros, Lucas Pelerin, Beatriz Cabrero Daniel, Fabien Grzeskowiak
** 
** Contact: crowd_group@inria.fr
*/

26 27 28 29 30
#ifndef LIB_VECTOR2D_H
#define LIB_VECTOR2D_H

#include <math.h>

LEGROS Julien's avatar
LEGROS Julien committed
31
class Vector2D {
32
 private:
33 34
  float x_;
  float y_;
35

36 37
 public:

LEGROS Julien's avatar
LEGROS Julien committed
38
  Vector2D() { 
39 40
    x_ = 0.0; 
    y_ = 0.0;
LEGROS Julien's avatar
LEGROS Julien committed
41
  }
42

LEGROS Julien's avatar
LEGROS Julien committed
43
  Vector2D(float xx, float yy) {
44 45
    x_ = xx;
    y_ = yy;
LEGROS Julien's avatar
LEGROS Julien committed
46
  }
47

LEGROS Julien's avatar
LEGROS Julien committed
48
  Vector2D(Vector2D * v) {
49 50
    this->x_ = v->x_;
    this->y_ = v->y_;
LEGROS Julien's avatar
LEGROS Julien committed
51
  }
52

LEGROS Julien's avatar
LEGROS Julien committed
53
  inline float magnitude() const { return sqrt(x_ * x_ + y_ * y_); }
54

LEGROS Julien's avatar
LEGROS Julien committed
55
  inline float sqrMagnitude() const { return x_ * x_ + y_ * y_; }
56

LEGROS Julien's avatar
LEGROS Julien committed
57
  void normalize() { 
LEGROS Julien's avatar
LEGROS Julien committed
58
    float mag = magnitude();
Javad's avatar
Javad committed
59 60
    x_ /= mag;
    y_ /= mag;
LEGROS Julien's avatar
LEGROS Julien committed
61
  }
62 63 64 65 66
  Vector2D getnormalized() {
	  Vector2D Result(x_, y_);
	  Result.normalize();
	  return Result;
  }
LEGROS Julien's avatar
LEGROS Julien committed
67
  float dot(Vector2D * v) { return this->x_ * v->x_ + this->y_ * v->y_;}
Javad's avatar
Javad committed
68
  float dot(Vector2D v) { return this->x_ * v.x_ + this->y_ * v.y_; }
69

LEGROS Julien's avatar
LEGROS Julien committed
70
  void set(float x, float y) {
71 72
    this->x_ = x;
    this->y_ = y;
LEGROS Julien's avatar
LEGROS Julien committed
73
  }
74

75 76 77 78 79 80 81
  void setX(float x) {
      this->x_ = x;
  }
  void setY(float y) {
      this->y_ = y;
  }

82
  inline float x() const{ return x_; }
83

84
  inline float y() const{ return y_; }
85 86 87 88 89 90 91 92 93

  Vector2D& operator+=(const Vector2D& rhs) {
	  x_ += rhs.x(); y_ += rhs.y();
	  return *this;
  }
  Vector2D& operator-=(const Vector2D& rhs) {
	  x_ -= rhs.x(); y_ -= rhs.y();
	  return *this;
  }
94 95
};

Javad's avatar
Javad committed
96 97 98 99 100 101 102 103
Vector2D operator-(Vector2D lhs, Vector2D rhs);
Vector2D operator+(Vector2D lhs, Vector2D rhs);
Vector2D operator*(float lhs, Vector2D rhs);
Vector2D operator*(Vector2D lhs, float rhs);
Vector2D operator/(Vector2D lhs, float rhs);
Vector2D operator-(Vector2D lhs);
double norm(Vector2D x);

104

LEGROS Julien's avatar
LEGROS Julien committed
105
#endif // LIB_VECTOR2D_H