summaryrefslogtreecommitdiffstats
path: root/kue/vector.h
blob: f1a0947ac436818843d5a1418c53175fcc4c96c9 (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
#ifndef _VECTOR_H
#define _VECTOR_H

#include <math.h>
#include "point.h"

// Implements a vector in 2D
class vector {
  public:
	// Normal constructors
	vector(double magnitude, double direction) { _magnitude = magnitude; _direction = direction; }
	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;
};

#endif