Added the point_rotator object.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402981
This commit is contained in:
Davis King 2009-04-04 15:41:21 +00:00
parent cd592e804b
commit a73c1619c7
3 changed files with 69 additions and 9 deletions

View File

@ -1244,23 +1244,46 @@ namespace dlib
typedef vector<long,2> point;
// ----------------------------------------------------------------------------------------
class point_rotator
{
public:
point_rotator (
const double& angle
)
{
sin_angle = std::sin(angle);
cos_angle = std::cos(angle);
}
template <typename T>
const dlib::vector<T,2> operator() (
const dlib::vector<T,2>& p
) const
{
double x = cos_angle*p.x() - sin_angle*p.y();
double y = sin_angle*p.x() + cos_angle*p.y();
return dlib::vector<double,2>(x,y);
}
private:
double sin_angle;
double cos_angle;
};
// ----------------------------------------------------------------------------------------
template <typename T>
const dlib::vector<T,2> rotate_point (
const dlib::vector<T,2>& center,
dlib::vector<T,2> p,
const dlib::vector<T,2>& p,
double angle
)
{
p -= center;
dlib::vector<double,2> temp;
const double cos_angle = cos(angle);
const double sin_angle = sin(angle);
temp.x() = cos_angle*p.x() - sin_angle*p.y();
temp.y() = sin_angle*p.x() + cos_angle*p.y();
return temp + center;
point_rotator rot(angle);
return rot(p-center)+center;
}
// ----------------------------------------------------------------------------------------

View File

@ -431,6 +431,39 @@ namespace dlib
typedef vector<long,2> point;
// ----------------------------------------------------------------------------------------
class point_rotator
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an object that takes 2D points or vectors and
rotates them around the origin by a given angle.
!*/
public:
point_rotator (
const double& angle
);
/*!
ensures
- When (*this)(p) is invoked it will return a point P such that:
- P is the point p rotated counter-clockwise around the origin
angle radians.
(Note that this is counter clockwise with respect to the normal
coordinate system with positive y going up and positive x going
to the right)
!*/
template <typename T>
const dlib::vector<T,2> operator() (
const dlib::vector<T,2>& p
) const;
/*!
ensures
- rotates p and returns the result
!*/
};
// ----------------------------------------------------------------------------------------
template <typename T>

View File

@ -288,6 +288,10 @@ namespace
DLIB_TEST(rotate_point(center, p1, -pi/4 + 10*pi) == point(8,-2));
DLIB_TEST(rotate_point(center, p1, pi/4 - 10*pi) == point(8,8));
DLIB_TEST(rotate_point(center, p1, -pi/4 - 10*pi) == point(8,-2));
point_rotator rot(pi/2);
DLIB_TEST(rot(point(1,0)) == point(0,1));
DLIB_TEST(rot(point(0,1)) == point(-1,0));
}
}