All Classes Namespaces Files Functions Variables Typedefs Pages
Vector2D.h
Go to the documentation of this file.
1 /*
2  * Vector2D.h
3  *
4  *
5  * All rights are retained by the authors and the University of Minnesota.
6  * Please contact sjguy@cs.umn.edu for licensing inquiries.
7  *
8  * Authors: Ioannis Karamouzas, Brian Skinner, and Stephen J. Guy
9  * Contact: ioannis@cs.umn.edu
10  */
11 
17 #pragma once
18 #include <iostream>
19 #define _USE_MATH_DEFINES
20 #include <math.h>
21 
22 namespace TTC {
28 class Vector2D {
29 
30  public:
32  float x;
34  float y;
35 
37  inline Vector2D() { x = 0; y = 0; }
38 
42  inline Vector2D(const Vector2D& v) { x = v.x; y = v.y; }
43 
48  inline Vector2D(float _x, float _y) { x = _x; y = _y; }
49 
54  inline Vector2D operator+(const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); }
55 
60  inline Vector2D operator-(const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
61 
66  inline float operator*(const Vector2D& v) const { return x * v.x + y * v.y; }
67 
71  inline Vector2D operator*(const float a) const { return Vector2D(x * a, y * a); }
72 
76  inline Vector2D operator/(const float a) const { return Vector2D(x / a, y / a); }
77 
82  inline float operator^(const Vector2D& v) const { return x*v.y - y*v.x;}
83 
87  inline void operator+=(const Vector2D& v) { x += v.x; y += v.y;}
88 
92  inline void operator-=(const Vector2D& v) { x -= v.x; y -= v.y; }
93 
97  inline void operator*=(const float& a) { x *= a; y *= a;}
98 
102  inline void operator/=(const float& a) { x /= a; y /= a;}
103 
108  inline bool operator==(const Vector2D& v) const { return (x == v.x && y == v.y); }
109 
114  inline bool operator!=(const Vector2D& v) const { return (x != v.x || y != v.y); }
115 
117  inline void normalize() { float d=sqrtf(x*x+y*y); if(d>0) { x/=d; y/=d; }}
118 
120  inline float length() const { return sqrtf(x*x+y*y); }
121 
123  inline float lengthSqr() const { return x*x+y*y; }
124 
126  inline Vector2D perpendicular() const { return Vector2D(-y, x); }
127 
128  };
129 
130 
136  inline std::ostream& operator << (std::ostream& os, const Vector2D& v) {
137  //return os << v.x << " " << v.y ;
138  return os << "(" << v.x << "," << v.y << ")";
139  }
140 
145  inline Vector2D normalize(const Vector2D& v) { float d=sqrtf(v.x*v.x+v.y*v.y); if(d>0) return v/d; return v;}
146 
152  inline float dot(const Vector2D& v1, const Vector2D& v2) { return v1.x*v2.x + v1.y*v2.y ; }
153 
159  inline float det(const Vector2D& v1, const Vector2D& v2) { return v1.x*v2.y - v1.y*v2.x; }
160 
166  inline Vector2D operator*(const float a, const Vector2D &v) {return Vector2D(v.x * a, v.y * a);}
167 
172  inline void clamp (Vector2D &v, float maxValue) {
173  float lengthV = v.length();
174  if(lengthV > maxValue)
175  {
176  float mult = (maxValue / lengthV);
177  v.x *= mult;
178  v.y *= mult;
179  }
180  }
181 }
Vector2D(float _x, float _y)
Constructor.
Definition: Vector2D.h:48
bool operator==(const Vector2D &v) const
Vector equality.
Definition: Vector2D.h:108
void operator/=(const float &a)
Divides the vector by a scalar.
Definition: Vector2D.h:102
void normalize()
Definition: Vector2D.h:117
Vector2D(const Vector2D &v)
Copy constructor.
Definition: Vector2D.h:42
Vector2D()
Definition: Vector2D.h:37
float dot(const Vector2D &v1, const Vector2D &v2)
The dot product between two vectors.
Definition: Vector2D.h:152
float y
Definition: Vector2D.h:34
Vector2D operator*(const float a) const
Scalar multiplication.
Definition: Vector2D.h:71
Vector2D operator/(const float a) const
Scalar division.
Definition: Vector2D.h:76
Vector2D normalize(const Vector2D &v)
Normalization of a vector.
Definition: Vector2D.h:145
void operator-=(const Vector2D &v)
Subtracts a 2d vector from the current vector.
Definition: Vector2D.h:92
Vector2D operator-(const Vector2D &v) const
Vector subtraction.
Definition: Vector2D.h:60
float det(const Vector2D &v1, const Vector2D &v2)
The cross product between two vectors.
Definition: Vector2D.h:159
bool operator!=(const Vector2D &v) const
Vector inequality.
Definition: Vector2D.h:114
float operator*(const Vector2D &v) const
Dot product.
Definition: Vector2D.h:66
Vector2D operator+(const Vector2D &v) const
Vector addition.
Definition: Vector2D.h:54
float length() const
Definition: Vector2D.h:120
void operator+=(const Vector2D &v)
Adds a 2d vector to the current vector.
Definition: Vector2D.h:87
void clamp(Vector2D &v, float maxValue)
Caps the magnitude of a vector to a maximum value.
Definition: Vector2D.h:172
void operator*=(const float &a)
Multiplies the vector by a scalar.
Definition: Vector2D.h:97
Vector2D operator*(const float a, const Vector2D &v)
Scalar multiplication (left operator)
Definition: Vector2D.h:166
float x
Definition: Vector2D.h:32
A two-dimensional vector class.
Definition: Vector2D.h:28
Vector2D perpendicular() const
Definition: Vector2D.h:126
std::ostream & operator<<(std::ostream &os, const Vector2D &v)
Writes a vector to an output stream.
Definition: Vector2D.h:136
float lengthSqr() const
Definition: Vector2D.h:123
float operator^(const Vector2D &v) const
Cross product.
Definition: Vector2D.h:82