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
|
#ifndef KOLF_VECTOR_H
#define KOLF_VECTOR_H
#include <math.h>
#include <qpoint.h>
class Point
{
public:
Point(double _x, double _y)
{
x = _x;
y = _y;
}
Point()
{
x = 0;
y = 0;
}
double x;
double y;
};
void debugPoint(const QString &, const Point &);
// This and vector.cpp by Ryan Cummings
// Implements a vector in 2D
class Vector {
public:
// Normal constructors
Vector(double magnitude, double direction) { _magnitude = magnitude; _direction = direction; }
Vector(const QPoint& source, const QPoint& dest);
Vector(const Point& source, const Point& dest);
Vector();
// Copy constructor
Vector(const Vector&);
// Accessors, sorta
double componentX() const { return (_magnitude * cos(_direction)); };
double componentY() const { return (_magnitude * sin(_direction)); };
// Sets individual components
// Wrappers around setComponents(double, double) - below
void setComponentX(double x);
void setComponentY(double y);
// Sets both components at once
void setComponents(double x, double y);
// Accessors
double magnitude() const { return _magnitude; }
double direction() const { return _direction; }
void setMagnitude(double m) { _magnitude = m; }
void setDirection(double d) { _direction = d; }
// Vector math
Vector operator+(const Vector&);
Vector operator-(const Vector&);
Vector& operator+=(const Vector&);
Vector& operator-=(const Vector&);
// Dot product
double operator*(const Vector&);
// Magnitude math
Vector operator+(double m) { return Vector(_magnitude + m, _direction); }
Vector operator-(double m) { return Vector(_magnitude - m, _direction); }
Vector operator*(double m) { return Vector(_magnitude * m, _direction); }
Vector operator/(double m) { return Vector(_magnitude / m, _direction); }
Vector& operator+=(double m);
Vector& operator-=(double m);
Vector& operator*=(double m);
Vector& operator/=(double m);
// Return the vector's equalivent on the unit circle
Vector unit() const { return Vector(1.0, _direction); }
protected:
double _magnitude;
double _direction;
};
void debugVector(const QString &, const Vector &);
#endif
|