Date: 02-16-2022
Return to Index
created by gbSnippets
'Simpler code ...
Sub DrawBezier(ByVal dt As Single, pt0 As Point, pt1 As Point, pt2 As Point, pt3 As Point)
Local t,x0,y0,x1,y1 As Single
t = 0
x1 = pt0.x * (1 - t) ^ 3 + pt1.x * 3 * t * (1 - t) ^ 2 + pt2.x * 3 * t ^ 2 * (1 - t) + pt3.x * t ^ 3
y1 = pt0.y * (1 - t) ^ 3 + pt1.y * 3 * t * (1 - t) ^ 2 + pt2.y * 3 * t ^ 2 * (1 - t) + pt3.y * t ^ 3
t = t + dt
Do While t < 1
x0 = x1 : y0 = y1
x1 = pt0.x * (1 - t) ^ 3 + pt1.x * 3 * t * (1 - t) ^ 2 + pt2.x * 3 * t ^ 2 * (1 - t) + pt3.x * t ^ 3
y1 = pt0.y * (1 - t) ^ 3 + pt1.y * 3 * t * (1 - t) ^ 2 + pt2.y * 3 * t ^ 2 * (1 - t) + pt3.y * t ^ 3
Graphic Line (x0, y0)-(x1, y1), %Blue
t = t + dt
Loop
' Connect to the final point.
t = 1
x0 = x1 : y0 = y1 : x1 = pt3.x : y1 = pt3.y
Graphic Line (x0, y0)-(x1, y1), %Blue
End Sub
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
%IDC_Graphic = 500
Global hDlg As Dword, P() As Point, pCount As Long
Function PBMain() As Long
Dialog New Pixels, 0, "Bezier Curve",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Graphic, hDlg, %IDC_Graphic, "", 0, 0, 200,200, %SS_Notify
Graphic Attach hDlg, %IDC_Graphic
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local pt As Point
Select Case Cb.Msg
Case %WM_InitDialog
ReDim P(4)
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Graphic
GetCursorPos pt
ScreenToClient hDlg, pt
Incr pCount
P(pCount) = pt
If pCount = 4 Then DrawBezier(0.01, P(1), P(2), P(3), P(4))
If pCount = 5 Then pCount = 1 : P(pCount) = pt : Graphic Clear
Graphic Box (P(pCount).x-5,P(pCount).y-5)-(P(pCount).x+5,P(pCount).y+5),,%Red
Graphic Set Pos (P(pCount).x,P(pCount).y-20)
Graphic Print Choose$(pCount,"p0","p1","p2","p3")
End Select
End Select
End Function
Function BX(ByVal t As Single, ByVal x0 As Single, ByVal x1 As Single, ByVal x2 As Single, ByVal x3 As Single) As Single
Function = x0 * (1 - t) ^ 3 + _
x1 * 3 * t * (1 - t) ^ 2 + _
x2 * 3 * t ^ 2 * (1 - t) + _
x3 * t ^ 3
End Function
Function BY(ByVal t As Single, ByVal y0 As Single, ByVal y1 As Single, ByVal y2 As Single, ByVal y3 As Single) As Single
Function = y0 * (1 - t) ^ 3 + _
y1 * 3 * t * (1 - t) ^ 2 + _
y2 * 3 * t ^ 2 * (1 - t) + _
y3 * t ^ 3
End Function
Sub DrawBezier(ByVal dt As Single, pt0 As Point, pt1 As Point, pt2 As Point, pt3 As Point)
Local t,x0,y0,x1,y1 As Single
' Draw the control lines.
Graphic Style 2 'dot
Graphic Line (pt0.X, pt0.Y)-(pt1.X, pt1.Y), %Red
Graphic Line (pt2.X, pt2.Y)-(pt3.X, pt3.Y), %Red
Graphic Style 0 'solid
' Draw the curve.
t = 0
x1 = BX(t, pt0.X, pt1.X, pt2.X, pt3.X)
y1 = BY(t, pt0.Y, pt1.Y, pt2.Y, pt3.Y)
t = t + dt
Do While t < 1
x0 = x1 : y0 = y1
x1 = BX(t, pt0.X, pt1.X, pt2.X, pt3.X)
y1 = BY(t, pt0.Y, pt1.Y, pt2.Y, pt3.Y)
Graphic Line (x0, y0)-(x1, y1), %Blue
t = t + dt
Loop
' Connect to the final point.
t = 1
x0 = x1 : y0 = y1
x1 = BX(t, pt0.X, pt1.X, pt2.X, pt3.X)
y1 = BY(t, pt0.Y, pt1.Y, pt2.Y, pt3.Y)
Graphic Line (x0, y0)-(x1, y1), %Blue
End Sub
'gbs_01459
'Date: 10-17-2014
http://www.garybeene.com/sw/gbsnippets.htm