Fast fill_poly function
Posted: Mon May 13, 2013 12:12 pm
' I forgot to use the graphics lock and unlock mechanism.
' Moreover the filling lines are now drawn from the centre of gravity point of the figure, wich makes the line shorter, and
' less lines are needed (about 50%).
'
option base 1
option angle degrees
dim x(10),y(10)
for i=1 to 5
read x(i),y(i)
next i
data 220,600, 240,260, 500,220, 600,500, 350,700
graphics
graphics clear .8,.8,.8
fill_poly(5,x,y,0.2,0.8,0.5,1)
draw_poly(5,x,y,1,0,0,3)
end
def draw_poly(n,x(),y(),r,g,b,thick)
draw color r,g,b ! draw size thick
n=n+1 ! x(n)=x(1) ! y(n)=y(1)
for i=2 to n ! draw line x(i-1),y(i-1) to x(i),y(i) ! next i
end def
def fill_poly(n,x(),y(),r,g,b,transp)
draw color r,g,b ! draw alpha transp
xs=0 ! ys=0
for i=1 to n ! xs= xs+x(i) ! ys=ys+y(i) ! next i
xs=xs/n ! ys=ys/n
n=n+1 ! x(n)=x(1) ! y(n)=y(1)
graphics lock
for i=2 to n
xo=x(i-1) ! yo=y(i-1)
xc=x(i)-xo ! yc=y(i)-yo
delta=.8/sqrt(xc*xc+yc*yc)
for lab=0 to 1 step delta
xe=xo+lab*xc ! ye=yo+lab*yc
draw line xs,ys to xe,ye
next lab
next i
graphics unlock
end def
' Moreover the filling lines are now drawn from the centre of gravity point of the figure, wich makes the line shorter, and
' less lines are needed (about 50%).
'
option base 1
option angle degrees
dim x(10),y(10)
for i=1 to 5
read x(i),y(i)
next i
data 220,600, 240,260, 500,220, 600,500, 350,700
graphics
graphics clear .8,.8,.8
fill_poly(5,x,y,0.2,0.8,0.5,1)
draw_poly(5,x,y,1,0,0,3)
end
def draw_poly(n,x(),y(),r,g,b,thick)
draw color r,g,b ! draw size thick
n=n+1 ! x(n)=x(1) ! y(n)=y(1)
for i=2 to n ! draw line x(i-1),y(i-1) to x(i),y(i) ! next i
end def
def fill_poly(n,x(),y(),r,g,b,transp)
draw color r,g,b ! draw alpha transp
xs=0 ! ys=0
for i=1 to n ! xs= xs+x(i) ! ys=ys+y(i) ! next i
xs=xs/n ! ys=ys/n
n=n+1 ! x(n)=x(1) ! y(n)=y(1)
graphics lock
for i=2 to n
xo=x(i-1) ! yo=y(i-1)
xc=x(i)-xo ! yc=y(i)-yo
delta=.8/sqrt(xc*xc+yc*yc)
for lab=0 to 1 step delta
xe=xo+lab*xc ! ye=yo+lab*yc
draw line xs,ys to xe,ye
next lab
next i
graphics unlock
end def