summaryrefslogtreecommitdiff
path: root/vector.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-06-01 19:02:52 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-06-01 19:02:52 +0200
commitdc104e37842049e040f7e39f4e1aab56cde1488c (patch)
tree7a2b98b1007fe7325752ae5a8e728d1410072dfb /vector.cpp
Moved common files here.
Diffstat (limited to 'vector.cpp')
-rw-r--r--vector.cpp150
1 files changed, 150 insertions, 0 deletions
diff --git a/vector.cpp b/vector.cpp
new file mode 100644
index 0000000..cf2c164
--- /dev/null
+++ b/vector.cpp
@@ -0,0 +1,150 @@
+#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;
+}
+
+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();
+}