简体   繁体   English

固定线栅格化算法的近似值

[英]Fixing the approximations of a line rasterization algorithm

I'm currently making an header for making simple graphics in the c++ console. 我目前正在为在C ++控制台中制作简单图形制作标题。 2 days ago i added a function to draw lines using the rasterization algorithm used here . 2天前,我添加了使用此处使用的栅格化算法绘制线条的功能。

But i have a problem: since the console's cartesian plane works only with integers, my function doesn't draw anithing when the numbers given if approximated are equal to 0, so i was wandering if you could do something like this: 但是我有一个问题:由于控制台的笛卡尔平面仅适用于整数,因此如果给定的数字近似为0时,我的函数不会绘制出anith,所以我在徘徊,如果您可以执行以下操作:

if ( y == 0 ) 
{ 
    //fix using some kind of 'forecast' of what y could be
}

so here is my code: 所以这是我的代码:

void Engine::line(int ax, int ay, int bx, int by, int color)
{

    int i = 0;

    if(ax < bx)
        i = 1;
    if(ax > bx)
        i = -1;


    int dx = bx - ax;
    int dy = by - ay;

    for (int x = ax; x != bx; x+=i)
    {
        int y = ay + (by - ay) * (x - ax)/(bx - ax);

        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hConsole, color);

        Engine::gotoxy(x,y); printf("%c", 219);
    }
}

And here is my output, where if the line is inclined a lot it doesn't properly show: image 这是我的输出,如果线条倾斜太多,则无法正确显示: image

I really hope you can help me, but if not, can you link me a better algorithm that is still simple but that works fine with integers? 我真的希望您能为我提供帮助,但如果不能,您能否为我提供一个更好的算法,该算法仍然很简单,但可以很好地使用整数? (not the Bresenham's one) (不是布雷森汉姆的那个)

The traditional approach is to write your algorithm 2, 4, or 8 times depending on the octant of the diagonal you want to draw. 传统方法是根据要绘制的对角线的八分圆数来编写算法2、4或8次。 basically, when |dx| 基本上,当| dx | > |dy| > | dy | you step by 1 in x. 您将x步进1。 When |dx| 当| dx | < |dy| <| dy | you step by 1 in y. 您在y中步进1。

As to what you have done in the loop, that looks the same as about the 6th equation on the wiki for Bresenham's, and it should be OK because you do all your multiplies before all your divide, but is doing all those multiplies and divides, which the latacode snippets avoid. 至于您在循环中所做的事情,与Bresenham's Wiki上的第六方程式相同,应该没问题,因为您在所有除法之前进行了所有乘法运算,但是正在进行所有这些乘法和除法运算, latacode代码段避免的。 You might need to factor in a half-pixel nudge. 您可能需要考虑微调半像素。

You might also want to look at the Wu antialias trick, which uses the float remainder to shade the 2 overlapping pixels, though how you then apply that to text mode is your problem, sorry. 您可能还想看一下Wu抗锯齿技巧,该技巧使用浮点余数来阴影2个重叠的像素,但是很抱歉,如何将其应用于文本模式。

With the same algorithm, you can improve the accuracy by first calculating y in float and then round it to the nearest integer. 使用相同的算法,可以通过首先在float中计算y ,然后将其舍入到最接近的整数来提高精度。 This can be combined with the previous answer. 可以与先前的答案结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM