From: fluffymormegil Date: Thu, 15 Mar 2012 20:20:31 +0000 (+0000) Subject: Added class for representing rectangles X-Git-Url: http://git.blackswordsonics.com/?a=commitdiff_plain;h=777dff5066c26d53a30173d9c7a8dcc209c87f8c;p=libmormegil Added class for representing rectangles --- diff --git a/include/libmormegil/Rectangle.hh b/include/libmormegil/Rectangle.hh new file mode 100644 index 0000000..f3c6546 --- /dev/null +++ b/include/libmormegil/Rectangle.hh @@ -0,0 +1,94 @@ +// libmormegil/Rectangle.hh +// +// In jurisdictions where this file would be adjuged to contain copyrightable +// material, it is copyright 2011 Martin Read, and released to the public +// under the terms of the Creative Commons Public Domain Dedication (cc-0). +// It is provided without any warranty, express or implied. + +// We need std::max<> and std::min<>. The thing called std::abs<> is in +// and is intended for use on complex numbers only; did I mention I +// hate the standards committee sometimes? + +#ifndef libmormegil_Rectangle_hh +#define libmormegil_Rectangle_hh + +#ifndef libmormegil_Coord_hh +#include +#endif + +namespace libmormegil +{ + template struct basic_rectangle + { + typedef basic_rectangle& ref; + typedef const basic_rectangle& const_ref; + typedef basic_coord punkt; + typedef basic_coord& punktref; + typedef const basic_coord& const_punktref; + basic_coord topleft; + basic_coord botright; + T area() const { return (botright.y + 1 - topleft.y) * (botright.x + 1 - topleft.x); } + T topedge() const { return topleft.y; } + T bottomedge() const { return botright.y; } + T leftedge() const { return topleft.x; } + T rightedge() const { return botright.x; } + bool is_square() const + { + return (botright.y - topleft.y) == (botright.x - topleft.x); + } + bool contains(const_punktref p) const + { + return (p.y >= topleft.y) && (p.x >= topleft.x) && (p.y <= botright.y) && (p.x <= botright.x); + } + T hdist(const_ref other) const + { + if (leftedge() > other.rightedge()) + { + return leftedge() - other.rightedge(); + } + else if (rightedge() < other.leftedge()) + { + return other.leftedge() - rightedge(); + } + else + { + return 0; + } + } + T vdist(const_ref other) const + { + if (topedge() > other.bottomedge()) + { + return topedge() - other.bottomedge(); + } + else if (bottomedge() < other.topedge()) + { + return other.topedge() - bottomedge(); + } + else + { + return 0; + } + } + T mindist_taxi(const_ref other) const + { + T h = hdist(other), v = vdist(other); + return h + v; + } + T mindistsq(const_ref other) const + { + T h = hdist(other), v = vdist(other); + return h * h + v * v; + } + T mindist_inf(const_ref other) const + { + T h = hdist(other), v = vdist(other); + return std::max(h, v); + } + }; + typedef basic_rectangle Rectangle; + typedef basic_rectangle Rectangle64; +} +#endif // libmormegil_Rectangle_hh + +// vim:ts=8:sts=4:sw=4:expandtab:fo=croq