Hdu4998rotate (matrix transformation)

Source: Internet
Author: User

Hdu4998rotate (matrix transformation)


You are required to rotate a Graph P around a vertex every time. After rotating it n times, you are required to ask which vertex the graph can rotate at a time.


Solution: each rotation is equivalent to a matrix transformation. After N rotations, the system performs n matrix transformations. Finally, the rotation points and degrees are reversed based on the final matrix. Because cos (R) = 0 may occur, the atan2 function will not cause errors.

The matrix transformation of rotating P around a certain point


Code:

# Include <cstdio> # include <cstring> # include <cmath> using namespace STD; const int n = 15; const double Pi = ACOs (-1.0 ); struct rec {Double V [3] [3]; Rec () {memset (v, 0, sizeof (v);} void Init (double CX, double cy, double R) {v [0] [0] = cos (r); V [0] [1] = sin (R ); V [1] [0] =-sin (r); V [1] [1] = cos (R ); V [2] [0] = Cx-cx * Cos (r) + cy * sin (r); V [2] [1] = cy-cx * sin (r) -Cy * Cos (r); V [2] [2] = 1;} rec operator * (const rec & A) {rec TMP; For (INT I = 0; I <3; I ++) for (Int J = 0; j <3; j ++) for (int K = 0; k <3; k ++) TMP. V [I] [J] + = V [I] [k] *. V [k] [J]; return TMP;} rec operator * = (const rec & A) {return * This = * This * A ;}} rec [N]; int main () {int t; int N; Double X, Y, R; scanf ("% d", & T); While (t --) {scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% lf ", & X, & Y, & R); REC [I]. init (X, Y, R);} rec ans = rec [0]; for (INT I = 1; I <n; I ++) ans * = rec [I]; r = atan2 (ans. V [0] [1], ans. V [0] [0]); If (r <0) r = 2 * PI + R; double a1 = (1-ans. V [0] [0]); double a2 = ans. V [0] [1]; double b1 = A1; double b2 = a2; Double A = ans. V [2] [0]; Double B = ans. V [2] [1]; y = (A * B2 + B * A1)/(A1 * B1 + A2 * B2); X = (b1 * Y-B) /B2; printf ("%. 10lf %. 10lf %. 10lf \ n ", X, Y, R);} return 0 ;}


Hdu4998rotate (matrix transformation)

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.