Add transform*rect and transform*size operators

Summary:
Adding 2 `transform*rect` and `transform*size` operations.
Changelog: [Internal]

Reviewed By: JoshuaGross

Differential Revision: D20304247

fbshipit-source-id: 7bde67c96a21ce15e9da0464bcfccc61ab8fe13d
This commit is contained in:
Samuel Susla
2020-03-09 04:56:39 -07:00
committed by Facebook Github Bot
parent d0871d0a9a
commit 138ade0117
3 changed files with 71 additions and 0 deletions
+20
View File
@@ -180,5 +180,25 @@ Point operator*(Point const &point, Transform const &transform) {
return result;
}
Rect operator*(Rect const &rect, Transform const &transform) {
auto transformedSize = rect.size * transform;
auto pointAdjustment = Point{(rect.size.width - transformedSize.width) / 2,
(rect.size.height - transformedSize.height) / 2};
return {rect.origin + pointAdjustment, transformedSize};
}
Size operator*(Size const &size, Transform const &transform) {
if (transform == Transform::Identity()) {
return size;
}
auto result = Size{};
result.width = transform.at(0, 0) * size.width;
result.height = transform.at(1, 1) * size.height;
return result;
}
} // namespace react
} // namespace facebook
+11
View File
@@ -79,6 +79,17 @@ struct Transform {
*/
Point operator*(Point const &point, Transform const &transform);
/*
* Applies tranformation to the given size.
*/
Size operator*(Size const &size, Transform const &transform);
/*
* Applies tranformation to the given rect.
* ONLY SUPPORTS scale transformation.
*/
Rect operator*(Rect const &rect, Transform const &transform);
} // namespace react
} // namespace facebook
@@ -0,0 +1,40 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <react/graphics/Transform.h>
#include <gtest/gtest.h>
using namespace facebook::react;
TEST(TransformTest, transformingSize) {
auto size = facebook::react::Size{100, 200};
auto scaledSize = size * Transform::Scale(0.5, 0.5, 1);
EXPECT_EQ(scaledSize.width, 50);
EXPECT_EQ(scaledSize.height, 100);
}
TEST(TransformTest, transformingPoint) {
auto point = facebook::react::Point{100, 200};
auto translatedPoint = point * Transform::Translate(-50, -100, 0);
EXPECT_EQ(translatedPoint.x, 50);
EXPECT_EQ(translatedPoint.y, 100);
}
TEST(TransformTest, transformingRect) {
auto point = facebook::react::Point{100, 200};
auto size = facebook::react::Size{300, 400};
auto rect = facebook::react::Rect{point, size};
auto transformedRect = rect * Transform::Scale(0.5, 0.5, 1);
EXPECT_EQ(transformedRect.origin.x, 175);
EXPECT_EQ(transformedRect.origin.y, 300);
EXPECT_EQ(transformedRect.size.width, 150);
EXPECT_EQ(transformedRect.size.height, 200);
}