summaryrefslogtreecommitdiffstats
path: root/kue/vector.cpp
blob: 40aed057947055368dec4a262f37a15eaa1d2ed3 (plain)
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
84
85
#include "vector.h"

// Creates a vector with between two points
vector::vector(const point &source, const point &dest) {
	_magnitude = source.distance(dest);
	_direction = source.angle(dest);
}

// Creates an empty vector
vector::vector() {
	_magnitude = 0.0;
	_direction = 0.0;
}

// Copy another vector object
vector::vector(const vector& v) {
	_magnitude = v._magnitude;
	_direction = v._direction;
}

// Set the X component
void vector::setComponentX(double x) {
	setComponents(x, componentY());
}

// Set the Y component
void vector::setComponentY(double y) {
	setComponents(componentX(), y);
}

// Operations with another vector performs vector math
vector vector::operator+(const vector& v) {
	double x = componentX() + v.componentX();
	double y = componentY() + v.componentY();

	return vector(sqrt((x * x) + (y * y)), atan2(y, x));	
}

vector vector::operator-(const vector& v) {
	double x = componentX() - v.componentX();
	double y = componentY() - v.componentY();

	return vector(sqrt((x * x) + (y * y)), atan2(y, x));	
}

vector& vector::operator+=(const vector& v) {
	setComponents(componentX() + v.componentX(), componentY() + v.componentY());
	return *this;
}

vector& vector::operator-=(const vector& v) {
	setComponents(componentX() - v.componentX(), componentY() - v.componentY());
	return *this;
}

double vector::operator*(const vector& v) {
	return ((componentX() * v.componentX()) + (componentY() * v.componentY()));
}

// Operations with a single double value affects the magnitude
vector& vector::operator+= (double m) {
	_magnitude += m;
	return *this;
}

vector& vector::operator-= (double m) {
	_magnitude -= m;
	return *this;
}

vector& vector::operator*= (double m) {
	_magnitude *= m;
	return *this;
}

vector& vector::operator/= (double m) {
	_magnitude /= m;
	return *this;
}

// Sets both components at once (the only way to do it efficently)
void vector::setComponents(double x, double y) {
	_direction = atan2(y, x);
	_magnitude = sqrt((x * x) + (y * y));
}