diff --git a/impeller/geometry/geometry_unittests.cc b/impeller/geometry/geometry_unittests.cc index 5791393800bc1..4c0f2e3340463 100644 --- a/impeller/geometry/geometry_unittests.cc +++ b/impeller/geometry/geometry_unittests.cc @@ -454,6 +454,46 @@ TEST(GeometryTest, CanUsePointAssignmentOperators) { } } +TEST(GeometryTest, PointDotProduct) { + { + Point p(1, 0); + Scalar s = p.Dot(Point(-1, 0)); + ASSERT_FLOAT_EQ(s, -1); + } + + { + Point p(0, -1); + Scalar s = p.Dot(Point(-1, 0)); + ASSERT_FLOAT_EQ(s, 0); + } + + { + Point p(1, 2); + Scalar s = p.Dot(Point(3, -4)); + ASSERT_FLOAT_EQ(s, -5); + } +} + +TEST(GeometryTest, PointCrossProduct) { + { + Point p(1, 0); + Scalar s = p.Cross(Point(-1, 0)); + ASSERT_FLOAT_EQ(s, 0); + } + + { + Point p(0, -1); + Scalar s = p.Cross(Point(-1, 0)); + ASSERT_FLOAT_EQ(s, -1); + } + + { + Point p(1, 2); + Scalar s = p.Cross(Point(3, -4)); + ASSERT_FLOAT_EQ(s, -10); + } +} + TEST(GeometryTest, CanConvertBetweenDegressAndRadians) { { auto deg = Degrees{90.0}; diff --git a/impeller/geometry/point.h b/impeller/geometry/point.h index ad30904fdad4a..f41184f0b867e 100644 --- a/impeller/geometry/point.h +++ b/impeller/geometry/point.h @@ -175,6 +175,12 @@ struct TPoint { return {x / length, y / length}; } + constexpr Scalar Cross(const TPoint& p) const { + return (x * p.y) - (y * p.x); + } + + constexpr Scalar Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); } + constexpr bool IsZero() const { return x == 0 && y == 0; } };