diff --git a/ui/gfx/geometry/rect.cc b/ui/gfx/geometry/rect.cc index 63c0bbf04f0369..dd51fd47d61602 100644 --- a/ui/gfx/geometry/rect.cc +++ b/ui/gfx/geometry/rect.cc @@ -257,6 +257,10 @@ void Rect::ClampToCenteredSize(const Size& size) { SetRect(new_x, new_y, new_width, new_height); } +void Rect::Transpose() { + SetRect(y(), x(), height(), width()); +} + void Rect::SplitVertically(Rect* left_half, Rect* right_half) const { DCHECK(left_half); DCHECK(right_half); diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h index 3109912b6a2b77..8a4a3f878f650d 100644 --- a/ui/gfx/geometry/rect.h +++ b/ui/gfx/geometry/rect.h @@ -103,6 +103,15 @@ class GFX_EXPORT Rect { constexpr Point bottom_left() const { return Point(x(), bottom()); } constexpr Point bottom_right() const { return Point(right(), bottom()); } + constexpr Point left_center() const { return Point(x(), y() + height() / 2); } + constexpr Point top_center() const { return Point(x() + width() / 2, y()); } + constexpr Point right_center() const { + return Point(right(), y() + height() / 2); + } + constexpr Point bottom_center() const { + return Point(x() + width() / 2, bottom()); + } + Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); } void SetRect(int x, int y, int width, int height) { @@ -189,6 +198,9 @@ class GFX_EXPORT Rect { // at given |size|. void ClampToCenteredSize(const Size& size); + // Transpose x and y axis. + void Transpose(); + // Splits |this| in two halves, |left_half| and |right_half|. void SplitVertically(Rect* left_half, Rect* right_half) const; diff --git a/ui/gfx/geometry/rect_f.cc b/ui/gfx/geometry/rect_f.cc index bb474ba63dcc40..eb657858a5fda5 100644 --- a/ui/gfx/geometry/rect_f.cc +++ b/ui/gfx/geometry/rect_f.cc @@ -183,6 +183,10 @@ void RectF::ClampToCenteredSize(const SizeF& size) { SetRect(new_x, new_y, new_width, new_height); } +void RectF::Transpose() { + SetRect(y(), x(), height(), width()); +} + void RectF::SplitVertically(RectF* left_half, RectF* right_half) const { DCHECK(left_half); DCHECK(right_half); diff --git a/ui/gfx/geometry/rect_f.h b/ui/gfx/geometry/rect_f.h index 9d99052c4a5f31..7ec5c97549b06e 100644 --- a/ui/gfx/geometry/rect_f.h +++ b/ui/gfx/geometry/rect_f.h @@ -70,6 +70,17 @@ class GFX_EXPORT RectF { constexpr PointF bottom_left() const { return PointF(x(), bottom()); } constexpr PointF bottom_right() const { return PointF(right(), bottom()); } + constexpr PointF left_center() const { + return PointF(x(), y() + height() / 2); + } + constexpr PointF top_center() const { return PointF(x() + width() / 2, y()); } + constexpr PointF right_center() const { + return PointF(right(), y() + height() / 2); + } + constexpr PointF bottom_center() const { + return PointF(x() + width() / 2, bottom()); + } + Vector2dF OffsetFromOrigin() const { return Vector2dF(x(), y()); } void SetRect(float x, float y, float width, float height) { @@ -149,6 +160,9 @@ class GFX_EXPORT RectF { // at given |size|. void ClampToCenteredSize(const SizeF& size); + // Transpose x and y axis. + void Transpose(); + // Splits |this| in two halves, |left_half| and |right_half|. void SplitVertically(RectF* left_half, RectF* right_half) const; diff --git a/ui/gfx/geometry/rect_unittest.cc b/ui/gfx/geometry/rect_unittest.cc index aaa533bfcde238..7acfb806501411 100644 --- a/ui/gfx/geometry/rect_unittest.cc +++ b/ui/gfx/geometry/rect_unittest.cc @@ -869,6 +869,31 @@ TEST(RectTest, Corners) { EXPECT_EQ(PointF(4.2f, 6.2f), f.bottom_right()); } +TEST(RectTest, Centers) { + Rect i(10, 20, 30, 40); + EXPECT_EQ(Point(10, 40), i.left_center()); + EXPECT_EQ(Point(25, 20), i.top_center()); + EXPECT_EQ(Point(40, 40), i.right_center()); + EXPECT_EQ(Point(25, 60), i.bottom_center()); + + RectF f(10.1f, 20.2f, 30.3f, 40.4f); + EXPECT_EQ(PointF(10.1f, 40.4f), f.left_center()); + EXPECT_EQ(PointF(25.25f, 20.2f), f.top_center()); + EXPECT_EQ(PointF(40.4f, 40.4f), f.right_center()); + EXPECT_EQ(25.25f, f.bottom_center().x()); + EXPECT_NEAR(60.6f, f.bottom_center().y(), 0.001f); +} + +TEST(RectTest, Transpose) { + Rect i(10, 20, 30, 40); + i.Transpose(); + EXPECT_EQ(Rect(20, 10, 40, 30), i); + + RectF f(10.1f, 20.2f, 30.3f, 40.4f); + f.Transpose(); + EXPECT_EQ(RectF(20.2f, 10.1f, 40.4f, 30.3f), f); +} + TEST(RectTest, ManhattanDistanceToPoint) { Rect i(1, 2, 3, 4); EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(1, 2)));