// 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 template NzRect::NzRect() { } template NzRect::NzRect(T X, T Y, T Width, T Height) : x(X), y(Y), width(Width), height(Height) { } template NzRect::NzRect(T vec[4]) : x(vec[0]), y(vec[1]), width(vec[2]), height(vec[3]) { } template template NzRect::NzRect(const NzRect& rect) : x(static_cast(rect.x)), y(static_cast(rect.y)), width(static_cast(rect.width)), height(static_cast(rect.height)) { } template bool NzRect::Contains(T X, T Y) const { return X >= x && X < x+width && Y >= y && Y < y+height; } template bool NzRect::Contains(const NzVector2& point) const { return Contains(point.x, point.y); } template bool NzRect::Contains(const NzRect& rect) const { return Contains(rect.x, rect.y) && Contains(rect.x + rect.width, rect.y + rect.height); } template void NzRect::ExtendTo(const NzVector2& point) { x = std::min(x, point.x); y = std::min(y, point.y); width = std::max(x+width, point.x)-x; height = std::max(y+height, point.y)-y; } template void NzRect::ExtendTo(const NzRect& rect) { x = std::min(x, rect.x); y = std::min(y, rect.y); width = std::max(x+width, rect.x+rect.width)-x; height = std::max(x+height, rect.y+rect.height)-y; } template NzVector2 NzRect::GetCenter() const { return NzVector2((x+width)/2, (y+height)/2); } template bool NzRect::Intersect(const NzRect& rect) const { NzRect intersection; // Optimisé par le compilateur return Intersect(rect, intersection); } template bool NzRect::Intersect(const NzRect& rect, NzRect& intersection) const { T left = std::max(x, rect.x); T right = std::min(x+width, rect.x+rect.width); T top = std::max(y, rect.y); T bottom = std::min(y+height, rect.y+rect.height); if (left < right && top < bottom) { intersection.x = left; intersection.y = top; intersection.width = right-left; intersection.height = bottom-top; return true; } else return false; } template bool NzRect::IsValid() const { return width > 0 && height > 0; } template NzString NzRect::ToString() const { NzStringStream ss; return ss << "Rect(" << x << ", " << y << ", " << width << ", " << height << ')'; } template NzRect::operator NzString() const { return ToString(); } template T& NzRect::operator[](unsigned int i) { if (i >= 4) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 4)"; throw std::domain_error(ss.ToString()); } return *(&x+i); } template T NzRect::operator[](unsigned int i) const { if (i >= 4) { NzStringStream ss; ss << __FILE__ << ':' << __LINE__ << ": Index out of range (" << i << " >= 4)"; throw std::domain_error(ss.ToString()); } return *(&x+i); } template std::ostream& operator<<(std::ostream& out, const NzRect& rect) { return out << rect.ToString(); } #include