Transformations

本文介绍了一种识别图案经过何种变换(旋转、反射等)后变为另一图案的方法。通过编写C++程序,实现90度旋转及水平反射,并逐一验证变换类型。

Transformations

A square pattern of size N x N (1 <= N <= 10) black and white square tiles is transformed into another square pattern. Write a program that will recognize the minimum transformation that has been applied to the original pattern given the following list of possible transformations:

• #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.
• #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.
• #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.
• #4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).
• #5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).
• #6: No Change: The original pattern was not changed.
• #7: Invalid Transformation: The new pattern was not obtained by any of the above methods.
In the case that more than one transform could have been used, choose the one with the minimum number above.

INPUT FORMAT

Line 1:A single integer, N
Line 2~N+1:N lines of N characters (each either @' or-’); this is the square before transformation
Line N+2~2*N+1:N lines of N characters (each either @' or-’); this is the square after transformation

SAMPLE INPUT

3
@-@
---
@@-
@-@
@--
--@

OUTPUT FORMAT

A single line containing the number from 1 through 7 (described above) that categorizes the transformation required to change from the ‘before’ representation to the ‘after’ representation.

SAMPLE OUTPUT

1

设计算法:

最初我是在想写个函数解决旋转90°的问题,那么旋转180°和270°只需要多次调用函数就行了,后面发现旋转90°其实就是求一个矩阵的转置矩阵,这个函数找找规律就可以写出来了,很简单。接着是写个函数解决反射问题,其实题目的反射我是没怎么看懂是什么意思,不过提到了镜面反射,自己写了个矩阵拿到镜子一照得到镜面反射结果,那么反射的函数也很简单就可以完成了,后面基本上就是一个一个去判断了,这个不难,也就那么几种情况,可以一个一个对着判断,也可以写成循环

C++编写:

写了三个代码都能够通过,其实都差不多,不过还是调了挺久的
最初代码:

/*
ID: your_id_here
TASK: transform
LANG: C++                 
*/
#include<iostream>
#include<cstdio>
using namespace std;

int N;
char before[12][12],after[12][12];

bool check(char a[12][12],char b[12][12])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(a[i][j] != b[i][j])
                return false;
        }
    }
    return true;
}

void rotate(char begin[12][12],char end[12][12])       /*旋转90°,其实这个就是求转置矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            end[j][N-1-i]=begin[i][j];

    }
}

void reflact(char begin[12][12],char end[12][12])      /*求反射矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            end[i][N-j-1]=begin[i][j];
        }
    }
}

void solve()
{
    char temp1[12][12],temp2[12][12],temp3[12][12],temp4[12][12],temp5[12][12],temp6[12][12],temp7[12][12];
    rotate(before,temp1);             /*旋转90° */
    if(check(temp1,after))
    {
        cout<<1<<endl;
        return;
    }

    rotate(temp1,temp2);     /*旋转180° */
    if(check(temp2,after))
    {
        cout<<2<<endl;
        return;
    }

    rotate(temp2,temp3);       /*旋转270° */
    if(check(temp3,after))     
    {
        cout<<3<<endl;
        return;
    }

    reflact(before,temp4);         /*反射 */ 
    if(check(temp4,after))
    {
        cout<<4<<endl;
        return;
    }

    rotate(temp4,temp5);           /*反射后旋转90° */
    rotate(temp5,temp6);           /*反射后旋转180° */
    rotate(temp6,temp7);           /*反射后旋转270° */
    if(check(temp5,after) || check(temp6,after) || check(temp7,after))
    {
        cout<<5<<endl;
        return;
    }

    if(check(before,after))
    {
        cout<<6<<endl;
        return;
    }
    cout<<7<<endl;
}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);

    cin>>N;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>before[i][j];
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>after[i][j];
    }
    solve();
    return 0;
}

改后:

/*
ID: your_id_here
TASK: transform
LANG: C++                 
*/
#include<iostream>
#include<cstdio>
using namespace std;

int N;
char before[12][12],after[12][12];

bool check(char a[12][12],char b[12][12])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(a[i][j] != b[i][j])
                return false;
        }
    }
    return true;
}

void rotate(char begin[12][12],char end[12][12])       /*旋转90°,其实这个就是求转置矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            end[j][N-1-i]=begin[i][j];

    }
}

void reflact(char begin[12][12],char end[12][12])      /*求反射矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            end[i][N-j-1]=begin[i][j];
        }
    }
}

void solve()
{
    char temp[8][12][12];
    rotate(before,temp[0]);             /*旋转90° */
    rotate(temp[0],temp[1]);            /*旋转180° */
    rotate(temp[1],temp[2]);            /*旋转270° */
    reflact(before,temp[3]);            /*反射 */ 
    rotate(temp[3],temp[4]);            /*反射后旋转90° */
    rotate(temp[4],temp[5]);            /*反射后旋转180° */
    rotate(temp[5],temp[6]);            /*反射后旋转270° */
    rotate(temp[2],temp[7]);            /*不进行任何变化 */

    for(int i=0;i<8;i++)
    {
        if(check(temp[i],after))
        {
            if(i>=0 && i<=3)
            {
                cout<<i+1<<endl;
                return;
            }
            else if(i>=4 && i<=6)
            {
                cout<<5<<endl;
                return;
            }
            else
            {
                cout<<6<<endl;
                return;
            }
        }
    }
    cout<<7<<endl;
}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);

    cin>>N;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>before[i][j];
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>after[i][j];
    }
    solve();
    return 0;
}

再次修改后:

/*
ID: your_id_here
TASK: transform
LANG: C++                 
*/
#include<iostream>
#include<cstdio>
using namespace std;

int N;
char before[12][12],after[12][12];

void rotate(char begin[12][12],char end[12][12])       /*旋转90°,其实这个就是求转置矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            end[j][N-1-i]=begin[i][j];

    }
}

void reflact(char begin[12][12],char end[12][12])      /*求反射矩阵 */
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            end[i][N-j-1]=begin[i][j];
        }
    }
}

void solve()
{
    char temp[8][12][12];
    rotate(before,temp[0]);             /*旋转90° */
    rotate(temp[0],temp[1]);            /*旋转180° */
    rotate(temp[1],temp[2]);            /*旋转270° */
    reflact(before,temp[3]);            /*反射 */ 
    rotate(temp[3],temp[4]);            /*反射后旋转90° */
    rotate(temp[4],temp[5]);            /*反射后旋转180° */
    rotate(temp[5],temp[6]);            /*反射后旋转270° */
    rotate(temp[2],temp[7]);            /*不进行任何变化 */

    for(int i=0;i<8;i++)
    {
        bool rec=true;
        int j,k;         /*必须在外面申明 */
        for(j=0;j<N;j++)
        {
            for(k=0;k<N;k++)
            {
                if(temp[i][j][k] != after[j][k])
                {
                    rec=false;
                    break;
                }
            }
            if(!rec)
                break;
        }
        if(j==N && k==N)
        {
            if(i>=0 && i<=3)
            {
                cout<<i+1<<endl;
                return;
            }
            else if(i>=4 && i<=6)
            {
                cout<<5<<endl;
                return;
            }
            else 
            {
                cout<<6<<endl;
                return;
            }
        }
    }
    cout<<7<<endl;
}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);

    cin>>N;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>before[i][j];
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
            cin>>after[i][j];
    }
    solve();
    return 0;
}
`tf_transformations` 是一个在机器人系统中广泛使用的库,主要用于处理坐标变换和旋转操作,尤其在 ROS(Robot Operating System)环境中。它提供了一系列函数用于处理变换矩阵、四元数、欧拉角等,是 `tf`(Transform)库的一部分,适用于需要处理 3D 空间变换的应用场景。 ### 功能概述 该库支持多种变换操作,包括但不限于: - **四元数与欧拉角之间的转换**:可以将旋转表示从四元数转换为欧拉角,或者反过来。 - **构建变换矩阵**:能够生成表示平移和旋转的齐次变换矩阵。 - **矩阵分解**:可以从变换矩阵中提取出平移部分和旋转部分。 - **坐标变换**:支持将点或向量从一个坐标系变换到另一个坐标系。 ### 使用方法 #### 安装 在使用 `tf_transformations` 之前,需要确保已经安装了 `tf` 包。通常情况下,在 ROS 环境中,这个库是默认安装的。如果需要手动安装,可以通过以下命令安装: ```bash sudo apt-get install ros-<rosdistro>-tf ``` 请将 `<rosdistro>` 替换为你的 ROS 发行版名称,如 `noetic` 或 `melodic`。 #### 示例代码 下面是一个简单的 Python 示例,展示如何使用 `tf_transformations` 来创建一个变换矩阵并对其进行分解: ```python import tf_transformations import numpy as np # 创建一个表示绕X轴旋转的四元数 quaternion = tf_transformations.quaternion_from_euler(1.5708, 0, 0) # 创建一个齐次变换矩阵 transform_matrix = tf_transformations.compose_matrix( translate=[1, 2, 3], angles=[1.5708, 0, 0] ) # 分解变换矩阵以获取平移和旋转信息 translation, rotation, _, _, _ = tf_transformations.decompose_matrix(transform_matrix) print("Translation:", translation) print("Rotation (quaternion):", rotation) ``` ### 常见问题及解决方案 1. **如何将四元数转换为欧拉角?** ```python euler_angles = tf_transformations.euler_from_quaternion(quaternion) ``` 2. **如何将欧拉角转换为四元数?** ```python quaternion = tf_transformations.quaternion_from_euler(roll, pitch, yaw) ``` 3. **如何创建一个齐次变换矩阵?** ```python transform_matrix = tf_transformations.compose_matrix( translate=[x, y, z], angles=[roll, pitch, yaw] ) ``` 4. **如何从齐次变换矩阵中提取平移和旋转信息?** ```python translation, rotation, _, _, _ = tf_transformations.decompose_matrix(transform_matrix) ``` ### 注意事项 - 在使用 `tf_transformations` 时,需要注意输入参数的单位,例如角度通常是以弧度为单位。 - 确保所有操作都在同一个坐标系下进行,否则需要进行坐标变换。 - 如果遇到性能瓶颈,可以考虑使用 NumPy 的向量化操作来优化代码。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值