From: florianf Date: Sun, 28 Feb 2010 10:51:32 +0000 (+0000) Subject: Rectf: Implement distance(). X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4a6701bd3dbd29530451f728126d30edb89eaddb;p=supertux.git Rectf: Implement distance(). Calculates the distance from this rectangle to another rectangle or a vector. git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6447 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- diff --git a/src/math/rectf.hpp b/src/math/rectf.hpp index f34bb43f7..f9d456593 100644 --- a/src/math/rectf.hpp +++ b/src/math/rectf.hpp @@ -20,6 +20,7 @@ #include #include "math/vector.hpp" +#include "object/anchor_point.hpp" /** This class represents a rectangle. * (Implementation Note) We're using upper left and lower right point instead of @@ -109,6 +110,20 @@ public: return true; } + float distance (const Vector& other, AnchorPoint ap = ANCHOR_MIDDLE) const + { + Vector v = get_anchor_pos (*this, ap); + return ((v - other).norm ()); + } + + float distance (const Rectf& other, AnchorPoint ap = ANCHOR_MIDDLE) const + { + Vector v1 = get_anchor_pos (*this, ap); + Vector v2 = get_anchor_pos (other, ap); + + return ((v1 - v2).norm ()); + } + // leave these two public to save the headaches of set/get functions for such // simple things :)