summaryrefslogtreecommitdiff
path: root/vector.cpp
blob: 29fbdc7ba84450ef3b59696f796a62973640332d (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "vector.h"

#include <boost/format.hpp>

#include <cmath>
#include <string>

Vector2::Vector2() {
	x = y = 0;
}

Vector2::Vector2(const Vector2& v) {
	x = v.x;
	y = v.y;
}

Vector2::Vector2(float x, float y) {
	this->x = x;
	this->y = y;
}

bool Vector2::operator==(const Vector2& v) const {
	return x == v.x && y == v.y;
}

Vector2& Vector2::operator+=(const Vector2& v) {
	x += v.x;
	y += v.y;
	return *this;
}

Vector2 Vector2::operator+(const Vector2& v) {
	return Vector2(*this) += v;
}

Vector2& Vector2::operator-=(const Vector2& v) {
	x -= v.x;
	y -= v.y;
	return *this;
}

Vector2 Vector2::operator-(const Vector2& v) {
	return Vector2(*this) -= v;
}

Vector2& Vector2::operator*=(const float f) {
	x *= f;
	y *= f;
	return *this;
}

Vector2& Vector2::operator/=(const float f) {
	x /= f;
	y /= f;
	return *this;
}

float Vector2::length() {
	return sqrtf(x*x + y*y);
}

std::string Vector2::str() {
	return (boost::format("[%.2f %.2f]") % x % y).str();
}

/**
 * Vector3
 */

Vector3::Vector3() {
	x = y = z = 0;
}

Vector3::Vector3(const Vector3& v) : Vector2(v) {
	z = v.z;
}

Vector3::Vector3(float x, float y, float z) : Vector2(x, y) {
	this->z = z;
}

bool Vector3::operator==(const Vector3& v) {
	return x == v.x && y == v.y && z == v.z;
}

bool Vector3::operator!=(const Vector3& v) {
	return !(*this == v);
}

Vector3& Vector3::operator+=(const Vector3& v) {
	x += v.x;
	y += v.y;
	z += v.z;
	return *this;
}

Vector3 Vector3::operator+(const Vector3& v) {
	return Vector3(*this) += v;
}

Vector3& Vector3::operator-=(const Vector3& v) {
	x -= v.x;
	y -= v.y;
	z -= v.z;
	return *this;
}

Vector3 Vector3::operator-(const Vector3& v) {
	return Vector3(*this) -= v;
}

Vector3& Vector3::operator*=(const float f) {
	x *= f;
	y *= f;
	z *= f;
	return *this;
}

Vector3 Vector3::operator*(const float f) {
	return Vector3(*this) *= f;
}

Vector3& Vector3::operator/=(const float f) {
	x /= f;
	y /= f;
	z /= f;
	return *this;
}

Vector3 Vector3::operator/(const float f) {
	return Vector3(*this) /= f;
}

Vector3 Vector3::cross(const Vector3& v) {
	return Vector3(y*v.z - z*v.y,
			z*v.x - x*v.z,
			x*v.y - y*v.x);
}

float Vector3::dot(const Vector3& v) {
	return x*v.x + y*v.y + z*v.z;
}

Vector2 Vector3::xz() {
	return Vector2(x, z);
}

float Vector3::length() {
	return sqrtf(x*x + y*y + z*z);
}

std::string Vector3::str() {
	return (boost::format("[%.2f %.2f %.2f]") % x % y % z).str();
}