// Copyright (C) 2012 Jérôme Leclercq // This file is part of the "Nazara Engine". // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include #include #include #include template NzVector2::NzVector2() { } template NzVector2::NzVector2(T X, T Y) : x(X), y(Y) { } template NzVector2::NzVector2(T scale) : x(scale), y(scale) { } template NzVector2::NzVector2(T vec[2]) : x(vec[0]), y(vec[1]) { } template template NzVector2::NzVector2(const NzVector2& vec) : x(static_cast(vec.x)), y(static_cast(vec.y)) { } template T NzVector2::AbsDotProduct(const NzVector2& vec) const { return std::fabs(x * vec.x) + std::fabs(y * vec.y); } template<> inline int NzVector2::AbsDotProduct(const NzVector2& vec) const { return std::labs(x * vec.x) + std::labs(y * vec.y); } template<> inline unsigned int NzVector2::AbsDotProduct(const NzVector2& vec) const { return std::labs(x * vec.x) + std::labs(y * vec.y); } template T NzVector2::Distance(const NzVector2& vec) const { return std::sqrt(SquaredDistance(vec)); } template float NzVector2::Distancef(const NzVector2& vec) const { return std::sqrt(static_cast(SquaredDistance(vec))); } template T NzVector2::DotProduct(const NzVector2& vec) const { return x * vec.x + y * vec.y; } template NzVector2 NzVector2::GetNormal() const { NzVector2 vec(*this); vec.Normalize(); return vec; } template void NzVector2::MakeCeil(const NzVector2& vec) { if (vec.x > x) x = vec.x; if (vec.y > y) y = vec.y; } template void NzVector2::MakeFloor(const NzVector2& vec) { if (vec.x < x) x = vec.x; if (vec.y < y) y = vec.y; } template T NzVector2::Length() const { return std::sqrt(SquaredLength()); } template float NzVector2::Lengthf() const { return std::sqrt(static_cast(SquaredLength())); } template void NzVector2::Normalize() { auto length = Length(); if (!NzNumberEquals(length, static_cast(0.0))) { x /= length; y /= length; } } template T NzVector2::SquaredDistance(const NzVector2& vec) const { return operator-(vec).SquaredLength(); } template T NzVector2::SquaredLength() const { return x * x + y * y; } template NzString NzVector2::ToString() const { NzStringStream ss; return ss << "Vector2(" << x << ", " << y << ')'; } template NzVector2::operator T*() { return &x; } template NzVector2::operator const T*() const { return &x; } template T& NzVector2::operator[](unsigned int i) { if (i >= 2) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 2)"; throw std::domain_error(ss.ToString()); } return *(&x+i); } template T NzVector2::operator[](unsigned int i) const { if (i >= 2) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 2)"; throw std::domain_error(ss.ToString()); } return *(&x+i); } template const NzVector2& NzVector2::operator+() const { return *this; } template NzVector2 NzVector2::operator-() const { return NzVector2(-x, -y); } template NzVector2 NzVector2::operator+(const NzVector2& vec) const { return NzVector2(x + vec.x, y + vec.y); } template NzVector2 NzVector2::operator-(const NzVector2& vec) const { return NzVector2(x - vec.x, y - vec.y); } template NzVector2 NzVector2::operator*(const NzVector2& vec) const { return NzVector2(x * vec.x, y * vec.y); } template NzVector2 NzVector2::operator*(T scale) const { return NzVector2(x * scale, y * scale); } template NzVector2 NzVector2::operator/(const NzVector2& vec) const { if (NzNumberEquals(vec.x, static_cast(0.0)) || NzNumberEquals(vec.y, static_cast(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } return NzVector2(x / vec.x, y / vec.y); } template NzVector2 NzVector2::operator/(T scale) const { if (NzNumberEquals(scale, static_cast(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } return NzVector2(x / scale, y / scale); } template NzVector2& NzVector2::operator+=(const NzVector2& vec) { x += vec.x; y += vec.y; return *this; } template NzVector2& NzVector2::operator-=(const NzVector2& vec) { x -= vec.x; y -= vec.y; return *this; } template NzVector2& NzVector2::operator*=(const NzVector2& vec) { x *= vec.x; y *= vec.y; return *this; } template NzVector2& NzVector2::operator*=(T scale) { x *= scale; y *= scale; return *this; } template NzVector2& NzVector2::operator/=(const NzVector2& vec) { if (NzNumberEquals(vec.x, static_cast(0.0)) || NzNumberEquals(vec.y, static_cast(0.0)) || NzNumberEquals(vec.z, static_cast(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } x /= vec.x; y /= vec.y; return *this; } template NzVector2& NzVector2::operator/=(T scale) { if (NzNumberEquals(scale, static_cast(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } x /= scale; y /= scale; return *this; } template bool NzVector2::operator==(const NzVector2& vec) const { return NzNumberEquals(x, vec.x) && NzNumberEquals(y, vec.y); } template bool NzVector2::operator!=(const NzVector2& vec) const { return !operator==(vec); } template bool NzVector2::operator<(const NzVector2& vec) const { return x < vec.x && y < vec.y; } template bool NzVector2::operator<=(const NzVector2& vec) const { return operator<(vec) || operator==(vec); } template bool NzVector2::operator>(const NzVector2& vec) const { return !operator<=(vec); } template bool NzVector2::operator>=(const NzVector2& vec) const { return !operator<(vec); } template std::ostream& operator<<(std::ostream& out, const NzVector2& vec) { return out << vec.ToString(); } template NzVector2 operator*(T scale, const NzVector2& vec) { return NzVector2(scale * vec.x, scale * vec.y); } template NzVector2 operator/(T scale, const NzVector2& vec) { if (NzNumberEquals(vec.x, static_cast(0.0)) || NzNumberEquals(vec.y, static_cast(0.0)) || NzNumberEquals(vec.z, static_cast(0.0))) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Division by zero"; throw std::domain_error(ss.ToString()); } return NzVector2(scale/vec.x, scale/vec.y); } #include