Commit a73c1619 authored by Davis King's avatar Davis King

Added the point_rotator object.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402981
parent cd592e80
......@@ -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;
}
// ----------------------------------------------------------------------------------------
......
......@@ -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>
......
......@@ -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));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment