TGC Codebase Backup



Bezier Curve Example by Markus

6th Apr 2013 5:01
Summary

plot a Bezier Curve from a array of vectors.



Description

plot a Bezier Curve from a array of vectors.
the position of the curve is a value parameter fromo 0 to 1.
the Bezier function "return" the result vector.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem
rem AGK Application 1.08B9

rem Bezier Curve Example
rem MR 06.04.2013

SetDisplayAspect(4/3)
SetVirtualResolution(800,600)

type TVector3
    x as float
    y as float
    z as float
endtype
global VectorMax=6
global dim path[VectorMax] as TVector3 //vector list
global bezret as TVector3 //return from Bezier until AGK can handle types correct

global TestSprite as Integer
TestSprite=createSprite(0)
setSpriteScale(TestSprite,1,1)

path[0].x=100
path[0].y=300

path[1].x=200
path[1].y=300+50

path[2].x=300
path[2].y=300-100

path[3].x=400
path[3].y=300+50

path[4].x=500
path[4].y=300+200

path[5].x=600
path[5].y=300-200

path[6].x=700
path[6].y=300-50

do
 Print("Hello Bezier Curve")

 setspritecolor(TestSprite,255,255,255,255)
 for i = 0 to VectorMax
  SetSpritePositionByOffset(TestSprite,path[i].x,path[i].y)
  drawsprite(TestSprite)
 next

 setspritecolor(TestSprite,0,255,0,128)
 for mu#=0.0 to 1.0 step 0.01
  Bezier(mu#)
  SetSpritePositionByOffset(TestSprite,bezret.x,bezret.y)
  drawsprite(TestSprite)
 next

 Sync()
loop
end

Function Bezier(mu as float)

 //MR 06.04.2013

 //General Bezier curve
 //mu 0.0 to 1.0

 k as integer ,kn as integer,nn as integer,nkn as integer
 blend as float,muk as float,munk as float

 px as float,py as float,pz as float

 n=VectorMax //< global

   bezret.x=0.0
   bezret.y=0.0
   bezret.z=0.0

   muk = 1.0
   munk = pow(1.0-mu,IntegerToFloat(n))

   For k=0 To n
      nn = n
      kn = k
      nkn = n - k
      blend = muk * munk
      muk =muk * mu
      munk = munk / (1.0-mu)
      While nn => 1
         blend=blend * nn
         nn=nn-1
         If kn > 1
            blend=blend / IntegerToFloat(kn)
            kn=kn-1
         EndIf
         If nkn > 1
            blend=blend / IntegerToFloat(nkn)
           nkn=nkn-1
         EndIf
      endwhile
      bezret.x=bezret.x+path[k].x * blend
      bezret.y=bezret.y+path[k].y * blend
      bezret.z=bezret.z+path[k].z * blend
   Next

EndFunction

function IntegerToFloat(i as integer)

    f#=i
    exitfunction f#

endfunction f#

Function pow(a as float,b as float)

 //C Like :-)

 exitfunction a^b

EndFunction c#