diff options
Diffstat (limited to 'src/vector3f.cc')
-rw-r--r-- | src/vector3f.cc | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/src/vector3f.cc b/src/vector3f.cc new file mode 100644 index 0000000..6d42742 --- /dev/null +++ b/src/vector3f.cc @@ -0,0 +1,157 @@ +/* + vector3f.cc + This file is part of the Project::OSiRiON world editor + and is distributed under the terms and conditions of + the GNU General Public License version 2 +*/ + +#include <cmath> +#include "vector3f.h" + +namespace editor +{ + +Vector3f::Vector3f() +{ + clear(); +} + +Vector3f::Vector3f(const Vector3f &other) +{ + assign(other); +} + +Vector3f::Vector3f(const float x, const float y, const float z) +{ + assign(x, y, z); +} + +Vector3f::~Vector3f() +{ +} + +void Vector3f::clear() +{ + coord[0] = 0; + coord[1] = 0; + coord[2] = 0; +} + +void Vector3f::assign(const float x, const float y, const float z) +{ + coord[0] = x; + coord[1] = y; + coord[2] = z; +} + +void Vector3f::assign(const Vector3f & other) +{ + coord[0] = other.coord[0]; + coord[1] = other.coord[1]; + coord[2] = other.coord[2]; +} + +Vector3f & Vector3f::operator=(const Vector3f & other) +{ + assign(other); + return (*this); +} + +Vector3f & Vector3f::operator*=(const float scalar) +{ + for (int i = 0; i < 3; i++) + coord[i] *= scalar; + return (*this); +} + +Vector3f & Vector3f::operator/=(const float scalar) +{ + for (int i = 0; i < 3; i++) + coord[i] /= scalar; + return (*this); +} + +Vector3f &Vector3f::operator-=(const Vector3f & other) +{ + for (int i = 0; i < 3; i++) + coord[i] -= other[i]; + return (*this); +} + +Vector3f &Vector3f::operator+=(const Vector3f &other) +{ + for (int i = 0; i < 3; i++) + coord[i] += other[i]; + return (*this); +} + +bool Vector3f::operator==(const Vector3f& other) const +{ + for (int i = 0; i < 3; i++) + if (coord[i] != other.coord[i]) + return (false); + return (true); +} + +float Vector3f::lengthsquared() const +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += coord[i] * coord[i]; + return ((float) r); +} + +float Vector3f::length() const +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += coord[i] * coord[i]; + + return (sqrtf(r)); +} + +void Vector3f::normalize() +{ + (*this) /= this->length(); +} + +Vector3f operator*(float scalar, const Vector3f &vector) +{ + return vector * scalar; +} + +const Vector3f crossproduct(const Vector3f & first, const Vector3f &second) +{ + float vx = first[1] * second[2] - first[2] * second[1]; + float vy = first[2] * second[0] - first[0] * second[2]; + float vz = first[0] * second[1] - first[1] * second[0]; + + return(Vector3f(vx, vy, vz)); +} + +float dotproduct(const Vector3f& first, const Vector3f& second) +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += first[i] * second[i]; + return (r); +} + +float distance(const Vector3f& first, const Vector3f& second) +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += (first[i] - second[i]) * (first[i] - second[i]); + + return (sqrtf(r)); +} + +float distancesquared(const Vector3f& first, const Vector3f& second) +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += (first[i] - second[i]) * (first[i] - second[i]); + return (r); +} + +} // namespace math |