Here's a function for plotting bezier curves. cp0-cp3 are your control points and t is a value from 0 to 1. The idea is to decide how many steps will be needed (the more steps, the smoother the line) then call BezierPoint to get the x-y point at that step.
Here is an example program using it.
Just use the arrow keys and KLO, to move the control points around
Type TPoint Field X:Int Field Y:Int End Type Function BezierPoint:TPoint(t:Float, cp0:TPoint, cp1:TPoint, cp2:TPoint, cp3:TPoint) Local ax:Float, bx:Float, cx:Float Local ay:Float, by:Float, cy:Float Local TSquared:Float, TCubed:Float Local PointResult:TPoint = New TPoint cx = 3.0 * (cp1.x - cp0.x) bx = 3.0 * (cp2.x - cp1.x) - cx ax = cp3.x - cp0.x - cx - bx cy = 3.0 * (cp1.y - cp0.y) by = 3.0 * (cp2.y - cp1.y) - cy ay = cp3.y - cp0.y - cy - by tSquared = t * t; tCubed = tSquared * t; Pointresult.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp0.x; Pointresult.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp0.y; Return Pointresult End Function
Here is an example program using it.
Const NumPoints = 100 Const td:Float = 1.0/NumPoints Include "Bezier.bmx" Graphics 640,480,32 Local cp0:TPoint = New TPoint Local cp1:TPoint = New TPoint Local cp2:TPoint = New TPoint Local cp3:TPoint = New TPoint Local result:TPoint cp0.x = 0 cp0.y = 300 cp1.x = 100 cp1.y = 200 cp2.x = 500 cp2.y = 200 cp3.x = 600 cp3.y = 300 While Not KeyHit(KEY_ESCAPE) If KeyDown(KEY_LEFT) cp1.x :- 1 End If If KeyDown(KEY_RIGHT) cp1.x :+ 1 End If If KeyDown(KEY_K) cp2.x :- 1 End If If KeyDown(KEY_L) cp2.x :+ 1 End If If KeyDown(KEY_UP) cp1.y :- 1 End If If KeyDown(KEY_DOWN) cp1.y :+ 1 End If If KeyDown(KEY_O) cp2.y :- 1 End If If KeyDown(KEY_COMMA) cp2.y :+ 1 End If Cls SetColor 255,255,255 For i = 0 Until numpoints t:Float = i*td result = BezierPoint(t,cp0,cp1,cp2,cp3) Plot(result.x,result.y) Next SetColor 255,0,0 DrawRect(cp0.x-2,cp0.y-2,4,4) DrawRect(cp1.x-2,cp1.y-2,4,4) DrawRect(cp2.x-2,cp2.y-2,4,4) DrawRect(cp3.x-2,cp3.y-2,4,4) FlushMem(); Flip Wend
Just use the arrow keys and KLO, to move the control points around