有一种方法来显示字符的矩阵作为一个图像

Is there a way to display a matrix of chars as an image?

本文关键字:一个 图像 显示 一种 方法 字符      更新时间:2023-10-16

我应该首先说明我对c++是一个新手。

我试图显示一个不断更新的20x20字符矩阵。目前,我正在使用for循环显示矩阵作为cout(下面的代码),但这是令人难以置信的闪烁-我正在寻找更流畅的东西。

是否有办法将这个字符矩阵转换成图像并显示?

这是我的第一个问题,如果我做错了什么,我道歉!

目前代码:

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main()
{
    int randInt;
    //Initialize matrix and location
    int matrix[20][20];
    int location[2] = {0,0};
    for (int i=0; i<20; i++)
    {
            for (int j=0; j<20; j++)
            {
                matrix[i][j] = 1;
            }
    }

    //move the X around
    for (int i=0; i<100; i++)
    {
        cout << string(50, 'n');
        //Change the X's location
        randInt = rand() % 4;
        switch (randInt)
        {
            case 0:
                if(location[1] > 0)
                    location[1] = location[1]-1;
                break;
            case 1:
                if(location[0] < 20)
                    location[0] = location[0]+1;
                break;
            case 2:
                if(location[1] < 20)
                    location[1] = location[1]+1;
                break;
            case 3:
                if(location[0] > 0)
                    location[0] = location[0]-1;
                break;
            default:
                cout << "Switch statement problem";
        }
        //Display the matrix
        for (int x=0; x<20; x++)
        {
            for (int y=0; y<20; y++)
            {
                if(x==location[0] && y==location[1])
                    cout << "X";
                else
                    cout << matrix[x][y];
            }
            cout << endl;
        }
        Sleep(100);
    }
system ("pause");
return 0;
}

您应该将location[2]重命名为' struct {int x,y;}位置的可读性。

那么你可以在RAM中建立一个字符数组并立即输出。

int _tmain(int argc, _TCHAR* argv[])
{
    char matrix[20][20];
    char image[21][21];
    struct { int x, y; } location;
    int x = 0;
    int y = 0;
    location.x = 7;
    location.y = 3;
    // fill the matrix
    for (x = 0; x < 20; ++x)
    {
        for (y = 0; y < 20; ++y)
        {
            matrix[y][x] = 'a' + x + y;
        }
    }
    // prepare the image
    y = 0;
    while (y < 20)
    {
        memcpy(image[y], matrix[y], 20);
        image[y][20] = 'n';
        ++y;
    }
    // add the cross
    image[location.y][location.x] = 'X';
    image[20][0] = '';
    // use the image        
    puts((char*)image);
}

请根据需要添加随机功能

如果您想将char转换为图像并查看颜色含义,请将char值以简单的pgm格式写入像素。

用下面的示例格式写一个文件

P2
# feep.pgm
24 7
15
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

查看此链接http://netpbm.sourceforge.net/doc/pgm.html获取PGM格式