Skip to content

Commit

Permalink
Add dot and cross product to Point (flutter#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero authored and dnfield committed Apr 27, 2022
1 parent f0a7b4e commit df2485c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
40 changes: 40 additions & 0 deletions impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
6 changes: 6 additions & 0 deletions impeller/geometry/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};

Expand Down

0 comments on commit df2485c

Please sign in to comment.