/* * This file is part of libgim. * * libgim is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * libgim is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with libgim. If not, see . * * Copyright 2010 Danny Robson */ #include "region.hpp" #include "enable_if.hpp" #include "debug.hpp" #include #include using namespace util; template region::region (T _x, T _y, T _w, T _h): x (_x), y (_y), w (_w), h (_h) { } template region& region::operator+= (const vector<2> &rhs) { x += rhs.x; y += rhs.y; return *this; } template T region::area (void) const { return w * h; } template T region::diameter (void) const { return (T)sqrt (w * w + h * h); } template bool region::empty (void) const { return almost_equal (area (), 0); } template point<2> region::base (void) const { return { static_cast (x), static_cast (y) }; } template point<2> region::centre (void) const { double cx = x + static_cast(w / 2.0), cy = y + static_cast(h / 2.0); return { cx, cy }; } template bool region::includes (const point<2> &p) const { return p.x >= x && p.y >= y && p.x <= x + w && p.y <= y + h; } template bool region::contains (const point<2> &p) const { return p.x > x && p.y > y && p.x < x + w && p.y < y + h; } template bool region::overlaps (const region &rhs) const { //return !overlap (rhs).empty (); return x < rhs.x + rhs.w && x + w > rhs.x && y < rhs.y + rhs.h && y + h > rhs.y; } template region region::overlap (const region &rhs) const { double newx1 = max (x, rhs.x), newy1 = max (y, rhs.y), newx2 = min (x + w, rhs.x + w), newy2 = min (y + h, rhs.y + h); return region (newx1, newy1, newx2 - newx1, newy2 - newy1); } template bool region::operator ==(const region& rhs) const { return almost_equal (x, rhs.x) && almost_equal (y, rhs.y) && almost_equal (w, rhs.w) && almost_equal (h, rhs.h); } template void region::sanity (void) const { CHECK (w >= 0 && h >= 0); } namespace util { template <> void region::sanity (void) const { return; } template <> void region::sanity (void) const { return; } } template std::ostream& operator<< (std::ostream &os, const region &rhs) { os << "region(" << rhs.x << ", " << rhs.y << ", " << rhs.w << ", " << rhs.h << ")"; return os; } template struct region; template struct region; template struct region; template std::ostream& operator<< (std::ostream&, const region&); template std::ostream& operator<< (std::ostream&, const region&);