-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector2d.cpp
83 lines (70 loc) · 1.13 KB
/
vector2d.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "vector2d.h"
#include <cmath>
Vector2d::Vector2d()
{
x = 0;
y = 0;
}
Vector2d::Vector2d(double x, double y)
{
this->x = x;
this->y = y;
}
double Vector2d::getX()
{
return x;
}
double Vector2d::getY()
{
return y;
}
void Vector2d::setX(double x)
{
this->x = x;
}
void Vector2d::setY(double y)
{
this->y = y;
}
Vector2d Vector2d::operator=(Vector2d b)
{
this->x = b.x;
this->y = b.y;
}
Vector2d sub(Vector2d a, Vector2d b)
{
Vector2d v;
v.setX(a.getX() - b.getX());
v.setY(a.getY() - b.getY());
return v;
}
Vector2d add(Vector2d a, Vector2d b)
{
Vector2d v;
v.setX(a.getX() + b.getX());
v.setY(a.getY() + b.getY());
return v;
}
Vector2d mult(Vector2d a, double b)
{
Vector2d v;
v.setX(a.getX() * b);
v.setY(a.getY() * b);
return v;
}
Vector2d normalize(Vector2d a)
{
Vector2d v;
double length = sqrt((a.getX() * a.getX()) + (a.getY() * a.getY()));
v.setX(a.getX() / length);
v.setY(a.getY() / length);
return v;
}
double dotProduct(Vector2d a, Vector2d b)
{
return (a.getX() * b.getX()) + (a.getY() * b.getY());
}
double length(Vector2d a)
{
return sqrt((a.getX() * a.getX()) + (a.getY() * a.getY()));
}