Skip to content

Commit

Permalink
Rect utility functions
Browse files Browse the repository at this point in the history
* 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/1176861
Reviewed-by: Ian Vollick <vollick@chromium.org>
Commit-Queue: Mitsuru Oshima (OOO) <oshima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585065}
  • Loading branch information
mitoshima authored and Commit Bot committed Aug 22, 2018
1 parent f4c510e commit 9bc77cd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ui/gfx/geometry/rect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions ui/gfx/geometry/rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions ui/gfx/geometry/rect_f.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions ui/gfx/geometry/rect_f.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
25 changes: 25 additions & 0 deletions ui/gfx/geometry/rect_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down

0 comments on commit 9bc77cd

Please sign in to comment.