2013-10-29 43 views
0

所以,我必須創建一個ppm文件,它會給我意大利國旗的圖像(從左到右,綠色,白色,然後紅色的順序是3個垂直條)。圖像必須是600×400(逐行)我試過多次重寫我的代碼,但是,我的圖像只是水平放置的三個橫條而不是垂直。此外,線條並不完全平坦。但最大的問題是,爲什麼我的綠色,白色和紅色酒吧不是垂直的?任何幫助是極大的讚賞。爲什麼我的圖片不符合我期望的樣子?

這裏是我的代碼:

#include <stdio.h> 

int main() { 
    printf("P6\n"); 
    printf("%d %d\n", 600, 400); 
    printf("255\n"); 

    int height, widthGreen, widthWhite, widthRed, i, j; 
    unsigned char Rcolor, Bcolor, Gcolor; 

    widthGreen = 200; 
    widthWhite = 400; 
    widthRed = 600; 
    height = 400; 

    for (j = 0; j < height; j++) { 
     for (i = 0; i < widthGreen; i++) { 
     Rcolor = 0; 
     Gcolor = 128; 
     Bcolor = 0; 

     printf("%c%c%c", Rcolor, Gcolor, Bcolor); 
     } 
    } 

    for (j = 0; j < height; j++) { 
     for (i = 201; i <= widthWhite; i++) { 
     Rcolor = 255; 
     Gcolor = 255; 
     Bcolor = 255; 

     printf("%c%c%c", Rcolor, Gcolor, Bcolor); 
     } 
    } 

    for (j = 0; j < height; j++) { 
     for (i = 401; i <= widthRed; i++) { 
     Rcolor = 255; 
     Gcolor = 0; 
     Bcolor = 0; 

     printf("%c%c%c", Rcolor, Gcolor, Bcolor); 
     } 
    } 

    return (0); 
} 
+0

可能重複[爲什麼我的圖像從我的ppm文件有點關閉?](http://stackoverflow.com/questions/19651249/why-is-my-image-from-my-ppm-file -a,小關) – shodanex

回答

0

四件事情:第一是值之間加空格。第二個是在每行之後添加新行。第三種是將值打印爲(無符號)整數,而不是作爲字符,255128都不會打印爲有效字符。第四個是使用一個環路的高度,其中有三個環路用於顏色。將循環計數器視爲像素,而您會理解爲什麼。

0

你爲什麼使用printf?您應該使用i和j索引來構建並索引到數組中。在您的代碼中,i和j變量僅用作循環計數器,而不是座標。

你在做什麼:

for each line 
    print some green 

for each line 
    print some white 

for each line 
    print some red 

什麼,你應該做的:

for each line 
    print some green 
    print some white 
    print some red 

您也可以使用更有意義的變量名,如行,而不是J和COL(列),而不是我

相關問題