// Copyright (C) 2015 Jérôme Leclercq // This file is part of the "Nazara Engine - Mathematics module" // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include #include #include #include #define F(a) static_cast(a) namespace Nz { template Vector2::Vector2(T X, T Y) { Set(X, Y); } template Vector2::Vector2(T scale) { Set(scale); } template Vector2::Vector2(const T vec[2]) { Set(vec); } template template Vector2::Vector2(const Vector2& vec) { Set(vec); } template Vector2::Vector2(const Vector3& vec) { Set(vec); } template Vector2::Vector2(const Vector4& vec) { Set(vec); } template T Vector2::AbsDotProduct(const Vector2& vec) const { return std::abs(x * vec.x) + std::abs(y * vec.y); } template T Vector2::AngleBetween(const Vector2& vec) const { return FromRadians(std::atan2(vec.y, vec.x) - std::atan2(y, x)); } template T Vector2::Distance(const Vector2& vec) const { return std::sqrt(SquaredDistance(vec)); } template float Vector2::Distancef(const Vector2& vec) const { return std::sqrt(static_cast(SquaredDistance(vec))); } template T Vector2::DotProduct(const Vector2& vec) const { return x*vec.x + y*vec.y; } template T Vector2::GetLength() const { return std::sqrt(GetSquaredLength()); } template float Vector2::GetLengthf() const { return std::sqrt(static_cast(GetSquaredLength())); } template Vector2 Vector2::GetNormal(T* length) const { Vector2 vec(*this); vec.Normalize(length); return vec; } template T Vector2::GetSquaredLength() const { return x*x + y*y; } template Vector2& Vector2::MakeUnit() { return Set(F(1.0), F(1.0)); } template Vector2& Vector2::MakeUnitX() { return Set(F(1.0), F(0.0)); } template Vector2& Vector2::MakeUnitY() { return Set(F(0.0), F(1.0)); } template Vector2& Vector2::MakeZero() { return Set(F(0.0), F(0.0)); } template Vector2& Vector2::Maximize(const Vector2& vec) { if (vec.x > x) x = vec.x; if (vec.y > y) y = vec.y; return *this; } template Vector2& Vector2::Minimize(const Vector2& vec) { if (vec.x < x) x = vec.x; if (vec.y < y) y = vec.y; return *this; } template Vector2& Vector2::Normalize(T* length) { T norm = GetLength(); if (norm > F(0.0)) { T invNorm = F(1.0) / norm; x *= invNorm; y *= invNorm; } if (length) *length = norm; return *this; } template Vector2& Vector2::Set(T X, T Y) { x = X; y = Y; return *this; } template Vector2& Vector2::Set(T scale) { x = scale; y = scale; return *this; } template Vector2& Vector2::Set(const T vec[2]) { std::memcpy(&x, vec, 2*sizeof(T)); return *this; } template Vector2& Vector2::Set(const Vector2& vec) { std::memcpy(this, &vec, sizeof(Vector2)); return *this; } template template Vector2& Vector2::Set(const Vector2& vec) { x = F(vec.x); y = F(vec.y); return *this; } template Vector2& Vector2::Set(const Vector3& vec) { x = vec.x; y = vec.y; return *this; } template Vector2& Vector2::Set(const Vector4& vec) { x = vec.x; y = vec.y; return *this; } template T Vector2::SquaredDistance(const Vector2& vec) const { return (*this - vec).GetSquaredLength(); } template String Vector2::ToString() const { StringStream ss; return ss << "Vector2(" << x << ", " << y << ')'; } template Vector2::operator T*() { return &x; } template Vector2::operator const T*() const { return &x; } template const Vector2& Vector2::operator+() const { return *this; } template Vector2 Vector2::operator-() const { return Vector2(-x, -y); } template Vector2 Vector2::operator+(const Vector2& vec) const { return Vector2(x + vec.x, y + vec.y); } template Vector2 Vector2::operator-(const Vector2& vec) const { return Vector2(x - vec.x, y - vec.y); } template Vector2 Vector2::operator*(const Vector2& vec) const { return Vector2(x * vec.x, y * vec.y); } template Vector2 Vector2::operator*(T scale) const { return Vector2(x * scale, y * scale); } template Vector2 Vector2::operator/(const Vector2& vec) const { #if NAZARA_MATH_SAFE if (NumberEquals(vec.x, F(0.0)) || NumberEquals(vec.y, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return Vector2(x / vec.x, y / vec.y); } template Vector2 Vector2::operator/(T scale) const { #if NAZARA_MATH_SAFE if (NumberEquals(scale, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return Vector2(x / scale, y / scale); } template Vector2& Vector2::operator+=(const Vector2& vec) { x += vec.x; y += vec.y; return *this; } template Vector2& Vector2::operator-=(const Vector2& vec) { x -= vec.x; y -= vec.y; return *this; } template Vector2& Vector2::operator*=(const Vector2& vec) { x *= vec.x; y *= vec.y; return *this; } template Vector2& Vector2::operator*=(T scale) { x *= scale; y *= scale; return *this; } template Vector2& Vector2::operator/=(const Vector2& vec) { #if NAZARA_MATH_SAFE if (NumberEquals(vec.x, F(0.0)) || NumberEquals(vec.y, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif x /= vec.x; y /= vec.y; return *this; } template Vector2& Vector2::operator/=(T scale) { #if NAZARA_MATH_SAFE if (NumberEquals(scale, F(0.0))) { String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif x /= scale; y /= scale; return *this; } template bool Vector2::operator==(const Vector2& vec) const { return NumberEquals(x, vec.x) && NumberEquals(y, vec.y); } template bool Vector2::operator!=(const Vector2& vec) const { return !operator==(vec); } template bool Vector2::operator<(const Vector2& vec) const { if (x == vec.x) return y < vec.y; else return x < vec.x; } template bool Vector2::operator<=(const Vector2& vec) const { if (x == vec.x) return y <= vec.y; else return x < vec.x; } template bool Vector2::operator>(const Vector2& vec) const { return !operator<=(vec); } template bool Vector2::operator>=(const Vector2& vec) const { return !operator<(vec); } template Vector2 Vector2::Lerp(const Vector2& from, const Vector2& to, T interpolation) { return Lerp(from, to, interpolation); } template Vector2 Vector2::Unit() { Vector2 vector; vector.MakeUnit(); return vector; } template Vector2 Vector2::UnitX() { Vector2 vector; vector.MakeUnitX(); return vector; } template Vector2 Vector2::UnitY() { Vector2 vector; vector.MakeUnitY(); return vector; } template Vector2 Vector2::Zero() { Vector2 vector; vector.MakeZero(); return vector; } } template std::ostream& operator<<(std::ostream& out, const Nz::Vector2& vec) { return out << vec.ToString(); } template Nz::Vector2 operator*(T scale, const Nz::Vector2& vec) { return Nz::Vector2(scale * vec.x, scale * vec.y); } template Nz::Vector2 operator/(T scale, const Nz::Vector2& vec) { #if NAZARA_MATH_SAFE if (Nz::NumberEquals(vec.x, F(0.0)) || Nz::NumberEquals(vec.y, F(0.0))) { Nz::String error("Division by zero"); NazaraError(error); throw std::domain_error(error); } #endif return Nz::Vector2(scale/vec.x, scale/vec.y); } #undef F #include