AP_Math: Vector2,3 get limit_length methods

This commit is contained in:
Randy Mackay 2021-03-11 21:04:12 +09:00
parent 3bb0482795
commit e2b46d05dc
4 changed files with 32 additions and 0 deletions

View File

@ -32,6 +32,19 @@ float Vector2<T>::length(void) const
return norm(x, y);
}
// limit vector to a given length. returns true if vector was limited
template <typename T>
bool Vector2<T>::limit_length(float max_length)
{
const float len = length();
if ((len > max_length) && is_positive(len)) {
x *= (max_length / len);
y *= (max_length / len);
return true;
}
return false;
}
// dot product
template <typename T>
T Vector2<T>::operator *(const Vector2<T> &v) const

View File

@ -138,6 +138,9 @@ struct Vector2
// gets the length of this vector
float length(void) const;
// limit vector to a given length. returns true if vector was limited
bool limit_length(float max_length);
// normalizes this vector
void normalize();

View File

@ -303,6 +303,19 @@ float Vector3<T>::length(void) const
return norm(x, y, z);
}
// limit xy component vector to a given length. returns true if vector was limited
template <typename T>
bool Vector3<T>::limit_length_xy(float max_length)
{
const float length_xy = norm(x, y);
if ((length_xy > max_length) && is_positive(length_xy)) {
x *= (max_length / length_xy);
y *= (max_length / length_xy);
return true;
}
return false;
}
template <typename T>
Vector3<T> &Vector3<T>::operator *=(const T num)
{

View File

@ -195,6 +195,9 @@ public:
// gets the length of this vector
float length(void) const;
// limit xy component vector to a given length. returns true if vector was limited
bool limit_length_xy(float max_length);
// normalizes this vector
void normalize()
{