REM ***********************************************
REM Title: Bezier
REM Author: 
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
 
sync on
 
 
sw = screen width()
sh = screen height() - 80
speed# = 0.001
 
randomize timer()
 
x1 = rnd(sw)
y1 = rnd(sh) + 16
x2 = rnd(sw)
y2 = rnd(sh) + 16
c1x = rnd(sw)
c1y = rnd(sh) + 16
c2x = rnd(sw)
c2y = rnd(sh) + 16
 
mousemode = 0
control = 0
 
 
repeat
 
   cls
 
   if inkey$()="a"
      mousemode = 1
      control = 1
   endif
 
   if inkey$()="b"
      mousemode = 1
      control = 2
   endif
 
   if inkey$()="1"
      mousemode = 1
      control = 3
   endif
 
   if inkey$()="2"
      mousemode = 1
      control = 4
   endif
 
   if mouseclick()=1
      mousemode = 0
   endif
 
   if mousemode = 1
      select control
         case 1
            x1 = mousex()
            y1 = mousey()
         endcase
         case 2
            x2 = mousex()
            y2 = mousey()
         endcase
         case 3
            c1x = mousex()
            c1y = mousey()
         endcase
         case 4
            c2x = mousex()
            c2y = mousey()
         endcase
      endselect
   endif
 
   text 0,0,"A: " + str$(x1) + "x " + str$(y1) + "y"
   text 0,20,"B: " + str$(x2) + "x " + str$(y2) + "y"
   text 0,40,"C1: " + str$(c1x) + "x " + str$(c1y) + "y"
   text 0,60,"C2: " + str$(c2x) + "x " + str$(c2y) + "y"
   text 0,80, str$(t#)
   center text screen width()/2,screen height()-20,"Bezier Curve - Press A/B/1/2 to move the points. Left Mouse to set"
 
 
   if thing = 1
      ttt# = ttt# + speed#
      if ttt# >= 1 then thing=0
   endif
 
   if thing = 0
      ttt# = ttt# - speed#
      if ttt# <= 0 then ttt# = 0: thing=1
   endif
 
 
   Bezier(x1, y1, x2, y2, c1x, c1y, c2x, c2y, ttt#)
   sync
 
until escapekey()
 
end
 
function Bezier(x1, y1, x2, y2, c1x, c1y, c2x, c2y, t#)
 
 
 
   x# = x1
   y# = y1
 
   cx# = 3 * (c1x - x1)
   bx# = 3 * (c2x - c1x) - cx#
   ax# = x2 - x1 - cx# - bx#
 
   cy# = 3 * (c1y - y1)
   by# = 3 * (c2y - c1y) - cy#
   ay# = y2 - y1 - cy# - by#
 
   text x1,y1,"a"
   text x2,y2,"b"
   text c1x,c1y,"c1"
   text c2x,c2y,"c2"
 
 
   x# = ((ax# * t# + bx#) * t# + cx#) * t# + x1
   y# = ((ay# * t# + by#) * t# + cy#) * t# + y1
 
   ink rgb(255,0,0),0
   circle x#, y#, 10
 
 
   ink rgb(255,255,255), 0
   for t# = 0 to 1.05 step .05
 
      x# = ((ax# * t# + bx#) * t# + cx#) * t# + x1
      y# = ((ay# * t# + by#) * t# + cy#) * t# + y1
 
      `  First time through?
      if t# = 0
         oldx# = x#
         oldy# = y#
      endif
 
      line oldx#, oldy#, x#, y#
 
      oldx# = x#
      oldy# = y#
 
   next t#
 
endfunction