In this snippet 10 random generated polygons of 10 vertices each are filled without using "paint" function.
C original source: http://code-heaven.blogspot.it/2009/10/ ... lygon.html
Code: Select all
'Polygons filled using scan line algorithm
#include "fbgfx.bi"
Using FB
'DEFINE_________________________________________________________________
#define POINTS 10
#define POLYGONS 10
#define SCR_W 640
#define SCR_H 480
'SUBS declarations______________________________________________________
declare sub fill_polygon(vertices() as integer, c as integer)
sub fill_polygon(a() as integer, c as integer)
'translation of a c snippet by Angad
'source of c code: http://code-heaven.blogspot.it/2009/10/simple-c-program-for-scan-line-polygon.html
dim as integer i,j,k,dy,dx, x,y,temp
dim as integer xi (0 to Ubound(a))
dim as single slope(0 to Ubound(a))
'join first and last vertex
a(Ubound(a),0)=a(0,0)
a(Ubound(a),1)=a(0,1)
for i=0 to Ubound(a) - 1
dy=a(i+1,1)-a(i,1)
dx=a(i+1,0)-a(i,0)
if(dy=0) then slope(i)=1.0
if(dx=0) then slope(i)=0.0
if ((dy <> 0) and (dx <> 0)) then
'calculate inverse slope
slope(i) = dx / dy
end if
next i
for y=0 to SCR_H-1
k = 0
for i=0 to Ubound(a) - 1
if( ((a(i,1)<=y) and (a(i+1,1)>y)) or ((a(i,1)>y) _
and (a(i+1,1)<=y))) then
xi(k)= int(a(i,0)+slope(i)*(y-a(i,1)))
k +=1
end if
next i
for j = 0 to k-2
'Arrange x-intersections in order
for i = 0 to k-2
if (xi(i) > xi(i+1)) then
temp = xi(i)
xi(i) = xi(i+1)
xi(i+1) = temp
end if
next i
next j
'line filling
for i = 0 to k - 2 step 2
line (xi(i),y)-(xi(i+1)+1,y), c
next i
next y
end sub
randomize timer
'variables declarations_________________________________________________
dim as integer a(0 to POINTS, 0 to 1), x, c, p
dim as double timer_01, timer_02
'INITIALIZING GRAPHICS _________________________________________________
screenres SCR_W, SCR_H, 24 'initialize graphics
cls
c = &h0000FF
timer_01 = timer
for p = 0 to POLYGONS-1
for x = 0 to POINTS
a(x, 0) = int (rnd*SCR_W)
a(x, 1) = int (rnd*SCR_H)
next x
fill_polygon(a(), rnd*&hFFFFFF)
next p
timer_02 = timer
draw string (SCR_W\2 +1, SCR_H - 20 + 1), "scanline fill in " + str(int((timer_02 - timer_01)*10000)/10000) + " sec", &h000000
draw string (SCR_W\2, SCR_H - 20), "scanline fill in " + str(int((timer_02 - timer_01)*10000)/10000) + " sec"
draw string (21, 21), "scanline polygon - translated from:", &h000000
draw string (21, 31), "code-heaven.blogspot.it/2009/10/simple-c-program-for-scan-line-polygon.html", &h000000
draw string (20, 20), "scanline polygon - translated from:"
draw string (20, 30), "code-heaven.blogspot.it/2009/10/simple-c-program-for-scan-line-polygon.html"
sleep