Add dot and cross product to Point (flutter/engine#33)
This commit is contained in:
committed by
Dan Field
parent
2132a0a1b9
commit
dd40a0e078
@@ -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};
|
||||
|
||||
@@ -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; }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user