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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值