TGC Codebase Backup



Interactive Bezier Curve by Richard Davey

20th Oct 2003 13:21
Summary

This program creates a real-time bezier curve of which you can control all 4 points (start, end, control 1 and control 2) by pressing the keys shown and moving the mouse. Bezier cu



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: Bezier Curve
Rem Created: 08/10/2003 02:15:43

Rem ***** Main Source File *****

sync rate 0
sync on

sw = screen width()
sh = screen height() - 80

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

set text font "Arial"
set text size 16

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"
   center text screen width()/2,screen height()-20,"Bezier Curve - Press A/B/1/2 to move the points. Left Mouse to set"

   Bezier(x1, y1, x2, y2, c1x, c1y, c2x, c2y)
   sync

until escapekey()

end

function Bezier(x1, y1, x2, y2, c1x, c1y, c2x, c2y)

   x as float
   y as float

   cx as float
   bx as float
   ax as float

   cy as float
   by as float
   ay as float

   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"

   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