From 24d08042496dedd91565196267eb04102d07c3c5 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 3 Jul 2020 14:05:34 +1000 Subject: [PATCH] AP_Math: added rotate() method to Vector2f --- libraries/AP_Math/vector2.cpp | 13 +++++++++++++ libraries/AP_Math/vector2.h | 3 +++ 2 files changed, 16 insertions(+) diff --git a/libraries/AP_Math/vector2.cpp b/libraries/AP_Math/vector2.cpp index 8aa8cc30c6..8404fdedc2 100644 --- a/libraries/AP_Math/vector2.cpp +++ b/libraries/AP_Math/vector2.cpp @@ -421,6 +421,18 @@ float Vector2::closest_distance_between_radial_and_point(const Vector2 &w, return sqrtf(closest_distance_between_radial_and_point_squared(w,p)); } +// rotate vector by angle in radians +template +void Vector2::rotate(float angle_rad) +{ + const float cs = cosf(angle_rad); + const float sn = sinf(angle_rad); + float rx = x * cs - y * sn; + float ry = x * sn + y * cs; + x = rx; + y = ry; +} + // only define for float template float Vector2::length_squared(void) const; template float Vector2::length(void) const; @@ -454,6 +466,7 @@ template float Vector2::closest_distance_between_line_and_point(const Vec template float Vector2::closest_distance_between_line_and_point_squared(const Vector2 &w1, const Vector2 &w2, const Vector2 &p); template float Vector2::closest_distance_between_lines_squared(const Vector2 &a1,const Vector2 &a2,const Vector2 &b1,const Vector2 &b2); template Vector2 Vector2::projected(const Vector2 &v); +template void Vector2::rotate(float angle); template void Vector2::reflect(const Vector2 &n); diff --git a/libraries/AP_Math/vector2.h b/libraries/AP_Math/vector2.h index 5f3e27ef82..00fda1aaeb 100644 --- a/libraries/AP_Math/vector2.h +++ b/libraries/AP_Math/vector2.h @@ -152,6 +152,9 @@ struct Vector2 // adjust position by a given bearing (in degrees) and distance void offset_bearing(float bearing, float distance); + // rotate vector by angle in radians + void rotate(float angle_rad); + // given a position p1 and a velocity v1 produce a vector // perpendicular to v1 maximising distance from p1 static Vector2 perpendicular(const Vector2 &pos_delta, const Vector2 &v1);