HDU4998Rotate(矩阵变换)

题目大意:HDU4998Rotate(矩阵变换)


题目大意:要求你将一个图形每次绕着一个点旋转p,这样旋转了n次后,问这样的图形可以一次绕着哪个点旋转多少。


解题思路:每次的旋转都等价于一次矩阵的变换。n次旋转就做n次矩阵变换,最后在根据最后得到的矩阵来反推出旋转点和度数。因为可能会有cos(r) == 0的情况所以用atan2函数不会出错。

                  绕着某个点旋转p的矩阵变换


代码:

#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] * a.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%lf%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(矩阵变换))