TGC Codebase Backup



Bezier Curves by Billj

28th Mar 2004 20:45
Summary

I went online and decided to look into bezier curves and before long i made a demo. Controls: Green boxes are end points and red boxes are control points



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    set display mode 800,600,32

sync on
sync rate 60

global bez_end_x1=100
global bez_end_y1=100
global bez_end_x2=200
global bez_end_y2=100
global bez_control_x1=80
global bez_control_y1=100
global bez_control_x2=220
global bez_control_y2=200

global p_type$="null"
global p_num=0

global difx=0
global dify=0

do
cls
gosub move_bezier
draw_bezier(bez_end_x1,bez_end_y1,bez_end_x2,bez_end_y2,bez_control_x1,bez_control_y1,bez_control_x2,bez_control_y2)
text 0,0,str$(screen fps())
text 0,10,p_type$
text 0,20,str$(p_num)
sync
loop


move_bezier:

if mouseclick()=1 and p_type$="null" and p_num=0
   if mousex()>=bez_end_x1-3 and mousex()<=bez_end_x1+3 and mousey()>=bez_end_y1-3 and mousey()<=bez_end_y1+3
      p_type$="end"
      p_num=1
   endif
   if mousex()>=bez_end_x2-3 and mousex()<=bez_end_x2+3 and mousey()>=bez_end_y2-3 and mousey()<=bez_end_y2+3
      p_type$="end"
      p_num=2
   endif
   if mousex()>=bez_control_x1-3 and mousex()<=bez_control_x1+3 and mousey()>=bez_control_y1-3 and mousey()<=bez_control_y1+3
      p_type$="control"
      p_num=1
   endif
   if mousex()>=bez_control_x2-3 and mousex()<=bez_control_x2+3 and mousey()>=bez_control_y2-3 and mousey()<=bez_control_y2+3
      p_type$="control"
      p_num=2
   endif
endif

if mouseclick()=1 and p_type$<>"null" and p_num<>0
   if p_type$="end" and p_num=1
      difx=bez_control_x1-bez_end_x1
      dify=bez_control_y1-bez_end_y1
      bez_end_x1=mousex()
      bez_end_y1=mousey()
      bez_control_x1=bez_end_x1+difx
      bez_control_y1=bez_end_y1+dify
   endif
   if p_type$="end" and p_num=2
      difx=bez_control_x2-bez_end_x2
      dify=bez_control_y2-bez_end_y2
      bez_end_x2=mousex()
      bez_end_y2=mousey()
      bez_control_x2=bez_end_x2+difx
      bez_control_y2=bez_end_y2+dify
   endif
   if p_type$="control" and p_num=1
      bez_control_x1=mousex()
      bez_control_y1=mousey()
   endif
   if p_type$="control" and p_num=2
      bez_control_x2=mousex()
      bez_control_y2=mousey()
   endif
endif

if mouseclick()=0
   p_type$="null"
   p_num=0
endif

return


function draw_bezier(ex1,ey1,ex2,ey2,cx1,cy1,cx2,cy2)
bx1#=ex1+cx1
by1#=ey1+cy1
cx#=ex2+cx2
cy#=ey2+cy2
ink RGB(192,192,192),0

for t#=0 to 1 step .002
   a#=t#
   b2#=1-t#
   px=ex1*(b2#^3)+3*bx1#*(b2#^2)*a#+(3*cx#)*b2#*(a#^2)+ex2*(a#^3)
   py=ey1*(b2#^3)+3*by1#*(b2#^2)*a#+(3*cy#)*b2#*(a#^2)+ey2*(a#^3)

   dot px,py
next t#

rem draw control points
ink RGB(0,255,128),0
box ex1-3,ey1-3,ex1+3,ey1+3
box ex2-3,ey2-3,ex2+3,ey2+3

ink RGB(255,0,0),0
line ex1,ey1,cx1,cy1
line ex2,ey2,cx2,cy2

box cx1-3,cy1-3,cx1+3,cy1+3
box cx2-3,cy2-3,cx2+3,cy2+3

endfunction