From: Florian Forster 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=f0a3dd948ba0246146e2a18650c475e20f6a4471;p=supertux.git Rectf: Implement distance(). Calculates the distance from this rectangle to another rectangle or a vector. SVN-Revision: 6447 --- 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 :)