【计算机图形学】图形变换(平移变换、比例变换、旋转变换、对称变换、错切变换、复合变换)

一 实验目的

  1. 编写图形各种变换的算法

二 实验内容

1:自行设计基本图案,完成1-5种简单变换

实验结果如下图所示:
图形初始化:


第一次点击左键,实现平移变换:

 

 
第二次点击左键,实现比例变换(同时伴有平移变换):

 
第三次点击左键,实现对称变换(以平行y轴方向的直线为对称轴):

 
第四次点击左键,实现对称变换(以平行x轴方向的直线为对称轴):


第五次点击左键,实现错切变换(沿x轴方向关于y错切):


第六次点击左键,实现错切变换(沿y轴方向关于x错切):

2:在实验题3-1的基础上实现多步复合变换,设计动画效果

代码来源:https://blog.csdn.net/weixin_42815846/article/details/113099023

实验结果如下图所示:
初始化界面:

 按任意键后出现动画效果:

三 程序说明

最终的实验代码如下表所示:

1

//

// 程序名称:实验3-1

// 功    能:实现预设图像的平移变换、比例变换等变换

// 编译环境:VS2019,EasyX_20220116

// 最后修改:2022-4-5

#include <graphics.h>

#include <conio.h>

#include <iostream>

#include <math.h>

using namespace std;

#define pi 3.1415926535

int main() {

    POINT t1[] = { {200,200} , {200,20} , {220,80} };

    POINT t2[] = { {200,200} , {200,20} , {180,80} };

    int len = 3;

    float Tx = 50, Ty = 50;//平移

    float Sx = 0.5, Sy = 0.5;//比例

    float angle = 45 * pi / 180;//旋转,没做出来QwQ

    float C = 0.5, B = -0.5;//错切

    //initialize graph

    initgraph(640, 480);

    //勾画初始图案

    setfillcolor(RED);

    fillpolygon(t1, 3);

    polygon(t2, 3);

    //record the times of changing

    int times = 0;

    ExMessage m;

    //全是单步变换。

    while (1) {

         m = getmessage(EX_MOUSE | EX_KEY);

         float cur1x[3], cur1y[3], cur2x[3], cur2y[3];

         //平移

         if (m.message == WM_LBUTTONDOWN && times == 0) {

             for (int i = 0; i < len; i++) {

                  t1[i].x += Tx;

                  t1[i].y += Ty;

                  t2[i].x += Tx;

                  t2[i].y += Ty;

             }

             setfillcolor(RED);

             fillpolygon(t1, 3);

             polygon(t2, 3);

             times++;

         }

         //比例

         else if (m.message == WM_LBUTTONDOWN && times == 1) {

             for (int i = 0; i < len; i++) {

                  t1[i].x += 50;

                  t2[i].x += 50;

             }

             //以一顶点为缩放点

             for (int i = 1; i < len; i++) {

                  t1[i].x = Sx * (t1[i].x - t1[0].x) + t1[0].x;

                  t2[i].x = Sx * (t2[i].x - t2[0].x) + t2[0].x;

                  t1[i].y = Sy * (t1[i].y - t1[0].y) + t1[0].y;

                  t2[i].y = Sy * (t2[i].y - t2[0].y) + t2[0].y;

             }

             setfillcolor(RED);

             fillpolygon(t1, 3);

             polygon(t2, 3);

             times++;

         }

         //对称 about x axis

         else if (m.message == WM_LBUTTONDOWN && times == 2) {

             float mid = t1[0].y;

             for (int i = 0; i < len; i++) {

                  t1[i].y = 2 * mid - t1[i].y;

                  t2[i].y = 2 * mid - t2[i].y;

             }

             setfillcolor(RED);

             fillpolygon(t1, 3);

             polygon(t2, 3);

             times++;

         }

         //对称 about y axis

         else if (m.message == WM_LBUTTONDOWN && times == 3) {

             float midx = t1[2].x;

             for (int i = 0; i < len; i++) {

                  t1[i].x = 2 * midx - t1[i].x;

                  t2[i].x = 2 * midx - t2[i].x;

                  //float cur1x[3], cur1y[3], cur2x[3], cur2y[3];

                  cur1x[i] = t1[i].x;

                  cur1y[i] = t1[i].y;

                  cur2x[i] = t2[i].x;

                  cur2y[i] = t2[i].y;

             }

             setfillcolor(RED);

             fillpolygon(t1, 3);

             polygon(t2, 3);

             times++;

         }

         //错切 沿x轴方向关于y错切(x = x + cy)

         else if (m.message == WM_LBUTTONDOWN && times == 4) {

             for (int i = 0; i < len; i++) {

                  t1[i].x += C * t1[i].y;

                  t2[i].x += C * t2[i].y;

             }

             setfillcolor(RED);

             fillpolygon(t1, 3);

             polygon(t2, 3);

             times++;

         }

         //错切 沿y轴方向关于x错切(y = y + bx)

         else if (m.message == WM_LBUTTONDOWN && times == 5) {

             for (int i = 0; i < len; i++) {

                  /*

                  //float cur1x[3], cur1y[3], cur2x[3], cur2y[3];

                  cur1x[i] = t1[i].x;

                  cur1y[i] = t1[i].y;

                  cur2x[i] = t2[i].x;

                  cur2y[i] = t2[i].y;

                  */

                  //t1[i].y += B * t1[i].x;

                  //t2[i].y += B * t2[i].x;

                  t1[i].y = cur1y[i] + B * cur1x[i];

                  t1[i].x = cur1x[i];

                  t2[i].y = cur2y[i] + B * cur2x[i];

                  t2[i].x = cur2x[i];

             }

             setfillcolor(RED);

             fillpolygon(t1, 3);

             polygon(t2, 3);

             times++;

         }

    }

    _getch();

    closegraph();

    return 0;

}

2

//

// 程序名称:实验3-2

// 功    能:实现预设图像的复合变换

// 编译环境:VS2019,EasyX_20220116

// 最后修改:2022-4-5

#include <graphics.h>

#include <conio.h>

#include <iostream>

#include <math.h>

#include <malloc.h>

#include <stdio.h>

using namespace std;

#define PI 3.1415926535

int dimension = 3, num = 4;

double points[50][2] = { {150,150},{150,200},{200,200},{200,150} };

//初始化

void initialize() {

    initgraph(800, 640);

    setbkcolor(WHITE);

    setcolor(WHITE);

    fillrectangle(0, 0, 800, 640);

    setcolor(BLACK);

    line(0, 80, 800, 80);

    setcolor(BLACK);

    line(0, 80, 800, 80);

    //说明框矩形

    RECT r = { 0,0,800,80 };

    drawtext(_T("\n\n依次展示旋转,放大,平移,关于直线对称,关于x错切"), &r, DT_CENTER | DT_VCENTER);

    HRGN rgn = CreateRectRgn(1, 81, 799, 639);

    setcliprgn(rgn);

   

    setcolor(BLACK);

    rectangle(0, 0, 800, 640);

    setcolor(RED);

}

//矩阵乘法

void multiply(double a[5][5], int ar, int ac, double b[5][5], int br, int bc) {

    if (ac != br) {

         cout << "matrix invalid";

         return;

    }

    double c[5][5];

    for (int i = 0; i < ar; i++) {

         for (int j = 0; j < bc; j++) {

             c[i][j] = 0;

         }

    }

    for (int i = 0; i < ar; i++) {

         for (int j = 0; j < bc; j++) {

             for (int k = 0; k < ac; k++) {

                  c[i][j] += a[i][k] * b[k][j];

             }

         }

    }

    for (int i = 0; i < ar; i++) {

         for (int j = 0; j < bc; j++) {

             b[i][j] = c[i][j];

         }

    }

}

//平移变换

void trans(double tx, double ty) {

    double T[5][5] = { {1,0,tx},{0,1,ty},{0,0,1} };

    double point[5][5];

    for (int i = 0; i < num; i++) {

         point[0][0] = points[i][0];

         point[1][0] = points[i][1];

         point[2][0] = 1;

         multiply(T, dimension, dimension, point, dimension, 1);

         points[i][0] = point[0][0];

         points[i][1] = point[1][0];

    }

}

//旋转变换

void rotate(double degree) {

    double theta = degree / 180 * PI;

    double R[5][5] = { {cos(theta),-sin(theta),0},{sin(theta),cos(theta),0},{0,0,1} };

    double point[5][5];

    for (int i = 0; i < num; i++) {

         point[0][0] = points[i][0];

         point[1][0] = points[i][1];

         point[2][0] = 1;

         multiply(R, dimension, dimension, point, dimension, 1);

         points[i][0] = point[0][0];

         points[i][1] = point[1][0];

    }

}

//缩放变换

void scale(double sx, double sy) {

    double S[5][5] = { {sx,0,0},{0,sy,0},{0,0,1} };

    double point[5][5];

    for (int i = 0; i < num; i++) {

         point[0][0] = points[i][0];

         point[1][0] = points[i][1];

         point[2][0] = 1;

         multiply(S, dimension, dimension, point, dimension, 1);

         points[i][0] = point[0][0];

         points[i][1] = point[1][0];

    }

}

//对称变换

void symmetry(int flag) {

    if (flag == 0) {

         for (int i = 0; i < num; i++) {

             points[i][1] = -points[i][1];

         }

    }

    else if (flag == 1) {

         for (int i = 0; i < num; i++) {

             points[i][0] = -points[i][0];

         }

    }

    else {

         return;

    }

}

void anysymmetry(int x1, int y1, int x2, int y2) {

    double k = 0, b = 0;

    if (x1 == x2) {

         trans(-x1, 0);

         symmetry(1);

         trans(x1, 0);

    }

    else if (y1 == y2) {

         trans(0, -y1);

         symmetry(0);

         trans(0, y1);

    }

    else {

         k = double((y1 - y2) / (x1 - x2));

         b = y1 - k * x1;

         trans(0, -b);

         rotate(-atan(k) * 180 * 1.0 / PI);

         symmetry(0);

         rotate(atan(k) * 180 * 1.0 / PI);

         trans(0, b);

    }

}

//输出图形

void paint() {

    for (int i = 0; i < num; i++) {

         if (i == num - 1) {

             line(points[i][0], points[i][1], points[0][0], points[0][1]);

             break;

         }

         line(points[i][0], points[i][1], points[i + 1][0], points[i + 1][1]);

    }

}

//错切变换

void cut(int flag, int degree) {

    double point[5][5];

    if (flag == 0) {

         double C[5][5] = { {1,0,0},{tan(double(degree) / 180 * PI),1,0},{0,0,1} };

         for (int i = 0; i < num; i++) {

             point[0][0] = points[i][0];

             point[1][0] = points[i][1];

             point[2][0] = 1;

             multiply(C, dimension, dimension, point, dimension, 1);

             points[i][0] = point[0][0];

             points[i][1] = point[1][0];

         }

    }

    else if (flag == 1) {

         double C[5][5] = { {1,tan(double(degree) / 180 * PI),0},{0,1,0},{0,0,1} };

         for (int i = 0; i < num; i++) {

             point[0][0] = points[i][0];

             point[1][0] = points[i][1];

             point[2][0] = 1;

             multiply(C, dimension, dimension, point, dimension, 1);

             points[i][0] = point[0][0];

             points[i][1] = point[1][0];

         }

    }

    else if (flag == 2) {

         double C[5][5] = { {1,tan(double(degree) / 180 * PI),0},{tan(double(degree) / 180 * PI),1,0},{0,0,1} };

         for (int i = 0; i < num; i++) {

             point[0][0] = points[i][0];

             point[1][0] = points[i][1];

             point[2][0] = 1;

             multiply(C, dimension, dimension, point, dimension, 1);

             points[i][0] = point[0][0];

             points[i][1] = point[1][0];

         }

    }

    else {

         return;

    }

}

//复合变换

void multitrans() {

    paint();

    int i = 50;

    while (i > 0) {

         i--;

         Sleep(50);

         clearcliprgn();

         trans(-150, -150);

         rotate(-20);

         trans(150, 150);

         paint();

    }

    i = 8;

    while (i > 0) {

         i--;

         Sleep(250);

         scale(1.1, 1.1);

         clearcliprgn();

         paint();

    }

    i = 40;

    while (i > 0) {

         i--;

         Sleep(50);

         trans(-1, -1);

         clearcliprgn();

         paint();

    }

    i = 4;

    while (i > 0) {

         i--;

         Sleep(250);

         anysymmetry(250, 100, 300, 560);

         clearcliprgn();

         setcolor(BLACK);

         line(250, 100, 300, 560);

         setcolor(RED);

         paint();

    }

    i = 20;

    while (i > 0) {

         i--;

         Sleep(150);

         cut(0, 1);

         clearcliprgn();

         paint();

    }

}

//主函数

int main() {

    initialize();

    _getch();

    multitrans();

    _getch();

    closegraph();

    return 0;

}

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MFC(Microsoft Foundation Classes)是一个基于C++的应用程序框架,可以在Windows操作系统上创建可视化窗口应用程序。在MFC中,可以使用GDI(图形设备接口)来实现基本图形变换。 下面简单介绍一下MFC实现基本图形变换的方法: 1. 错切变换 错切变换可以使图形在水平或垂直方向上倾斜。在MFC中,可以使用CBitmap和CDC类来实现错切变换。 ```c++ CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); CDC dcMemory; dcMemory.CreateCompatibleDC(NULL); dcMemory.SelectObject(&bitmap); // 水平错切 XFORM xform = { 1, 0.5, 0, 1, 0, 0 }; dc.SetWorldTransform(&xform); dc.BitBlt(0, 0, 200, 200, &dcMemory, 0, 0, SRCCOPY); // 垂直错切 XFORM xform = { 1, 0, 0.5, 1, 0, 0 }; dc.SetWorldTransform(&xform); dc.BitBlt(0, 0, 200, 200, &dcMemory, 0, 0, SRCCOPY); ``` 2. 平移变换 平移变换可以使图形在水平或垂直方向上移动。在MFC中,可以使用CBitmap和CDC类来实现平移变换。 ```c++ CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); CDC dcMemory; dcMemory.CreateCompatibleDC(NULL); dcMemory.SelectObject(&bitmap); // 水平平移 dc.BitBlt(100, 0, 200, 200, &dcMemory, 0, 0, SRCCOPY); // 垂直平移 dc.BitBlt(0, 100, 200, 200, &dcMemory, 0, 0, SRCCOPY); ``` 3. 旋转变换 旋转变换可以使图形绕着一个旋转一定角度。在MFC中,可以使用CBitmap和CDC类来实现旋转变换。 ```c++ CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); CDC dcMemory; dcMemory.CreateCompatibleDC(NULL); dcMemory.SelectObject(&bitmap); // 绕中心点旋转 XFORM xform; xform.eM11 = cos(30 * PI / 180); xform.eM12 = sin(30 * PI / 180); xform.eM21 = -sin(30 * PI / 180); xform.eM22 = cos(30 * PI / 180); xform.eDx = 100; xform.eDy = 100; dc.SetWorldTransform(&xform); dc.BitBlt(0, 0, 200, 200, &dcMemory, 0, 0, SRCCOPY); ``` 4. 比例变换 比例变换可以使图形在水平或垂直方向上缩放。在MFC中,可以使用CBitmap和CDC类来实现比例变换。 ```c++ CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); CDC dcMemory; dcMemory.CreateCompatibleDC(NULL); dcMemory.SelectObject(&bitmap); // 水平缩放 XFORM xform = { 2, 0, 0, 1, 0, 0 }; dc.SetWorldTransform(&xform); dc.BitBlt(0, 0, 200, 200, &dcMemory, 0, 0, SRCCOPY); // 垂直缩放 XFORM xform = { 1, 0, 0, 2, 0, 0 }; dc.SetWorldTransform(&xform); dc.BitBlt(0, 0, 200, 200, &dcMemory, 0, 0, SRCCOPY); ``` 5. 对称变换 对称变换可以使图形关于某条轴对称。在MFC中,可以使用CBitmap和CDC类来实现对称变换。 ```c++ CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP1); CDC dcMemory; dcMemory.CreateCompatibleDC(NULL); dcMemory.SelectObject(&bitmap); // 水平对称 XFORM xform = { -1, 0, 0, 1, 0, 0 }; dc.SetWorldTransform(&xform); dc.BitBlt(0, 0, 200, 200, &dcMemory, 0, 0, SRCCOPY); // 垂直对称 XFORM xform = { 1, 0, 0, -1, 0, 0 }; dc.SetWorldTransform(&xform); dc.BitBlt(0, 0, 200, 200, &dcMemory, 0, 0, SRCCOPY); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MorleyOlsen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值