Add Point::AngleTo (flutter/engine#35510)
This commit is contained in:
@@ -773,6 +773,35 @@ TEST(GeometryTest, PointAbs) {
|
||||
ASSERT_POINT_NEAR(a_abs, expected);
|
||||
}
|
||||
|
||||
TEST(GeometryTest, PointAngleTo) {
|
||||
// Negative result in the CCW (with up = -Y) direction.
|
||||
{
|
||||
Point a(1, 1);
|
||||
Point b(1, -1);
|
||||
Radians actual = a.AngleTo(b);
|
||||
Radians expected = Radians{-kPi / 2};
|
||||
ASSERT_FLOAT_EQ(actual.radians, expected.radians);
|
||||
}
|
||||
|
||||
// Check the other direction to ensure the result is signed correctly.
|
||||
{
|
||||
Point a(1, -1);
|
||||
Point b(1, 1);
|
||||
Radians actual = a.AngleTo(b);
|
||||
Radians expected = Radians{kPi / 2};
|
||||
ASSERT_FLOAT_EQ(actual.radians, expected.radians);
|
||||
}
|
||||
|
||||
// Differences in magnitude should have no impact on the result.
|
||||
{
|
||||
Point a(100, -100);
|
||||
Point b(0.01, 0.01);
|
||||
Radians actual = a.AngleTo(b);
|
||||
Radians expected = Radians{kPi / 2};
|
||||
ASSERT_FLOAT_EQ(actual.radians, expected.radians);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(GeometryTest, CanUseVector3AssignmentOperators) {
|
||||
{
|
||||
Vector3 p(1, 2, 4);
|
||||
|
||||
@@ -204,6 +204,10 @@ struct TPoint {
|
||||
return *this - axis * this->Dot(axis) * 2;
|
||||
}
|
||||
|
||||
constexpr Radians AngleTo(const TPoint& p) const {
|
||||
return Radians{std::atan2(this->Cross(p), this->Dot(p))};
|
||||
}
|
||||
|
||||
constexpr bool IsZero() const { return x == 0 && y == 0; }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user