10-11-2024, 12:05 AM
clean summary of all things bezier -- nth order curves, points, and derivatives
Code: (Select All)
sw = 800
sh = 600
screen _newimage(sw, sh, 32)
if _resize then
sw = _resizewidth - 20
sh = _resizeheight - 20
screen _newimage(sw, sh, 32)
end if
n = 8
dim x(n - 1), y(n - 1)
x(0)=sw/2:y(0)=sh/7
x(1)=sw/3:y(1)=2*sh/7
x(2)=2*sw/3:y(2)=3*sh/7
x(3)=sw/5:y(3)=4*sh/7
x(4)=sw/2:y(4)=5*sh/7
x(5)=sw/5:y(5)=5*sh/7
x(6)=sw/2:y(6)=6*sh/7
x(7)=4*sw/5:y(7)=6*sh/7
cls
for i=0 to n - 1
circle (x(i), y(i)), 3, _rgb(255,255,0)
next
preset (x(0), y(0))
ox = x(0)
oy = y(0)
dt = 0.02
for t=0 to 1 step dt
bx = 0
by = 0
dx = 0
dy = 0
for i=0 to n - 1
bin = 1
for j=1 to i
bin = bin*(n - j)/j
next
p = bin*((1 - t)^(n - 1 - i))*(t^i)
bx = bx + p*x(i)
by = by + p*y(i)
q = bin*((1 - t)^(n - 2 - i))*(t^(i - 1))*(i - n*t + t)
dx = dx + q*x(i)
dy = dy + q*y(i)
next
'hairline
if abs(bx - ox)>1 and abs(by - oy)>1 then
'line (ox, oy)-(bx, by), _rgb(55,55,55)
ox = bx
oy = by
end if
'point
pset (bx, by), _rgb(255,0,0)
'derivative vector
m = sqr(dx*dx + dy*dy)
line -step(7*dx/m, 7*dy/m), _rgb(0,255,0)
next
'line (ox, oy)-(bx, by), _rgb(55,55,55)