uncrustify libraries/AP_Math/vector2.h
This commit is contained in:
parent
ffb4163b51
commit
087b2f99c7
@ -9,16 +9,16 @@
|
|||||||
|
|
||||||
// Derived closely from:
|
// Derived closely from:
|
||||||
/****************************************
|
/****************************************
|
||||||
* 2D Vector Classes
|
* 2D Vector Classes
|
||||||
* By Bill Perone (billperone@yahoo.com)
|
* By Bill Perone (billperone@yahoo.com)
|
||||||
* Original: 9-16-2002
|
* Original: 9-16-2002
|
||||||
* Revised: 19-11-2003
|
* Revised: 19-11-2003
|
||||||
* 18-12-2003
|
* 18-12-2003
|
||||||
* 06-06-2004
|
* 06-06-2004
|
||||||
*
|
*
|
||||||
* © 2003, This code is provided "as is" and you can use it freely as long as
|
* © 2003, This code is provided "as is" and you can use it freely as long as
|
||||||
* credit is given to Bill Perone in the application it is used in
|
* credit is given to Bill Perone in the application it is used in
|
||||||
****************************************/
|
****************************************/
|
||||||
|
|
||||||
#ifndef VECTOR2_H
|
#ifndef VECTOR2_H
|
||||||
#define VECTOR2_H
|
#define VECTOR2_H
|
||||||
@ -28,134 +28,169 @@
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
struct Vector2
|
struct Vector2
|
||||||
{
|
{
|
||||||
T x, y;
|
T x, y;
|
||||||
|
|
||||||
// trivial ctor
|
// trivial ctor
|
||||||
Vector2<T>() { x = y = 0; }
|
Vector2<T>() {
|
||||||
|
x = y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// setting ctor
|
// setting ctor
|
||||||
Vector2<T>(const T x0, const T y0): x(x0), y(y0) {}
|
Vector2<T>(const T x0, const T y0) : x(x0), y(y0) {
|
||||||
|
}
|
||||||
|
|
||||||
// function call operator
|
// function call operator
|
||||||
void operator ()(const T x0, const T y0)
|
void operator ()(const T x0, const T y0)
|
||||||
{ x= x0; y= y0; }
|
{
|
||||||
|
x= x0; y= y0;
|
||||||
|
}
|
||||||
|
|
||||||
// test for equality
|
// test for equality
|
||||||
bool operator==(const Vector2<T> &v)
|
bool operator==(const Vector2<T> &v)
|
||||||
{ return (x==v.x && y==v.y); }
|
{
|
||||||
|
return (x==v.x && y==v.y);
|
||||||
|
}
|
||||||
|
|
||||||
// test for inequality
|
// test for inequality
|
||||||
bool operator!=(const Vector2<T> &v)
|
bool operator!=(const Vector2<T> &v)
|
||||||
{ return (x!=v.x || y!=v.y); }
|
{
|
||||||
|
return (x!=v.x || y!=v.y);
|
||||||
|
}
|
||||||
|
|
||||||
// negation
|
// negation
|
||||||
Vector2<T> operator -(void) const
|
Vector2<T> operator -(void) const
|
||||||
{ return Vector2<T>(-x, -y); }
|
{
|
||||||
|
return Vector2<T>(-x, -y);
|
||||||
|
}
|
||||||
|
|
||||||
// addition
|
// addition
|
||||||
Vector2<T> operator +(const Vector2<T> &v) const
|
Vector2<T> operator +(const Vector2<T> &v) const
|
||||||
{ return Vector2<T>(x+v.x, y+v.y); }
|
{
|
||||||
|
return Vector2<T>(x+v.x, y+v.y);
|
||||||
|
}
|
||||||
|
|
||||||
// subtraction
|
// subtraction
|
||||||
Vector2<T> operator -(const Vector2<T> &v) const
|
Vector2<T> operator -(const Vector2<T> &v) const
|
||||||
{ return Vector2<T>(x-v.x, y-v.y); }
|
{
|
||||||
|
return Vector2<T>(x-v.x, y-v.y);
|
||||||
|
}
|
||||||
|
|
||||||
// uniform scaling
|
// uniform scaling
|
||||||
Vector2<T> operator *(const T num) const
|
Vector2<T> operator *(const T num) const
|
||||||
{
|
{
|
||||||
Vector2<T> temp(*this);
|
Vector2<T> temp(*this);
|
||||||
return temp*=num;
|
return temp*=num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// uniform scaling
|
// uniform scaling
|
||||||
Vector2<T> operator /(const T num) const
|
Vector2<T> operator /(const T num) const
|
||||||
{
|
{
|
||||||
Vector2<T> temp(*this);
|
Vector2<T> temp(*this);
|
||||||
return temp/=num;
|
return temp/=num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// addition
|
// addition
|
||||||
Vector2<T> &operator +=(const Vector2<T> &v)
|
Vector2<T> &operator +=(const Vector2<T> &v)
|
||||||
{
|
{
|
||||||
x+=v.x; y+=v.y;
|
x+=v.x; y+=v.y;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// subtraction
|
// subtraction
|
||||||
Vector2<T> &operator -=(const Vector2<T> &v)
|
Vector2<T> &operator -=(const Vector2<T> &v)
|
||||||
{
|
{
|
||||||
x-=v.x; y-=v.y;
|
x-=v.x; y-=v.y;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// uniform scaling
|
// uniform scaling
|
||||||
Vector2<T> &operator *=(const T num)
|
Vector2<T> &operator *=(const T num)
|
||||||
{
|
{
|
||||||
x*=num; y*=num;
|
x*=num; y*=num;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// uniform scaling
|
// uniform scaling
|
||||||
Vector2<T> &operator /=(const T num)
|
Vector2<T> &operator /=(const T num)
|
||||||
{
|
{
|
||||||
x/=num; y/=num;
|
x/=num; y/=num;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// dot product
|
// dot product
|
||||||
T operator *(const Vector2<T> &v) const
|
T operator *(const Vector2<T> &v) const
|
||||||
{ return x*v.x + y*v.y; }
|
{
|
||||||
|
return x*v.x + y*v.y;
|
||||||
|
}
|
||||||
|
|
||||||
// gets the length of this vector squared
|
// gets the length of this vector squared
|
||||||
T length_squared() const
|
T length_squared() const
|
||||||
{ return (T)(*this * *this); }
|
{
|
||||||
|
return (T)(*this * *this);
|
||||||
|
}
|
||||||
|
|
||||||
// gets the length of this vector
|
// gets the length of this vector
|
||||||
T length() const
|
T length() const
|
||||||
{ return (T)sqrt(*this * *this); }
|
{
|
||||||
|
return (T)sqrt(*this * *this);
|
||||||
|
}
|
||||||
|
|
||||||
// normalizes this vector
|
// normalizes this vector
|
||||||
void normalize()
|
void normalize()
|
||||||
{ *this/=length(); }
|
{
|
||||||
|
*this/=length();
|
||||||
|
}
|
||||||
|
|
||||||
// returns the normalized vector
|
// returns the normalized vector
|
||||||
Vector2<T> normalized() const
|
Vector2<T> normalized() const
|
||||||
{ return *this/length(); }
|
{
|
||||||
|
return *this/length();
|
||||||
|
}
|
||||||
|
|
||||||
// reflects this vector about n
|
// reflects this vector about n
|
||||||
void reflect(const Vector2<T> &n)
|
void reflect(const Vector2<T> &n)
|
||||||
{
|
{
|
||||||
Vector2<T> orig(*this);
|
Vector2<T> orig(*this);
|
||||||
project(n);
|
project(n);
|
||||||
*this= *this*2 - orig;
|
*this= *this*2 - orig;
|
||||||
}
|
}
|
||||||
|
|
||||||
// projects this vector onto v
|
// projects this vector onto v
|
||||||
void project(const Vector2<T> &v)
|
void project(const Vector2<T> &v)
|
||||||
{ *this= v * (*this * v)/(v*v); }
|
{
|
||||||
|
*this= v * (*this * v)/(v*v);
|
||||||
|
}
|
||||||
|
|
||||||
// returns this vector projected onto v
|
// returns this vector projected onto v
|
||||||
Vector2<T> projected(const Vector2<T> &v)
|
Vector2<T> projected(const Vector2<T> &v)
|
||||||
{ return v * (*this * v)/(v*v); }
|
{
|
||||||
|
return v * (*this * v)/(v*v);
|
||||||
|
}
|
||||||
|
|
||||||
// computes the angle between 2 arbitrary vectors
|
// computes the angle between 2 arbitrary vectors
|
||||||
T angle(const Vector2<T> &v1, const Vector2<T> &v2)
|
T angle(const Vector2<T> &v1, const Vector2<T> &v2)
|
||||||
{ return (T)acos((v1*v2) / (v1.length()*v2.length())); }
|
{
|
||||||
|
return (T)acos((v1*v2) / (v1.length()*v2.length()));
|
||||||
|
}
|
||||||
|
|
||||||
// computes the angle between this vector and another vector
|
// computes the angle between this vector and another vector
|
||||||
T angle(const Vector2<T> &v2)
|
T angle(const Vector2<T> &v2)
|
||||||
{ return (T)acos(((*this)*v2) / (this->length()*v2.length())); }
|
{
|
||||||
|
return (T)acos(((*this)*v2) / (this->length()*v2.length()));
|
||||||
|
}
|
||||||
|
|
||||||
// computes the angle between 2 normalized arbitrary vectors
|
// computes the angle between 2 normalized arbitrary vectors
|
||||||
T angle_normalized(const Vector2<T> &v1, const Vector2<T> &v2)
|
T angle_normalized(const Vector2<T> &v1, const Vector2<T> &v2)
|
||||||
{ return (T)acos(v1*v2); }
|
{
|
||||||
|
return (T)acos(v1*v2);
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef Vector2<int16_t> Vector2i;
|
typedef Vector2<int16_t> Vector2i;
|
||||||
typedef Vector2<uint16_t> Vector2ui;
|
typedef Vector2<uint16_t> Vector2ui;
|
||||||
typedef Vector2<int32_t> Vector2l;
|
typedef Vector2<int32_t> Vector2l;
|
||||||
typedef Vector2<uint32_t> Vector2ul;
|
typedef Vector2<uint32_t> Vector2ul;
|
||||||
typedef Vector2<float> Vector2f;
|
typedef Vector2<float> Vector2f;
|
||||||
|
|
||||||
#endif // VECTOR2_H
|
#endif // VECTOR2_H
|
||||||
|
Loading…
Reference in New Issue
Block a user