Compare commits

...

1 Commits

Author SHA1 Message Date
Matthias Grob 5c9fac79ae Vector2: Add function to rotate a 2D vector 2023-03-03 19:03:00 +01:00
2 changed files with 41 additions and 0 deletions
+25
View File
@@ -71,6 +71,31 @@ public:
return (*this).cross(b);
}
/**
* @brief rotate vector
*
* @param[in] angle [rad] The angle around the positive out of plane axis.
*/
void rotate(const Type angle)
{
Vector2 &v(*this);
Type sin_angle = sin(angle);
Type cos_angle = cos(angle);
v = Vector2(
cos_angle * v(0) - sin_angle * v(1),
sin_angle * v(0) + cos_angle * v(1)
);
}
/**
* @brief Transform vector to new coordinate system
*
* @param[in] angle [rad] The rotation of new coordinate system around the positive out of plane axis.
*/
void transform(const Type angle)
{
this->rotate(-angle);
}
};
+16
View File
@@ -64,4 +64,20 @@ TEST(MatrixVector2Test, Vector2)
Vector2f h(g);
EXPECT_FLOAT_EQ(h(0), 1.23f);
EXPECT_FLOAT_EQ(h(1), 423.4f);
// rotate 2D vector
Vector2f i(1.0f, 0.0f);
i.rotate(M_PI_PRECISE / 4.f);
EXPECT_FLOAT_EQ(i(0), i(1));
i.rotate(M_PI_PRECISE / 4.f);
EXPECT_FLOAT_EQ(i(0), 0.f);
EXPECT_FLOAT_EQ(i(1), 1.f);
// transform vector
Vector2f j(1.0f, 0.0f);
j.transform(-M_PI_PRECISE / 4.f);
EXPECT_FLOAT_EQ(j(0), j(1));
j.transform(-M_PI_PRECISE / 4.f);
EXPECT_FLOAT_EQ(j(0), 0.f);
EXPECT_FLOAT_EQ(j(1), 1.f);
}