right click deletes control points
editing (moving points) drag the control point
Code: Select all
Type point2d
x As double
y As double
End Type
Type bezier
as integer n=-1
pt(any) As point2d 'anchors and control points (p0 thru pN)
declare sub append_pt(x as double, y as double)
declare sub delete_pt(d as integer)
declare sub plot(pres as integer)
End Type
sub bezier.append_pt(x as double, y as double)
n+=1
redim preserve pt(n)
pt(n).x=x
pt(n).y=y
End Sub
sub bezier.delete_pt(d as integer)
for i as integer = d to n-1
pt(i)=pt(i+1)
Next
n-=1
redim preserve pt(n)
End Sub
sub bezier.plot(pres as integer)
dim as point2d c(n,n)
dim as double x_range,y_range
pset(pt(0).x,pt(0).y)
for t as integer = 1 to pres
for i as integer = 0 to n-1
x_range=pt(i+1).x - pt(i).x
y_range=pt(i+1).y - pt(i).y
c(i,0).x=pt(i).x+x_range*t/pres
c(i,0).y=pt(i).y+y_range*t/pres
Next
for k as integer = 0 to n
for i as integer = 0 to n-(k+2)
x_range=c(i+1,k).x - c(i,k).x
y_range=c(i+1,k).y - c(i,k).y
c(i,k+1).x=c(i,k).x+x_range*t/pres
c(i,k+1).y=c(i,k).y+y_range*t/pres
if n<(k+3) then line -(c(i,k+1).x,c(i,k+1).y)
Next
Next
next
End Sub
sub updatebz(pres as integer,byref bz as bezier)
screenlock
ScreenSet 0,1
cls
with bz
for i as integer=0 to bz.n
circle(.pt(i).x,.pt(i).y),6
Next
select case .n
case 1
line(.pt(0).x,.pt(0).y)-(.pt(1).x,.pt(1).y)
case is > 1
.plot(pres)
End Select
end with
screencopy 0,1
screenset 1,1
screenunlock
End Sub
Dim As bezier bz
Dim As Integer mouse_x, mouse_y, mouse_wheel, mouse_button, click_count
dim as integer selected_point
Dim As BOOLEAN mouse_button_released,snap,edit
ScreenRes 400,400,,2
ScreenSet 1,1
Do
Select Case InKey
Case Chr(27),Chr(255)+"k"
Exit Do
End Select
GetMouse(mouse_x, mouse_y, mouse_wheel, mouse_button)
if edit=false then
snap=false
screencopy 0,1
for i as integer=0 to bz.n
with bz
select case mouse_x
case .pt(i).x -3 to .pt(i).x+3
select case mouse_y
case .pt(i).y -3 to .pt(i).y+3
snap=true
selected_point=i
screenset 1,1
circle(.pt(i).x,.pt(i).y),6,3,,,,f
exit for
End Select
End Select
End With
Next
endif
Select Case mouse_button
case 0
mouse_button_released = TRUE
if edit=true then
edit=false
updatebz(100,bz)
EndIf
Case 1
If mouse_button_released = TRUE Then
mouse_button_released = FALSE
if snap=true then
edit=true
else
bz.append_pt(mouse_x,mouse_y)
updatebz(100,bz)
EndIf
endif
case 2
If mouse_button_released = TRUE Then
mouse_button_released = FALSE
if snap=true then
snap=false
bz.delete_pt(selected_point)
updatebz(100,bz)
EndIf
endif
End Select
if edit=true then
with bz
.pt(selected_point).x=mouse_x
.pt(selected_point).y=mouse_y
end with
updatebz(10,bz)
EndIf
Sleep 1
Loop