#include #include "vector3.h" const float EPSILON = 0.001f; vector3::vector3() : x(0), y(0), z(0) {} vector3::vector3(vector3& v) : x(v.x), y(v.y), z(v.z) {} vector3::vector3(float _x, float _y, float _z) : x(_x),y(_y),z(_z){} // присваиваение векторов vector3& vector3::operator= (vector3& v) { x = v.x; y = v.y; z = v.z; return *this; } // сравнение векторов bool vector3::operator== (vector3& v) { if (fabs(x-v.x) < EPSILON) if (fabs(y-v.y) < EPSILON) if (fabs(z-v.z) < EPSILON) return true; return false; } // величина вектора (от magnitude - величина) float vector3::mag() { return sqrt(x*x+y*y+z*z); } // отрицательный вектор (унарная операция!!!) vector3 vector3::operator- () { return vector3(-x,-y,-z); } // сложение векторов vector3 vector3::operator+ (vector3& v) { return vector3(x+v.x,y+v.y,z+v.z); } // разность векторов vector3 vector3::operator- (vector3& v) { return vector3(x-v.x,y-v.y,z-v.z); } // умножение вектора на скаляр vector3 vector3::operator* (float& a) { return vector3(x*a,y*a,z*a); } // деление вектора на скаляр vector3 vector3::operator/ (float& a) { float b = 1.0f / a; return vector3 (x*b,y*b,z*b); } // скалярное произедение векторов (dot product) float vector3::operator* (vector3& v) { return x*v.x+y*v.y+z*v.z; } // векторное произведение векторов (cross product) vector3 vector3::cross(vector3& v) { return vector3(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); } vector3& vector3::operator+= (vector3& v) { x += v.x; y += v.y; z += v.z; return *this; } vector3& vector3::operator-= (vector3& v) { x -= v.x; y -= v.y; z -= v.z; return *this; } vector3& vector3::operator*= (float& a) { x *= a; y *= a; z *= a; return *this; } vector3& vector3::operator/= (float& a) { float b = 1.0f/a; x *= b; y *= b; z *= b; return *this; } // нормирование вектора void vector3::normalize () { float magnitude = mag(); if (magnitude > 0) { float invertedMag = 1 / magnitude; x *= invertedMag; y *= invertedMag; z *= invertedMag; } } // расстояние между двумя точками (не векторами!!!) float vector3::distance (vector3& v) { float dx = x - v.x; float dy = y - v.y; float dz = z - v.z; return sqrt(dx*dx + dy*dy + dz*dz); }