Fast fill_poly function

Post Reply
Henko
Posts: 816
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Fast fill_poly function

Post by Henko »

' 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

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Fast fill_poly function

Post by Operator »

Amazing this self coded graphics primitives...
Imagine a matrix manipulation library which
could also be imported in smart basic
( include# matrix_by_Henko.txt ) :D

Henko
Posts: 816
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Fast fill_poly function

Post by Henko »

Operator wrote:Amazing this self coded graphics primitives...
Imagine a matrix manipulation library which
could also be imported in smart basic
( include# matrix_by_Henko.txt ) :D
Well, no need for imagination. I'll upload my "matlib" library, which deals with vectors and matrices. There are no explaining comments, but i think you will quite understand what they do. The only obscure function could be "ls", which is a least square n-degree polynomial fit of a 2D x-y cloud. Enjoy!

Post Reply