C++ 2D 图形 : Flat Bottom Triangle Rasterization

标签 c++ 2d rasterizing

我正在尝试使用 C++ 在 Windows 控制台上构建一个简单的 2D 应用程序来显示各种图元,因此我从最基本的一个开始:三角形。我可以正确显示三角形的轮廓和顶点,但在填充它时遇到问题。我遇到过一些此处介绍的光栅化算法:http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html并决定我会采用第一种方法。我复制了平底三角形案例的代码,但这似乎在我的应用程序上无法正常工作,而且我不知道问题的原因是什么。

我的结果:

My result

ma​​in()

screen.fillFlatBottomTriangle(30,10, 10, 140/4, 256/2, 140/4, block_char, FG_CYAN);
screen.drawTriangle(30,10, 10, 140/4, 256/2, 140/4, block_char, BG_DARK_GREY);

screen.drawPixel(30,10,block_char,FG_RED);
screen.drawPixel(10,140/4,block_char,FG_GREEN);
screen.drawPixel(256/2,140/4,block_char,FG_MAGENTA);

填充FlatBottomTriangle

//http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
void Screen::fillFlatBottomTriangle(int x1, int y1, int x2, int y2, int x3, int y3, short character, short color)
{
    float invslope1 = (x2 - x1) / (y2 - y1);
    float invslope2 = (x3 - x1) / (y3 - y1);

    float curx1 = x1;
    float curx2 = x1;

    for (int scanlineY = y1; scanlineY <= y2; scanlineY++)
    {
        this->drawLine((int)curx1, scanlineY, (int)curx2, scanlineY, character, color);
        curx1 += invslope1;
        curx2 += invslope2;
    }
}

画线

// https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)
void Screen::drawLine(int x1, int y1, int x2, int y2, short character, short color)
{
    float delta_x = x2 - x1;
    float delta_y = y2 - y1;
    float step;

    if (abs(delta_x) >= abs(delta_y))
        step = abs(delta_x);
    else 
        step = abs(delta_y);

    
    delta_x = delta_x / step;
    delta_y = delta_y / step;
    

    float x = x1;
    float y = y1;

    for (int i = 1; i <= step; i++)
    {
        this->drawPixel((int)x, (int)y, character, color);
        x += delta_x;
        y += delta_y;
    }
}

绘制三角形

void Screen::drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, short character, short color)
{
    this->drawLine(x1,y1, x2, y2, character, color);
    this->drawLine(x1,y1, x3, y3, character, color);
    this->drawLine(x2,y2, x3, y3, character, color);
}

最佳答案

请参阅这部分:(x2 - x1)/(y2 - y1)

由于这里的所有变量都是整数,所以这是整数除法。也就是说,除法的结果向 0 舍入。将结果分配给 float 不会改变这一点。

要进行浮点除法,您应该将至少一个操作数转换为浮点:(x2 - x1)/(float) (y2 - y1)

您当前的代码将其中一个反斜率舍入为 0,从而在左侧产生垂直线。另一侧的 x 值也增加得不够快,因为该部分也在下方进行了舍入。

关于C++ 2D 图形 : Flat Bottom Triangle Rasterization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65524726/

相关文章:

c++ - 模板作为参数类型

c++ - 带余数的动态二维数组访问冲突

python - Gdal_rasterize nodata_value 不起作用

c++ - Xcode C++ 独立文件未编译

c# - 通过代码更改音频窗口设置(从设置应用程序)

c++ - 使用 *this 与使用 & 运算符的基对象和派生对象的地址差异

c# - 一条线穿过的所有点

animation - 游戏/动画模型/原型(prototype)工具

用于绘制具有透明度的 2d 的 C++ 快速库

c++ - 沿光栅化圆弧遍历像素