summaryrefslogtreecommitdiffstats
path: root/kolf/vector.cpp
blob: f4c05262f49b3d0a2da71742d419365315a9bbbb (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <kdebug.h>

#include "vector.h"

// this and vector.h by Ryan Cummings

// Creates a vector with between two points
Vector::Vector(const QPoint &source, const QPoint &dest) {
	_magnitude = sqrt(pow(source.x() - dest.x(), 2) + pow(source.y() - dest.y(), 2));
	_direction = atan2(source.y() - dest.y(), source.x() - dest.x());
}

// Creates a vector with between two points
Vector::Vector(const Point &source, const Point &dest) {
	_magnitude = sqrt(pow(source.x - dest.x, 2) + pow(source.y - dest.y, 2));
	_direction = atan2(source.y - dest.y, source.x - dest.x);
}

// 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 efficiently)
void Vector::setComponents(double x, double y) {
	_direction = atan2(y, x);
	_magnitude = sqrt((x * x) + (y * y));
}

void debugPoint(const QString &text, const Point &p)
{
	kdDebug(12007) << text << " (" << p.x << ", " << p.y << ")" << endl;
}

void debugVector(const QString &text, const Vector &p)
{
	// debug degrees
	kdDebug(12007) << text << " (magnitude: " << p.magnitude() << ", direction: " << p.direction() << ", direction (deg): " << (360L / (2L * M_PI)) * p.direction() << ")" << endl;
}