News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Bezier curves

Started by empty, September 15, 2005, 02:32:34 PM

Previous topic - Next topic

empty

This example shows how to do Bezier curves in PlayBasic.

PlayBASIC Code: [Select]
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



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

PlayBASIC Code: [Select]
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



monkeybot

all i get is a straight line
???

kevin


Looks like a curve to me

monkeybot

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.

kevin

#4
 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. 

monkeybot

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.