From e560faa520ea9259c0802ddf692cc187e59d6534 Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 2 May 2014 14:13:00 -0400 Subject: [PATCH] Added some utility functions for QPointF distances --- Maths.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Maths.h b/Maths.h index e31ee22..65429ea 100644 --- a/Maths.h +++ b/Maths.h @@ -27,7 +27,24 @@ inline qreal degreesToRadians(qreal degrees) { return degrees / 180.0f * M_PI; } inline qreal radiansToDegrees(qreal radians) { return radians / M_PI * 180.0f; } -// Wrap value around ie. wrapAround(-1, 3) ==> 2 +/// Wrap value around ie. wrapAround(-1, 3) ==> 2 inline int wrapAround(int index, int max) { return (index + max) % max; } +/// Square of x. +inline qreal sq(qreal x) { return x*x; } + +/// +inline qreal distSq(const QPointF& p1, const QPointF& p2) { + return sq(p1.x() - p2.x()) + sq(p1.y() - p2.y()); +} + +/// Euclidian distance between two points. +inline qreal dist(const QPointF& p1, const QPointF& p2) { + return sqrt( distSq(p1, p2) ); +} + +inline bool distIsInside(const QPointF& p1, const QPointF& p2, qreal radius) { + return distSq(p1, p2) < sq(radius); +} + #endif /* MATH_H_ */