2013-04-25 56 views
0

我應該打開這個事實,我對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; 
} 
+1

如果您正在使用UNIX,看看詛咒 – didierc 2013-04-25 03:45:50

+0

'系統(「暫停」);'指示的Windows和Visual Studio。嘗試Ctrl-F5而不是暫停的調用。 – harper 2013-04-25 03:53:02

+0

您是否試圖在圖像中顯示矩陣的條目(即,您可以看到實際的數字),還是試圖通過對數字進行顏色編碼來了解矩陣中的內容(即,0是純黑色的255是純白色的)?如果是後者,像OpenCV這樣的圖像處理庫可能會有用。 – lightalchemist 2013-04-25 04:10:10

回答

0

您應該location[2]重命名爲類似`{結構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] = '\0'; 

    // use the image   
    puts((char*)image); 
} 

請根據需要添加隨機功能。

0

如果要將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格式