#include "SFVec2f.h" #include "SFPoint2f.h" #include "SFString.h" #include using namespace X3DTK; using namespace std; const SFVec2f SFVec2f::null(0.0f, 0.0f); // constructors, destructor SFVec2f::SFVec2f() : x(0.0f), y(0.0f) { } SFVec2f::SFVec2f(float x, float y) { this->x = x; this->y = y; } SFVec2f::SFVec2f(const SFVec2f &v) { x = v.x; y = v.y; } SFVec2f &SFVec2f::operator= (const SFVec2f &v) { x = v.x; y = v.y; return *this; } SFVec2f &SFVec2f::operator+= (const SFVec2f &v) { x += v.x; y += v.y; return *this; } SFVec2f &SFVec2f::operator-= (const SFVec2f &v) { x -= v.x; y -= v.y; return *this; } SFVec2f::SFVec2f(const SFString &s) { istringstream iss(s, istringstream::in); iss >> x >> y; } SFVec2f::SFVec2f(const SFPoint2f &P) { x = P.x; y = P.y; } namespace X3DTK { // operations on vectors //overloading of the operators +, -, * bool operator== (const SFVec2f &v1, const SFVec2f &v2) { return ((v1.x == v2.x) && (v1.y == v2.y)); } bool operator!= (const SFVec2f &v1, const SFVec2f &v2) { return ((v1.x != v2.x) || (v1.y != v2.y)); } SFVec2f operator+ (const SFVec2f &v1, const SFVec2f &v2) { SFVec2f res; res.x = v1.x + v2.x; res.y = v1.y + v2.y; return res; } SFVec2f operator- (const SFVec2f &v1, const SFVec2f &v2) { SFVec2f res; res.x = v1.x - v2.x; res.y = v1.y - v2.y; return res; } SFVec2f operator- (const SFVec2f &v) { SFVec2f res; res.x = -v.x; res.y = -v.y; return res; } //scalar product float operator* (const SFVec2f &v1, const SFVec2f &v2) { return v1.x * v2.x + v1.y * v2.y; } SFVec2f operator* (const float a, const SFVec2f &v) { SFVec2f res; res.x = a * v.x; res.y = a * v.y; return res; } } //norm2 float SFVec2f::norm() const { return (float)sqrt(x * x + y * y); } SFVec2f SFVec2f::normalize() { float r = sqrtf(x * x + y * y); if (r != 0.0f) { x /= r; y /= r; } return *this; } SFVec2f SFVec2f::normalized() const { float r = sqrtf(x * x + y * y); if (r != 0.0f) return SFVec2f(x/r, y/r); return SFVec2f(0.0f, 0.0f); } SFString SFVec2f::toSFString() const { return "\"" + SFString::number(x) + SFString(" ") + SFString::number(y) + "\""; }