NYOJ 298-点的变换(经典矩阵解决点平移、缩放、翻转和旋转)
来源:互联网 发布:河北大学网络教育招生 编辑:程序博客网 时间:2024/05/18 02:14
题目地址:NYOJ 298
思路:该题如果用对每个点模拟的操作,时间复杂度为O(n+m),结果肯定超时。然而利用矩阵乘法可以在O(m)的时间内把所有的操作合并为一个矩阵,然后每个点与该矩阵相乘可以得出最终的位置。
PS:十个利用矩阵乘法解决的经典题目 超级详细。
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;const int maxn=1e5+10;struct node { double mp[5][5];} q[maxn],res,init,ans;struct node Mul(struct node x,struct node y) { struct node tmp; int i,j,k; for(i=0; i<3; i++) { for(j=0; j<3; j++) { tmp.mp[i][j]=0; for(k=0; k<3; k++) tmp.mp[i][j]=tmp.mp[i][j]+x.mp[i][k]*y.mp[k][j]; } } return tmp;}int main() { int n,m,i,j; double a,b,bs,jd,hd; while(~scanf("%d %d",&n,&m)) { for(i=0; i<n; i++) { scanf("%lf %lf",&q[i].mp[0][0],&q[i].mp[1][0]); q[i].mp[2][0]=1; } for(i=0; i<3; i++) { ans.mp[i][i]=1; } char str[10]; while(m--) { memset(str,0,sizeof(str)); memset(res.mp,0,sizeof(res.mp)); scanf("%s",str); for(i=0; i<3; i++) res.mp[i][i]=1; if(str[0]=='M') { scanf("%lf %lf",&a,&b); res.mp[0][2]=a; res.mp[1][2]=b; } else if(str[0]=='X') { res.mp[1][1]=-1; } else if(str[0]=='Y') { res.mp[0][0]=-1; } else if(str[0]=='S') { scanf("%lf",&bs); res.mp[0][0]=bs; res.mp[1][1]=bs; } else if(str[0]=='R') { scanf("%lf",&jd); hd=jd/180*pi; res.mp[0][0]=cos(hd); res.mp[0][1]=-sin(hd); res.mp[1][0]=sin(hd); res.mp[1][1]=cos(hd); } ans=Mul(res,ans); } for(i=0; i<n; i++) { init=Mul(ans,q[i]); printf("%.1lf %.1lf\n",init.mp[0][0],init.mp[1][0]); } } return 0;}
1 0
- NYOJ 298-点的变换(经典矩阵解决点平移、缩放、翻转和旋转)
- Nyoj 298 点的变换[利用矩阵求解坐标点的转换,平移,绕原点旋转,沿x,y轴翻转]
- nyoj--298 点的变换(矩阵)
- 矩阵的平移,缩放和旋转
- 矩阵变换与图片平移,旋转,缩放的原理
- opencv 图像仿射变换 计算仿射变换后对应特征点的新坐标 图像旋转、缩放、平移
- opencv 图像仿射变换 计算仿射变换后对应特征点的新坐标 图像旋转、缩放、平移
- NYOJ 题目298 点的变换(数学,矩阵相乘,模板经典)
- nyoj ACM:点的变换(矩阵运算)
- NYOJ 298 点的变换 (矩阵快速幂)
- NYOJ 298 点的变换(矩阵快速幂)
- PCL 利用空间同名点求取旋转和平移矩阵
- NYOJ 298 点的变换
- nyoj 298 点的变换
- openGL 矩阵的旋转-平移-缩放
- Unity中矩阵的平移、旋转、缩放
- [caioj 1479,利用矩阵乘法解决的经典题目一]点的变换
- 第四节 WebGL中的图形变换:旋转、平移和缩放
- OracleXENuiv,数据库创建用户,表空间,
- LintCode Majority Number 主元素
- 指针数组与数组指针
- 2012年5月SAT香港真题解析
- linux根目录文件夹介绍
- NYOJ 298-点的变换(经典矩阵解决点平移、缩放、翻转和旋转)
- 黑马程序员——集合框架
- Debian系统升级:从Wheezy到Jessie
- 互联网金融机构最高补贴2000万元
- lib和dll文件的区别和联系
- Redis主从配置相关脚本
- mongodb不停机备份
- 客户端Socket
- 安卓事件分发机制