Implementation of 2D image rotation

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://blog.csdn.net/clever101/


The two-dimensional image described here refers to the rotation of a triangle or polygon around a center point at a specified angle. A two-dimensional graph is actually composed of a series of discrete points. discrete points are placed in a specific coordinate system as vectors. Therefore, the rotation of a two-dimensional image is based on the rotation of a vector.


First, consider a vector p = (x, y). Then it is written as coordinates in the form of X + Iy, which is the coordinate of point P in the complex plane.

Question: Suppose there is an angle d, and the vector p rotates the angle D counterclockwise without changing its modulus. What is the Rotated Vector P?


Problem Analysis:


A simple example .....

For a vector p () that rotates 90 degrees counter-clockwise without changing its modulo size, we will clearly draw, after this change, the vectors are p '(). We restore them to the normal form, the original vectors () --- 1 + I * 0, and the new vectors) --- 0 + I * 1, it is not difficult to find that we multiply the general form of the original vector p by an I, and then sort it out to get the general coordinate form of the new vector p .... further imagine, what is I? I = cos (90) + I * sin (90); that is to say, the following conclusion can be drawn:

For a vector p: x + I * y, If you rotate the D angle counterclockwise, the new vector p should be a general expression of vector p (x + I * Y) multiply cos (d) + I * sin (D), then sort out a result, and regard the coefficient of I as the plural part of P, and the other part as the integer part of P, because we need to ensure that its size does not change, we only need to ensure that the modulo size of the multiplication vector is 1, and cos (d) + I * sin (d) meets all requirements.

Therefore, for P = (x, y), the vector is rotated counterclockwise and the size is not changed. The resulting vector is as follows:

P: (x, y) --------> P': (x * Cos (d)-y * sin (D), x * sin (d) + y * Cos (d ))

If the rotation is clockwise:

P: (x, y) --------> P': (x * Cos (-d)-y * sin (-d), x * sin (-d) + y * Cos (-D ))


Now that we understand the principle of vector rotation, you can see that the above vector rotation is performed around the origin (0, 0). what we want to achieve now is to rotate around a central point, what should we do? It is very easy, that is, to translate the point to be rotated to the coordinate system at the center point origin. After the rotation is completed, it is then translated back (for convenience of drawing ).


I understood the principle, so I realized it through programming. The following is the main implementation code:

Double const Pi = 3.1415926; <br/> /*! @ Struct <br/> * @ brief two-dimensional point struct <br/> */<br/> struct stpoint <br/> {< br/> stpoint (cpoint & point) <br/>{< br/> m_coorx = static_cast <double> (point. x); <br/> m_coory = static_cast <double> (point. y); <br/>}< br/> stpoint (Double X, Double Y) <br/>{< br/> m_coorx = X; <br/> m_coory = y; <br/>}</P> <p> /*! <Br/> * @ brief and cpoint class conversion of MFC <br/> */point after return conversion <br/> */<br/> operator cpoint const () <br/>{< br/> return cpoint (static_cast <long> (m_coorx), static_cast <long> (m_coory )); <br/>}< br/> stpoint operator-(stpoint & otherpt) <br/>{< br/> return stpoint (m_CoorX-OtherPt.m_CoorX, m_CoorY-OtherPt.m_CoorY ); <br/>}< br/> stpoint operator + (stpoint & otherpt) <br/>{< br/> return stpoint (M _ Coorx + otherpt. m_coorx, m_coory + otherpt. m_coory); <br/>}< br/> double m_coorx; <br/> double m_coory; <br/>}; <br/> /*! <Br/> * @ brief calculate x * sin (d) + y * Cos (d) <br/> * @ Param [in] Left left <br/> * @ Param [in] Right: Right <br/> */return calculation result <br/> */<br/> double vectormulti (stpoint & left, stpoint & right) <br/>{< br/> return (left. m_coorx * right. m_coory + left. m_coory * right. m_coorx); <br/>}< br/> /*! <Br/> * @ brief calculate x * Cos (d)-y * sin (d) <br/> * @ Param [in] Left left <br/> * @ Param [in] Right: Right <br/> */return calculation result <br/> */<br/> double crossproduct (stpoint & left, stpoint & right) <br/>{< br/> return (left. m_coorx * right. m_CoorX-Left.m_CoorY * right. m_coory); <br/>}< br/> /*! <Br/> * @ brief (x * Cos (d)-y * sin (D), x * sin (d) + y * Cos (d )) <br/> * @ Param [in] point to be rotated <br/> * @ Param [in] Right conversion point <br/> */return Rotation subsequent vertex <br/> */<br/> stpoint pointmulti (stpoint & point, stpoint & tranpt) <br/>{< br/> return stpoint (crossproduct (point, tranpt), vectormulti (point, tranpt )); <br/>}< br/> /*! <Br/> * @ brief transform a single vertex in a two-dimensional image <br/> * @ Param [in] [out] point to be rotated <br/> * @ Param [in] nangle Rotation Angle <br/> * @ Param [in] rotatecenter rotation center <br/> */return <br/> */<br/> void pointrotate (stpoint & point, int nangle, stpoint & rotatecenter) <br/>{< br/> // pan to the coordinate system at the center point origin <br/> point = point-rotatecenter; <br/> // degrees to radians <br/> double radian = static_cast <double> (nangle)/180.0 * PI; <br/> stpoint rotatept (COS (radian ), sin (radian); <br/> point = pointmulti (point, rotatept); <br/> // translation back <br/> point = point + rotatecenter; <br/>}< br/>

:

Before rotation:



After rotation:


Download related source code:


Code for Two-Dimensional Image Rotation


References:


1. Magic vector rotation









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.