-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector2D.h
64 lines (48 loc) · 2.02 KB
/
Vector2D.h
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <cmath>
/*Reference: https://codereview.stackexchange.com/questions/26608/review-of-2d-vector-class */
/*The Vector2d class is an object consisting of simply an x and
y value. Certain operators are overloaded to make it easier
for vector math to be performed.*/
class Vector2d {
public:
/*The x and y values are public to give easier access for
outside funtions. Accessors and mutators are not really
necessary*/
double x;
double y;
//Constructor assigns the inputs to x and y.
Vector2d();
Vector2d(double, double);
/*The following operators simply return Vector2ds that
have operations performed on the relative (x, y) values*/
Vector2d operator+(const Vector2d&) const;
Vector2d operator-(const Vector2d&) const;
Vector2d operator*(const Vector2d&) const;
Vector2d operator/(const Vector2d&) const;
//Check if the Vectors have the same values.
bool operator==(const Vector2d&) const;
/*Check which Vectors are closer or further from the
origin.*/
bool operator>(const Vector2d&) const;
bool operator<(const Vector2d&) const;
bool operator>=(const Vector2d&) const;
bool operator<=(const Vector2d&) const;
//Negate both the x and y values.
Vector2d operator-() const;
//Apply scalar operations.
Vector2d operator*(const float&) const;
Vector2d operator/(const float&) const;
//Product functions
static double DotProduct(const Vector2d&, const Vector2d&);
static double CrossProduct(const Vector2d&, const Vector2d&);
//Returns the length of the vector from the origin.
double Magnitude();
//Return the unit vector of the input
static Vector2d Normal(Vector2d);
//Return a vector perpendicular to the left.
static Vector2d Perpendicular(const Vector2d&);
//Return true if two line segments intersect.
static bool Intersect(const Vector2d&, const Vector2d&, const Vector2d&, const Vector2d&);
//Return the point where two lines intersect.
static Vector2d GetIntersect(const Vector2d&, const Vector2d&, const Vector2d&, const Vector2d&);
};