2012-10-11 2 views
3

사각형에 대한 사용자 입력 매개 변수를 기반으로 4 개의 삼각형을 생성하려고합니다. 내 생각은 직사각형의 중심에서 원점으로 기울기를 얻는 것입니다. ppm 파일은 왼쪽 상단에 있습니다. 이로 인해 모든 것이 근본적으로 바뀌게됩니다. 어쨌든 필자는 문제가 내 주요 기능의 임베디드 루프에서 발생한다고 생각하지만 확실하지 않습니다. 내 출력은 왼쪽에있는 삼각형이 사면을 계속 껴안고있는 것처럼 아래쪽 삼각형을 범람하는 3 개의 삼각형 중 하나입니다.사각형에 4 개의 삼각형이 PPM에 표시됩니다.

출력이 file.ppm으로 파이프되어 표시됩니다.

#include <stdio.h> 
#include <math.h> 

void make_header (int width, int height); 
void make_pixel (unsigned char r, unsigned char g, unsigned char b); 
void print_Row (float yValue, int width, int height); 
void print_Row2 (float yValue, int width, int height); 

void make_header(int width, int height) 
{ 
    fprintf(stdout,"P6\n"); 
    fprintf(stdout,"%d %d 255\n",width,height); 
} 

void make_pixel (unsigned char r, unsigned char g, unsigned char b) 
{ 
    fprintf(stdout,"%c%c%c", r,g,b); 
} 

void print_Row(float yValue, int width, int height) 
{ 
    int x; 

    for(x=0;x<yValue;x++) 
    { 
     make_pixel(255,0,0); // left triangle 
    } 


    for(x=x; x<width-yValue; x++) 
    { 
     make_pixel(0,255,0); //top triangle 
    } 

    for(x=x;x<width;x++) 
    { 
     make_pixel(0,0,255); // right triangle 
    } 
} 

void print_Row2(float yValue, int width, int height) 
{ 
    int x; 

    for(x=0;x<yValue;x++) 
    { 
     make_pixel(255,0,0); 
    } 

    for(x=x;x<width-yValue;x++) 
    { 
     make_pixel(0,0,0); //bottom triangle 
    } 

    for(x=x;x<width;x++) 
    { 
     make_pixel(0,0,255); 
    } 
} 

int main() 
{ 
    float slope, inv_slope, width_midpoint, height_midpoint, yValue ; 
    int width,height,x,b; 

    fprintf(stderr, "\nEnter width: "); 
    scanf("%d", &width); 
    fprintf(stderr, "\nEnter height: "); 
    scanf("%d", &height); 
    make_header(width, height); 

    width_midpoint = width/2; 
    height_midpoint = height/2; 
    slope = (height_midpoint -0)/(width_midpoint - 0); 
    inv_slope = pow(slope,-1); 


    for(b=0;b<height_midpoint;b++) 
    { 
     for(x=0;x<width;x++) 
     { 
      yValue = (inv_slope)*(x) + (b); 
      print_Row(yValue, width, height); 
     } 
    } 

    for(b=height_midpoint; b<=height; b++) 
    { 
     for(x=0;x<width;x++) 
     { 
      yValue = (-1*(slope))*(x)+(b); 
      print_Row2(yValue,width,height); 
     } 
    } 

    return 0; 
} 

답변

1

실제로 많은 양의 데이터를 쓰고 있습니다. 메인 루프를 이것으로 바꾸면 모든 것이 정상이됩니다.)

for(b=0;b<height_midpoint;b++) 
{ 
    for(x=0;x<width;x++) 
    { 
     if(x < (b/slope)) { 
      make_pixel(255,0,0); 
     } 
     else if(x > (width - (b/slope))) { 
      make_pixel(0,255,0); 
     } 
     else 
      make_pixel(0,0,255); 
    } 
} 

for(b=height_midpoint; b<=height; b++) 
{ 
    for(x=0;x<width;x++) 
    { 
     if(x > (b/slope)) { 
      make_pixel(0,255,0); 
     } 
     else if(x > (width - (b/slope))) { 
      make_pixel(0,0,0); 
     } 
     else 
      make_pixel(255,0,0); 
    } 
} 
+0

와우 나는 정말 어리 석다. 정말 고맙습니다. –

+0

걱정할 필요가 없습니다. 어쨌든 문제는 사각형의 위아래 반으로 나누어서 올바른 길을 가고있었습니다. "if (x> (foo/bar))"를 올바른 순서로 수행하도록 조심하십시오. 그렇지 않으면 똑같은 문제가 발생할 것입니다. – wldsvc

관련 문제