Scan line algorithm
INPUT: polygon vertex as P1(X1,Y1),P2(X2,Y2),P3(X3,Y3)……
OUTPUT: endpoints of line segment to draw scan line( example: x1,x2,x3,x4,……..)
ASSUMTIONS: polygon have n vertex
Variables: Poly[][],x_values[],I,U, Z,y_scan,ymax, y_min,active_edge[][],Pnext, Ppre.
1. Get the polygon vertex as input from user as Poly[(X1,Y1),P2(X2,Y2),P3(X3,Y3)]…..
2. Find the maximum(y_max) and minimum(y_min) value of y from input vertex.
3. For each y_scan=ymax, y_scan>y_min,y_scan
{ I=0;
For each polygon
{ active edge (P1,P2) , Pnext=P2, Ppre=P5
if( y_scan ==P1.y)
{ if( ( Pnext.y>y_scan>Ppre) OR(Pnext.y<y_scan<Ppre))
{ x_values[i]=P1.x}
Else
{ x_values[i]=P1.x;
x_values[i++]=P1.x;
}
}
Else
{
U=((y_scan-P1.y)/(P2.y-P1.y));
If(0<U<1)
{ x_values[i]=P1.x+(P2.x-P1,x)*U}
}
z=0;
While(z=<i)
{ line(x_values[z],Yscan, x_values[z+1], Yscan)
z=z+2;
}
}
4 END