Search code examples
c++openglglut

how scan line fill work


in this code:

Scan-line fill OpenGL/GLUT algorithm in C++

just this part: how it work can anyone explain to me what is that for ?

    void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)
{
    float temp,x,mx;
    int i;

    if(y1>y2)
    {
        temp=x1,x1=x2,x2=temp;
        temp=y1,y1=y2,y2=temp;
    }

    if(y1==y2)
        mx=x2-x1;
    else
        mx=(x2-x1)/(y2-y1);

    x=x1;

    for(i=int(y1);i<=(int)y2;i++)
    {
        if(x<(float)le[i]) le[i]=(int)x;
        if(x>(float)re[i]) re[i]=(int)x;
        x+=mx;
    }
}

Solution

  • It is computing the left and right edges (x coordinates) of the scan-lines. Y coordinate of a scan-line is implicitly defined as the array index of le&re. So for instance, the i'th scan-line is defined as the line from point (le[i], i) to (re[i], i).

    You can imagine it as if you are horizontally scanning the scene from the bottom working your way up. And keeping x coordinates of where the scan-line hits the object first time (entering through the object on the left side) and where the scan-line hits the object second time (leaving the object from the right side).