nyoj 298 点的变换

版权声明:本文为博主原创文章,转载需注明出处。 https://blog.csdn.net/Hearthougan/article/details/24841293

题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=298

对所有的操作,我们可以用一个矩阵储存起来,一系列的操作,我们可以利用矩阵相乘,来储存!

平移:


关于x轴对称:


关于y轴对称:


缩放:


旋转:


例如按上述顺序操作(先平移,再关于x轴对称,再关于y轴对称,然后再缩放,最后在旋转)则可以表示为:


同样道理,这题 利用这种思路可已解决问题

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;
#define PI acos(-1.0)

const int MAXN = 10010;
struct Node
{
    double x, y;
}iPoint[MAXN];

struct Matrix
{
    double iMatrix[3][3];
};

Matrix Multi_Mattrix(Matrix a, Matrix b)
{
    Matrix c;

    int i, j, k;
    for(i = 0; i < 3; ++i)
    {
        for(j = 0; j < 3; ++j)
        {
            c.iMatrix[i][j] = 0;
            for(k = 0; k < 3; ++k)
                c.iMatrix[i][j] += a.iMatrix[i][k] * b.iMatrix[k][j];
        }
    }
    return c;
}

int main()
{
    int n, m, i;
    Matrix iMove, iXSymmetry, iYSymmetry, iEnlarge, iRotate;

    scanf("%d%d", &n, &m);
    for(i = 0; i < n; ++i)
        scanf("%lf%lf", &iPoint[i].x, &iPoint[i].y);

    memset(iMove.iMatrix, 0, sizeof(iMove.iMatrix));//平移
    memset(iXSymmetry.iMatrix, 0, sizeof(iXSymmetry.iMatrix));//关于x轴对称
    memset(iYSymmetry.iMatrix, 0, sizeof(iYSymmetry.iMatrix));//关于y轴对称
    memset(iEnlarge.iMatrix, 0, sizeof(iEnlarge.iMatrix));//缩放
    memset(iRotate.iMatrix, 0, sizeof(iRotate.iMatrix));//旋转
    iMove.iMatrix[0][0] = iMove.iMatrix[1][1] = iMove.iMatrix[2][2] = 1;
    iXSymmetry.iMatrix[0][0] = iXSymmetry.iMatrix[2][2] = 1; iXSymmetry.iMatrix[1][1] = -1;
    iYSymmetry.iMatrix[1][1] = iYSymmetry.iMatrix[2][2] = 1; iYSymmetry.iMatrix[0][0] = -1;
    iEnlarge.iMatrix[2][2] = iRotate.iMatrix[2][2] = 1;


    Matrix tmp;
    memset(tmp.iMatrix, 0, sizeof(tmp.iMatrix));
    tmp.iMatrix[0][0] = tmp.iMatrix[1][1] = tmp.iMatrix[2][2] = 1;
    char c;
    double x, y;
    while(m--)
    {
        getchar();
        scanf("%c", &c);

        switch(c)
        {
        case 'M':
            scanf("%lf %lf", &x, &y);
            iMove.iMatrix[0][2] = x;
            iMove.iMatrix[1][2] = y;
            tmp = Multi_Mattrix(iMove, tmp);
            break ;
        case 'X':
            tmp = Multi_Mattrix(iXSymmetry, tmp);
            break ;
        case 'Y':
            tmp = Multi_Mattrix(iYSymmetry, tmp);
            break ;
        case 'S':
            scanf("%lf", &x);
            iEnlarge.iMatrix[0][0] = iEnlarge.iMatrix[1][1] = x;
            tmp = Multi_Mattrix(iEnlarge, tmp);
            break ;
        case 'R':
            scanf("%lf", &x);
            y = x/180 * PI;
            iRotate.iMatrix[0][0] = iRotate.iMatrix[1][1] = cos(y);
            iRotate.iMatrix[0][1] = -sin(y);
            iRotate.iMatrix[1][0] = sin(y);
            tmp = Multi_Mattrix(iRotate, tmp);
            break ;
        default:
            break ;
        }
    }
    Matrix res;
    for(i = 0; i < n; ++i)
    {
        memset(res.iMatrix, 0, sizeof(res.iMatrix));

        res.iMatrix[0][0] = iPoint[i].x;
        res.iMatrix[1][0] = iPoint[i].y;
        res.iMatrix[2][0] = 1;
        res = Multi_Mattrix(tmp, res);
        printf("%.1lf %.1lf\n", res.iMatrix[0][0], res.iMatrix[1][0]);
    }
    return 0;
}



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页