Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,48 @@ TEST(GeometryTest, RectGetPositive) {
}
}

TEST(GeometryTest, RectScale) {
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Scale(0);
auto expected = Rect::MakeLTRB(0, 0, 0, 0);
ASSERT_RECT_NEAR(expected, actual);
}
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Scale(-2);
auto expected = Rect::MakeLTRB(200, 200, -200, -200);
ASSERT_RECT_NEAR(expected, actual);
}
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Scale(Point{0, 0});
auto expected = Rect::MakeLTRB(0, 0, 0, 0);
ASSERT_RECT_NEAR(expected, actual);
}
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Scale(Size{-1, -2});
auto expected = Rect::MakeLTRB(100, 200, -100, -200);
ASSERT_RECT_NEAR(expected, actual);
}
}

TEST(GeometryTest, RectProject) {
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Project(r);
auto expected = Rect::MakeLTRB(0, 0, 1, 1);
ASSERT_RECT_NEAR(expected, actual);
}
{
auto r = Rect::MakeLTRB(-100, -100, 100, 100);
auto actual = r.Project(Rect::MakeLTRB(0, 0, 100, 100));
auto expected = Rect::MakeLTRB(0.5, 0.5, 1, 1);
ASSERT_RECT_NEAR(expected, actual);
}
}

TEST(GeometryTest, CubicPathComponentPolylineDoesNotIncludePointOne) {
CubicPathComponent component({10, 10}, {20, 35}, {35, 20}, {40, 40});
auto polyline = component.CreatePolyline(1.0f);
Expand Down
33 changes: 27 additions & 6 deletions impeller/geometry/rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ struct TRect {
{size.width - r.size.width, size.height - r.size.height});
}

constexpr TRect operator*(Type scale) const {
return TRect({origin.x * scale, origin.y * scale},
{size.width * scale, size.height * scale});
}
constexpr TRect operator*(Type scale) const { return Scale(scale); }

constexpr TRect operator*(const TRect& r) const {
return TRect({origin.x * r.origin.x, origin.y * r.origin.y},
Expand All @@ -108,6 +105,20 @@ struct TRect {
return origin == r.origin && size == r.size;
}

constexpr TRect Scale(Type scale) const {
return TRect({origin.x * scale, origin.y * scale},
{size.width * scale, size.height * scale});
}

constexpr TRect Scale(TPoint<T> scale) const {
return TRect({origin.x * scale.x, origin.y * scale.y},
{size.width * scale.x, size.height * scale.y});
}

constexpr TRect Scale(TSize<T> scale) const {
return Scale(TPoint<T>(scale));
}

constexpr bool Contains(const TPoint<Type>& p) const {
return p.x >= GetLeft() && p.x < GetRight() && p.y >= GetTop() &&
p.y < GetBottom();
Expand Down Expand Up @@ -255,7 +266,7 @@ struct TRect {

/// @brief Returns a rectangle with expanded edges. Negative expansion
/// results in shrinking.
constexpr TRect<T> Expand(T left, T top, T right, T bottom) {
constexpr TRect<T> Expand(T left, T top, T right, T bottom) const {
return TRect(origin.x - left, //
origin.y - top, //
size.width + left + right, //
Expand All @@ -264,12 +275,22 @@ struct TRect {

/// @brief Returns a rectangle with expanded edges in all directions.
/// Negative expansion results in shrinking.
constexpr TRect<T> Expand(T amount) {
constexpr TRect<T> Expand(T amount) const {
return TRect(origin.x - amount, //
origin.y - amount, //
size.width + amount * 2, //
size.height + amount * 2);
}

/// @brief Returns a new rectangle that represents the projection of the
/// source rectangle onto this rectangle. In other words, the source
/// rectangle is redefined in terms of the corrdinate space of this
/// rectangle.
constexpr TRect<T> Project(TRect<T> source) const {
return source.Shift(-origin).Scale(
TSize<T>(1.0 / static_cast<Scalar>(size.width),
1.0 / static_cast<Scalar>(size.height)));
}
};

using Rect = TRect<Scalar>;
Expand Down