ssloy / tinyrenderer

A brief computer graphics / rendering course
https://github.com/ssloy/tinyrenderer/wiki
Other
20.61k stars 1.98k forks source link

Why not use the more universal Bresenham? #138

Open jiannanya opened 10 months ago

jiannanya commented 10 months ago

Hi there, I know the original one is correct, but why not use the more universal Bresenham like blow ?

code like:

void bresenhamLine(int x1, int y1, int x2, int y2)
{
    int x, y, dx, dy, p;
    x = x1;
    y = y1;
    dx = x2 - x1;
    dy = y2 - y1;

    p = 2*dy - dx;
    for(; x <= x2; x++){
        SetPixel(x,y);
        if(p>0){
            y++
            p+=2*(dy-dx);
         } else{
            p+=2*dy;
         }
    }
} 
eduardoheleno commented 5 months ago

I'm not sure, but i think that your example doesn't cover lines with slope > 1

digitsensitive commented 4 months ago

Hello @jiannanya Your approach does not cover, as mentioned by @eduardoheleno, all cases. It does not correctly handle lines with negative slopes, vertical lines, or lines with slopes greater than 1.

Regarding your topic title: I was also wondering why they did not use the original algorithm. I think that is because of educational reasons, I don't believe the one in the tutorial is faster than the original one. Or am I wrong?

And btw. there's a missing semicolon after y++ :-)