2013-10-29 56 views
1

任務是在800列和600行的圖像上創建俄羅斯國旗的圖像。因此,標誌分爲三個等分(白色之上,藍色在中間,並衝在底部)爲什麼我的ppm圖像文件有點偏離?

這裏是我的代碼:

#include <stdio.h> 

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

    int width, heightWhite, heightBlue, heightRed, i, j; 
    unsigned char Rcolor, Bcolor, Gcolor; 

    width = 800; 
    heightWhite = 200; 
    heightBlue = 400; 
    heightRed = 600; 

    for (j = 0; j <= heightWhite; j++) { 
     for (i = 0; i <= width; i++) { 
     Rcolor = 255; 
     Bcolor = 255; 
     Gcolor = 255; 

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

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

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

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

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

    return (0); 
} 

但是,當我看着所產生的圖像我的程序中,我注意到藍色和紅色條的頂部並不是完全水平的(它看起來像是行的一部分,使得藍色和紅色條的頂部比前面的像素高)我無法確定爲什麼我得到這個。我已經在Gimp上運行了我的教師的ppm文件(這是我用來查看ppm文件的內容),線條應該是完美的。有任何想法嗎?

(我不知道如何安裝我的PPM文件,但在這裏就是它應該是這樣的:http://users.csc.calpoly.edu/~dekhtyar/101-Fall2013/labs/lab7.html)(這是第一個標誌)

回答

0

打印一個多像素的每一行(中每種顏色的最後一行打印200像素以上)。

變化

for (i = 0; i <= width; i++) { 

for (i = 0; i < width; i++) { 

編輯:

但怎麼來的,我可以說 「< =」 的高度?

for (j = 0; j < heightWhite; j++) = 0...199 = 200 items 
for (j = 1; j <= heightWhite; j++) = 1...200 = 200 items 

請注意,所有的代碼可以用兩個循環來執行:

#include <stdio.h> 

int main(void) 
{ 
    int width = 800, height = 600, icolor = 0, i, j; 
    unsigned char color[][3] = { 
     {255, 255, 255}, /* white */ 
     {0, 0, 255},  /* blue */ 
     {255, 0, 0}  /* red */ 
    }; 

    printf("P6\n"); 
    printf("%d %d\n", width, height); 
    printf("255\n"); 

    for (i = 0; i < height; i++) { 
     for (j = 0; j < width; j++) { 
     printf("%c%c%c", color[icolor][0], color[icolor][1], color[icolor][2]); 
     } 
     if (i && (i % 200 == 0)) icolor++; /* 200, 400, 600 */ 
    } 
    return 0; 
} 
+0

啊我明白了!只是好奇,但我怎麼能說「<=」的高度?謝謝您的幫助! – Karen

+0

@凱倫,你有一個編輯 –

+0

我認爲你的模數技巧有點太聰明。明確地說,對於每種顏色,用三個循環來表示不同的寬度會使IMO更容易理解,並且可以處理不同寬度的色塊。 – shodanex

相關問題