Posted: 26th Feb 2003 20:07
On the weekend I've spend some time trying to understand the bezier curves based on a tutorial at darkbasic.de, and I was successful. So I just wrote this little programm which is an extended version of the curves, not only based on four points, but on any number of reference points.

+ Code Snippet
` BEZIER CURVES
` created by JamesBlond
` 23/02/2003



`SETUP

sync on : sync rate 60 : set display mode 800,600,32
randomize timer()

refnum  = 3     : `number of (initial) reference points
density = 100   : `number of dots on the curve

dim xcoord(9)
dim ycoord(9)

mclick=1
keypressed=0
new=1
gosub _newref



`MAIN LOOP

while escapekey()=0

 gosub _setref
 gosub _moveref
 sync

endwhile



`Creates random reference points
_newref:
 for r=0 to refnum
  xcoord(r)=rnd(780)+10
  ycoord(r)=rnd(550)+40
 next r
return



`Calculates and draws the bezier curve
_drawbezier:

 ink rgb(0,255,0),0
 cls 0
 print "Press Esc to exit."
 print "Press 1 to 9 to change the number of reference points and draw a new curve."
 print "Use the mouse to drag and drop the reference points to new positions."

 for d=0 to density

  d#=d : density#=density
  p#=d#/density#
  x#=0 : y#=0

  for ref=0 to refnum
   const#=nCr(refnum,ref)*p#^(ref)*(1-p#)^(refnum-ref)
   x#=x#+xcoord(ref)*const#
   y#=y#+ycoord(ref)*const#
  next ref

  dot x#,y#
  if new=1 then sync

 next d

 new=0

return



`set new number of reference points
_setref:

 if scancode()=0 then keypressed=0

 if val(inkey$())>0 and val(inkey$())=xcoord(ref)-3 and mousex()=ycoord(ref)-3 and mousey()0
  for i=1 to n
   a=a*i
  next i
 endif

 if (n-r)>0
  for i=1 to (n-r)
   b=b*i
  next i
 endif

 if r>0
  for i=1 to r
   c=c*i
  next i
 endif

 value=(a)/(b*c)

endfunction value
Posted: 26th Feb 2003 20:14
Something went wrong there, and that edit button doesn't work...

+ Code Snippet
` BEZIER CURVES
` created by JamesBlond
` 23/02/2003



`SETUP

sync on : sync rate 60 : set display mode 800,600,32
randomize timer()

refnum  = 3     : `number of (initial) reference points
density = 100   : `number of dots on the curve

dim xcoord(9)
dim ycoord(9)

mclick=1
keypressed=0
new=1
gosub _newref



`MAIN LOOP

while escapekey()=0

 gosub _setref
 gosub _moveref
 sync

endwhile



`Creates random reference points
_newref:
 for r=0 to refnum
  xcoord(r)=rnd(780)+10
  ycoord(r)=rnd(550)+40
 next r
return



`Calculates and draws the bezier curve
_drawbezier:

 ink rgb(0,255,0),0
 cls 0
 print "Press Esc to exit."
 print "Press 1 to 9 to change the number of reference points and draw a new curve."
 print "Use the mouse to drag and drop the reference points to new positions."

 for d=0 to density

  d#=d : density#=density
  p#=d#/density#
  x#=0 : y#=0

  for ref=0 to refnum
   const#=nCr(refnum,ref)*p#^(ref)*(1-p#)^(refnum-ref)
   x#=x#+xcoord(ref)*const#
   y#=y#+ycoord(ref)*const#
  next ref

  dot x#,y#
  if new=1 then sync

 next d

 new=0

return



`set new number of reference points
_setref:

 if scancode()=0 then keypressed=0

 if val(inkey$())>0 and val(inkey$())<10 and keypressed=0
  keypressed=1
  refnum=val(inkey$())
  new=1
  gosub _newref
  gosub _drawbezier
 endif

return



`drag and drop points
_moveref:

 for ref=0 to refnum

  if mousex()>=xcoord(ref)-3 and mousex()<=xcoord(ref)+3 and mousey()>=ycoord(ref)-3 and mousey()<=ycoord(ref)+3
   if mouseclick()=1 and selected=-1
    mclick=1
    selected=ref
   endif
   ink rgb(255,0,0),0
  else
   ink rgb(255,0,0),0
   if mclick=1 then dot mousex(),mousey()
   ink rgb(0,255,0),0
  endif

  circle xcoord(ref),ycoord(ref),3
  text xcoord(ref)+5,ycoord(ref)-5,str$(ref)

 next ref

 if mouseclick()=0 and mclick=1
  xcoord(selected)=mousex()
  ycoord(selected)=mousey()
  gosub _drawbezier
  mclick=0
  selected=-1
 endif

return



function nCr(n,r)

 a=1 : b=1 : c=1

 if n>0
  for i=1 to n
   a=a*i
  next i
 endif

 if (n-r)>0
  for i=1 to (n-r)
   b=b*i
  next i
 endif

 if r>0
  for i=1 to r
   c=c*i
  next i
 endif

 value=(a)/(b*c)

endfunction value
Posted: 26th Feb 2003 20:47
Very useful tool!
Good code .
Posted: 26th Feb 2003 22:35
db friendly?
Posted: 27th Feb 2003 14:02
Don't know, but I don't see why it should not be.
Posted: 27th Feb 2003 20:32
yep it works in dbc good work
Posted: 30th Mar 2003 3:39
+ Code Snippet
` BEZIER CURVES
` created by JamesBlond
` 23/02/2003



`SETUP

sync on : sync rate 60 : set display mode 800,600,32
randomize timer()

refnum  = 3     : `number of (initial) reference points
density = 100   : `number of dots on the curve

dim xcoord(9)
dim ycoord(9)

mclick=1
keypressed=0
new=1
gosub _newref



`MAIN LOOP

while escapekey()=0

 gosub _setref
 gosub _moveref
 sync

endwhile



`Creates random reference points
_newref:
 for r=0 to refnum
  xcoord(r)=rnd(780)+10
  ycoord(r)=rnd(550)+40
 next r
return



`Calculates and draws the bezier curve
_drawbezier:

 ink rgb(0,255,0),0
 cls 0
 print "Press Esc to exit."
 print "Press 1 to 9 to change the number of reference points and draw a new curve."
 print "Use the mouse to drag and drop the reference points to new positions."

 for d=0 to density

  d#=d : density#=density
  p#=d#/density#
  x#=0 : y#=0

  for ref=0 to refnum
   const#=nCr(refnum,ref)*p#^(ref)*(1-p#)^(refnum-ref)
   x#=x#+xcoord(ref)*const#
   y#=y#+ycoord(ref)*const#
  next ref

  dot x#,y#
  if new=1 then sync

 next d

 new=0

return



`set new number of reference points
_setref:

 if scancode()=0 then keypressed=0

 if val(inkey$())>0 and val(inkey$())<10 and keypressed=0
  keypressed=1
  refnum=val(inkey$())
  new=1
  gosub _newref
  gosub _drawbezier
 endif

return



`drag and drop points
_moveref:

 for ref=0 to refnum

  if mousex()>=xcoord(ref)-3 and mousex()<=xcoord(ref)+3 and mousey()>=ycoord(ref)-3 and mousey()<=ycoord(ref)+3
   if mouseclick()=1 and selected=-1
    mclick=1
    selected=ref
   endif
   ink rgb(255,0,0),0
  else
   ink rgb(255,0,0),0
   if mclick=1 then dot mousex(),mousey()
   ink rgb(0,255,0),0
  endif

  circle xcoord(ref),ycoord(ref),3
  text xcoord(ref)+5,ycoord(ref)-5,str$(ref)

 next ref

   for ref=1 to refnum

      x1 = xcoord(ref)
      y1 = ycoord(ref)
      x2 = xcoord(ref - 1)
      y2 = ycoord(ref - 1)
      line x1,y1,x2,y2

   next ref

 if mouseclick()=0 and mclick=1
  xcoord(selected)=mousex()
  ycoord(selected)=mousey()
  gosub _drawbezier
  mclick=0
  selected=-1
 endif

return



function nCr(n,r)

 a=1 : b=1 : c=1

 if n>0
  for i=1 to n
   a=a*i
  next i
 endif

 if (n-r)>0
  for i=1 to (n-r)
   b=b*i
  next i
 endif

 if r>0
  for i=1 to r
   c=c*i
  next i
 endif

 value=(a)/(b*c)

endfunction value


Now with lines connecting data points