` 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
Alex Wanuch
aka rapscaLLion
Kousen Dev Progress >> Currently Working On Editors