c - PPM 中显示的矩形中的 4 个三角形

标签 c ppm

我试图根据用户输入的矩形参数生成 4 个三角形。我的想法是获得从矩形中心到原点的斜率,在 ppm 文件中原点位于左上角。这导致一切都从本质上逆转。不管怎样,我相信我遇到的麻烦是来 self 的主函数中的嵌入式循环,但我不确定。我的输出是 3 个三角形,其中左侧的三角形超出了底部的三角形,就好像它一直紧贴斜坡一样。

输出通过管道传送到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;
}

最佳答案

您实际上写入了太多数据。用这个替换主循环,一切都应该没问题;)

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);
    }
}

关于c - PPM 中显示的矩形中的 4 个三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12833088/

相关文章:

c - 在ARM Linux中,每个线程在内核堆栈的 "bottom"处保留的几个字节的目的是什么

c - 为什么读写管道时需要关闭fds?

c - 初始化结构体成员

c - 如何正确将RGB颜色保存到ppm文件?

用于安装多个 perl 模块的 Windows 批处理文件 - 仅运行第一行

c - 逻辑或 C 编程语言

摄氏度到华氏度的转换总是在摄氏度下显示零

c - 在C中将PPM从RGB转换为HSL

c - ppm 文件的图像卷积

c++ - 如何在 C++ 中将 image.jpg 转换为 image.ppm