UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: empty on September 15, 2005, 02:32:34 PM

Title: Bezier curves
Post by: empty on September 15, 2005, 02:32:34 PM
This example shows how to do Bezier curves in PlayBasic.

[pbcode]
Constant Smoothness# = 0.05 // the smaller the value the smoother the curve

Type TPoint
  x,y
EndType

Dim Points(4) As TPoint

Do
  Cls 0
  Print "Place 4 points with mouseclicks"
  Print "1st = Start point, 2nd = Control Point 1, 3rd = Control Point 2, 4th = End point"
  Ink $00FF00
  count = 0
  Repeat
     Text 1,10," "
     If MouseButton() = 1
        Inc count
        BoxC MouseX()-2, MouseY()-2, MouseX()+2, MouseY()+2, 1, 255
        Points(Count).X = MouseX()
        Points(Count).Y = MouseY()
        While MouseButton() = 1
           Text 1, 1," "
           Sync
        EndWhile
     EndIf
     Sync
  Until count=4
 
  old_x = Points(1).x
  old_y = Points(1).y
  For t# = 0.0 To 1.0 Step Smoothness#
     f1# = (1-t#)*(1-t#)*(1-t#)
     f2# = 3*t#*(1-t#)*(1-t#)
     f3# = 3*t#*t#*(1-t#)
     f4# = t#*t#*t#
     new_x = f1#*Points(1).x + f2#*Points(2).x + f3#*Points(3).x + f4#*Points(4).x
     new_y = f1#*Points(1).y + f2#*Points(2).y + f3#*Points(3).y + f4#*Points(4).y
     Line old_x, old_y, new_x, new_y
     old_x = new_x
     old_y = new_y
  Next t#
  Line old_x, old_y, Points(4).x, Points(4).y   
  Sync
  Ink $FFFFFF
  Print "Press a key"
  Sync
  WaitKey
Loop
[/pbcode]

 This version is for older version of PlayBasic V1.63 such as learning edition (and older retail editions)

[pbcode]
constant Smoothness = 0.05*100 // the smaller the value the smoother the curve

Type TPoint
  x,y
EndType

Dim Points(4) As TPoint

Do
  Cls 0
  Print "Place 4 points with mouseclicks"
  Print "1st = Start point, 2nd = Control Point 1, 3rd = Control Point 2, 4th = End point"
  Ink $00FF00
  count = 0
  Repeat
     Text 1,10," "
     If MouseButton() = 1
        Inc count
        BoxC MouseX()-2, MouseY()-2, MouseX()+2, MouseY()+2, 1, 255
        Points(Count).X = MouseX()
        Points(Count).Y = MouseY()
        While MouseButton() = 1
           Text 1, 1," "
           Sync
        EndWhile
     EndIf
     Sync
  Until count=4
 
  old_x = Points(1).x
  old_y = Points(1).y
  For tlp = 0 To 100 Step Smoothness
     t#=tlp/100.0
 
     
     f1# = (1-t#)*(1-t#)*(1-t#)
     f2# = 3*t#*(1-t#)*(1-t#)
     f3# = 3*t#*t#*(1-t#)
     f4# = t#*t#*t#
     new_x = f1#*Points(1).x + f2#*Points(2).x + f3#*Points(3).x + f4#*Points(4).x
     new_y = f1#*Points(1).y + f2#*Points(2).y + f3#*Points(3).y + f4#*Points(4).y
     Line old_x, old_y, new_x, new_y
     old_x = new_x
     old_y = new_y
  Next
  Line old_x, old_y, Points(4).x, Points(4).y   
  Sync
  Ink $FFFFFF
  Print "Press a key"
  Sync
  WaitKey
Loop
[/pbcode]
Title: Re: Bezier curves
Post by: monkeybot on October 29, 2009, 09:52:04 AM
all i get is a straight line
???
Title: Re: Bezier curves
Post by: kevin on October 29, 2009, 10:19:27 AM

Looks like a curve to me
Title: Re: Bezier curves
Post by: monkeybot on October 30, 2009, 12:43:11 PM
it works with compiler 1.64j and IDE 1.1.7
but not with compiler 1.64m and IDE 1.15f
i can only run the earlier version as the IDE crashes too much.
Title: Re: Bezier curves
Post by: kevin on October 30, 2009, 01:21:54 PM
 The example uses a float step in For a loop,  this can cause  issues with some editions of  PB ( from about 1.63 through -> 1.64i) due to some runtime opt's.   So above, i've posted a version that will run in old clunkers. 
Title: Re: Bezier curves
Post by: monkeybot on October 31, 2009, 07:49:00 AM
i would like to be upto date but can't,i am going to re-install xp soon will see if that helps with the crashing.