You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
2.8 KiB
C++

#ifndef _Vector_2d_
#define _Vector_2d_
#include <math.h>
class Vector2d
{
public:
Vector2d() : _x(0.0), _y(0.0){}
~Vector2d() {}
typedef double Standard_Real;
Vector2d(Standard_Real X, Standard_Real Y) : _x(X), _y(Y){}
inline Standard_Real& x() {return _x;}
inline Standard_Real& y() {return _y;}
inline Standard_Real x() const { return _x; }
inline Standard_Real y() const { return _y; }
static Vector2d Zero() {return Vector2d();}
bool IsZero() {return (fabs(_x)< 0.000001 && fabs(_y)< 0.000001);}
void set(Standard_Real X, Standard_Real Y) {_x = X; _y = Y;}
Vector2d rotateBy(double dAngle) const
{
return Vector2d(_x * cos(dAngle) - _y * sin(dAngle), _x * sin(dAngle) + _y * cos(dAngle));
}
Vector2d rotateBy(const Vector2d& pt, double dAngle) const
{
return Vector2d((_x - pt._x) * cos(dAngle) - (_y - pt._y) * sin(dAngle) + pt._x, (_x - pt._x) * sin(dAngle) + (_y - pt._y) * cos(dAngle) + pt._y);
}
inline Vector2d& operator = (const Vector2d& rhs)
{
set(rhs.x(), rhs.y());
return *this;
}
inline Vector2d operator + (const Vector2d& rhs) const
{
return Vector2d(_x + rhs.x(), _y + rhs.y());
}
inline Vector2d& operator += (const Vector2d& rhs)
{
_x += rhs.x();
_y += rhs.y();
return *this;
}
inline Vector2d operator - ( const Vector2d& rhs) const
{
return Vector2d(_x-rhs.x(), _y-rhs.y());
}
inline Vector2d& operator -= (const Vector2d& rhs)
{
_x -= rhs.x();
_y -= rhs.y();
return *this;
}
inline Vector2d operator - () const;
inline double operator * (const Vector2d& rhs) const
{
return _x*rhs.x() + _y*rhs.y();
}
inline Vector2d operator * (double rhs) const
{
return Vector2d(_x*rhs, _y*rhs);
}
inline Vector2d& operator *= (double rhs)
{
_x *= rhs;
_y *= rhs;
return *this;
}
inline double operator ^ (const Vector2d& rhs) const
{
return _x * rhs.y() - _y * rhs.x();
}
inline Vector2d operator / (double rhs) const
{
return Vector2d(_x/rhs, _y/rhs);
}
inline bool operator == (const Vector2d& rhs) const {return _x == rhs.x() && _y == rhs.y();}
inline double norm() const
{
return sqrt(_x * _x + _y * _y);
}
inline double squaredNorm() const
{
return (_x * _x + _y * _y);
}
inline double dot(const Vector2d& rhs) const
{
return _x*rhs.x() + _y*rhs.y();
}
inline double cross(const Vector2d& rhs) const
{
return _x * rhs.y() - _y * rhs.x();
}
inline void normalize()
{
double length = norm();
if(length <= 1e-9)
return;
_x = _x / length;
_y = _y / length;
}
private:
Standard_Real _x;
Standard_Real _y;
};
//global functions
/// Multiplication of scalar with vector.
inline Vector2d operator * (double rhs, const Vector2d& rvs)
{
return Vector2d(rvs.x()*rhs, rvs.y()*rhs);
}
inline Vector2d Vector2d::operator - () const
{
return Vector2d(-_x, -_y);
}
#endif