Scanline Algorithm

1k Views Asked by At

I am stuck in the midway(or at the start you can say) while implementing scanline algo. I follow parity test i.e.,

for each scanline

edgeCnt = 0; 

for each pixel on scanline (l to r)

 if (oldpixel->newpixel crosses edge)

  edgeCnt ++;

// draw the pixel if edgeCnt odd

if (edgeCnt % 2)

  setPixel(pixel);

Suppose my triangle has vertices A(10,10), B(100, 100) and C(200, 30). Now the issue with this algo is. Point A can't be counted, because if it is counted then on the same horizontal line there is no edge detected and thus the whole line after A will be colored. Now if I exclude vertices, a pixel before vertex C is colored but since the scan won't detect the vertex, C is not detected and it will keep coloring the lines till the edge on the next higher line is detected.

Is there any standard solution to this?

1

There are 1 best solutions below

0
On

If there's a change of direction at the vertex, don't count it. If the direction is the same, count it. And if the line is horizontal, ignore it and check against the next vertex.