Commit 9bc77cd8 authored by Mitsuru Oshima's avatar Mitsuru Oshima Committed by Commit Bot

Rect utility functions

* left_center()/top_center()/right_center()/bottom center()
  return the center of each edge.
* Transpose()
  swap x/y axis

Bug: None
Test: covered by unittests
Change-Id: Icd63578dfe236caf6376c043fc1a0ffe74dfe6cc
Reviewed-on: https://chromium-review.googlesource.com/1176861Reviewed-by: default avatarIan Vollick <vollick@chromium.org>
Commit-Queue: Mitsuru Oshima (OOO) <oshima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585065}
parent f4c510e1
...@@ -257,6 +257,10 @@ void Rect::ClampToCenteredSize(const Size& size) { ...@@ -257,6 +257,10 @@ void Rect::ClampToCenteredSize(const Size& size) {
SetRect(new_x, new_y, new_width, new_height); 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 { void Rect::SplitVertically(Rect* left_half, Rect* right_half) const {
DCHECK(left_half); DCHECK(left_half);
DCHECK(right_half); DCHECK(right_half);
......
...@@ -103,6 +103,15 @@ class GFX_EXPORT Rect { ...@@ -103,6 +103,15 @@ class GFX_EXPORT Rect {
constexpr Point bottom_left() const { return Point(x(), bottom()); } constexpr Point bottom_left() const { return Point(x(), bottom()); }
constexpr Point bottom_right() const { return Point(right(), 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()); } Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); }
void SetRect(int x, int y, int width, int height) { void SetRect(int x, int y, int width, int height) {
...@@ -189,6 +198,9 @@ class GFX_EXPORT Rect { ...@@ -189,6 +198,9 @@ class GFX_EXPORT Rect {
// at given |size|. // at given |size|.
void ClampToCenteredSize(const Size& size); void ClampToCenteredSize(const Size& size);
// Transpose x and y axis.
void Transpose();
// Splits |this| in two halves, |left_half| and |right_half|. // Splits |this| in two halves, |left_half| and |right_half|.
void SplitVertically(Rect* left_half, Rect* right_half) const; void SplitVertically(Rect* left_half, Rect* right_half) const;
......
...@@ -183,6 +183,10 @@ void RectF::ClampToCenteredSize(const SizeF& size) { ...@@ -183,6 +183,10 @@ void RectF::ClampToCenteredSize(const SizeF& size) {
SetRect(new_x, new_y, new_width, new_height); 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 { void RectF::SplitVertically(RectF* left_half, RectF* right_half) const {
DCHECK(left_half); DCHECK(left_half);
DCHECK(right_half); DCHECK(right_half);
......
...@@ -70,6 +70,17 @@ class GFX_EXPORT RectF { ...@@ -70,6 +70,17 @@ class GFX_EXPORT RectF {
constexpr PointF bottom_left() const { return PointF(x(), bottom()); } constexpr PointF bottom_left() const { return PointF(x(), bottom()); }
constexpr PointF bottom_right() const { return PointF(right(), 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()); } Vector2dF OffsetFromOrigin() const { return Vector2dF(x(), y()); }
void SetRect(float x, float y, float width, float height) { void SetRect(float x, float y, float width, float height) {
...@@ -149,6 +160,9 @@ class GFX_EXPORT RectF { ...@@ -149,6 +160,9 @@ class GFX_EXPORT RectF {
// at given |size|. // at given |size|.
void ClampToCenteredSize(const SizeF& size); void ClampToCenteredSize(const SizeF& size);
// Transpose x and y axis.
void Transpose();
// Splits |this| in two halves, |left_half| and |right_half|. // Splits |this| in two halves, |left_half| and |right_half|.
void SplitVertically(RectF* left_half, RectF* right_half) const; void SplitVertically(RectF* left_half, RectF* right_half) const;
......
...@@ -869,6 +869,31 @@ TEST(RectTest, Corners) { ...@@ -869,6 +869,31 @@ TEST(RectTest, Corners) {
EXPECT_EQ(PointF(4.2f, 6.2f), f.bottom_right()); 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) { TEST(RectTest, ManhattanDistanceToPoint) {
Rect i(1, 2, 3, 4); Rect i(1, 2, 3, 4);
EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(1, 2))); EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(1, 2)));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment