Bug 1352863 - Add a DistanceTo() method to BaseRect. r=kats
MozReview-Commit-ID: BHGXvNFlfb4
--- a/gfx/2d/BaseRect.h
+++ b/gfx/2d/BaseRect.h
@@ -562,25 +562,49 @@ struct BaseRect {
return Sub(
static_cast<T>(-std::numeric_limits<int32_t>::max() * 0.5),
static_cast<T>(-std::numeric_limits<int32_t>::max() * 0.5),
static_cast<T>(std::numeric_limits<int32_t>::max()),
static_cast<T>(std::numeric_limits<int32_t>::max())
);
};
+ // Returns a point representing the distance, along each dimension, of the
+ // given point from this rectangle. The distance along a dimension is defined
+ // as zero if the point is within the bounds of the rectangle in that
+ // dimension; otherwise, it's the distance to the closer endpoint of the
+ // rectangle in that dimension.
+ Point DistanceTo(const Point& aPoint) const
+ {
+ return {DistanceFromInterval(aPoint.x, x, XMost()),
+ DistanceFromInterval(aPoint.y, y, YMost())};
+ }
+
friend std::ostream& operator<<(std::ostream& stream,
const BaseRect<T, Sub, Point, SizeT, MarginT>& aRect) {
return stream << '(' << aRect.x << ',' << aRect.y << ','
<< aRect.width << ',' << aRect.height << ')';
}
private:
// Do not use the default operator== or operator!= !
// Use IsEqualEdges or IsEqualInterior explicitly.
bool operator==(const Sub& aRect) const { return false; }
bool operator!=(const Sub& aRect) const { return false; }
+
+ // Helper function for DistanceTo() that computes the distance of a
+ // coordinate along one dimension from an interval in that dimension.
+ static T DistanceFromInterval(T aCoord, T aIntervalStart, T aIntervalEnd)
+ {
+ if (aCoord < aIntervalStart) {
+ return aIntervalStart - aCoord;
+ }
+ if (aCoord > aIntervalEnd) {
+ return aCoord - aIntervalEnd;
+ }
+ return 0;
+ }
};
} // namespace gfx
} // namespace mozilla
#endif /* MOZILLA_GFX_BASERECT_H_ */