-
Notifications
You must be signed in to change notification settings - Fork 0
/
twoVector.cpp
55 lines (40 loc) · 895 Bytes
/
twoVector.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
#include "twoVector.hpp"
// Constructor.
TwoVector::TwoVector(double xVal, double yVal) {
x = xVal;
y = yVal;
}
double TwoVector::mag() {
return std::sqrt(x*x + y*y);
}
// Operator overloads:
// Dot-product.
double TwoVector::operator*(const TwoVector& v) {
return x * v.x + y * v.y;
}
// Multiplication by a scalar.
TwoVector TwoVector::operator*(const double& a) {
TwoVector result;
result.x = a * x;
result.y = a * y;
return result;
}
TwoVector operator*(const double& a, const TwoVector& v) {
TwoVector result;
result.x = a * v.x;
result.y = a * v.y;
return result;
}
// Vector addition/subtraction.
TwoVector TwoVector::operator+(const TwoVector& v) {
TwoVector result;
result.x = x + v.x;
result.y = y + v.y;
return result;
}
TwoVector TwoVector::operator-(const TwoVector& v) {
TwoVector result;
result.x = x - v.x;
result.y = y - v.y;
return result;
}