Well if you want to code you very own bezier curves (I see no point in that) I assume you atleast need to be very good at vectors and linear algebra to even get a grip.
Here is some code from Blitz3D (Note that it's not mine but I guess it's public or from the archives) it would be quite easy to convert to BlitzMax.
; // Initialize Graphics!
Graphics 640,480,32,0
SeedRnd MilliSecs()
; // Initialize Control Points for 4 point bezier
x1#=Rand(100,540)
y1#=Rand(100,380)
x2#=Rand(100,540)
y2#=Rand(100,380)
x3#=Rand(100,540)
y3#=Rand(100,380)
x4#=Rand(100,540)
y4#=Rand(100,380)
; // Initialize Control Points for 3 point bezier
a1#=Rand(100,540)
b1#=Rand(100,380)
a2#=Rand(100,540)
b2#=Rand(100,380)
a3#=Rand(100,540)
b3#=Rand(100,380)
; // Initialize buffer for drawing
SetBuffer BackBuffer()
; // Don't worry about this, these are variables for the control point selection and misc
md=False
mdid=0
size=0
detail=3
detailinc#=0
drawmode=0
; // This is just a font.
verdana12=LoadFont("Verdana",12,False,False,False)
; // Start loop! Exit when escape key is pressed.
While Not KeyDown(1)
; // Clear screen!
Cls
; // Check for control point selection, don't worry about this little doozy.
Gosub control_point_selection
; // Print instructions!
Color 255,255,255
SetFont verdana12
Text 0,(12*0),"Bezier Functions (3 and 4 Control Points)"
Text 0,(12*1),"by Jonathan Nguyen"
Text 0,(12*2),\"world_creator@..."
Text 0,(12*4),"Drag control points to manipulate bezier"
Text 0,(12*5),"Space: Toggle line drawing."
Text 0,(12*6),"-/+: Change detail of curve"
If drawmode=0
Text 0,(12*8),"Current Draw Mode: Dots"
Else
Text 0,(12*8),"Current Draw Mode: Line"
EndIf
Text 0,(12*9),"Current Detail Level: "+Str$(detail)
; // User controls
If KeyHit(57)=True Then drawmode=drawmode+1
If drawmode=2 Then drawmode=0
If KeyHit(12)=True Then detail=detail-1
If KeyHit(13)=True Then detail=detail+1
If detail<1 Then detail=1
If detail>6 Then detail=6
Select detail
Case 1
detailinc#=0.1
Case 2
detailinc#=0.05
Case 3
detailinc#=0.01
Case 4
detailinc#=0.005
Case 5
detailinc#=0.001
Case 6
detailinc#=0.0005
End Select
; // Prep for drawing 4 point bezier
Color 255,255,128
If drawmode=0
t#=0
endt#=1
Else
t#=detailinc#
endt#=1+detailinc#
EndIf
; // Start loop to cycle through t# values (distance along curve)
Repeat
; // Call functions to retrieve bezier curve coordinates.
x#=b4x#(t#,x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#)
y#=b4y#(t#,x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#)
; // Draw the curve based on draw mode.
If drawmode=0
Plot x#,y#
Else
Line x#,y#,b4x#(t#-detailinc#,x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#),b4y#(t#-detailinc#,x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#)
EndIf
; // Increment the t# value
t#=t#+detailinc#
; // End that loop
Until t#=>endt#
; // Prep for drawing 3 point bezier
Color 128,255,255
If drawmode=0
t#=0
endt#=1
Else
t#=detailinc#
endt#=1+detailinc#
EndIf
; // Start loop to cycle through t# values (distance along curve)
Repeat
; // Call functions to retrieve bezier curve coordinates.
x#=b3x#(t#,a1#,b1#,a2#,b2#,a3#,b3#)
y#=b3y#(t#,a1#,b1#,a2#,b2#,a3#,b3#)
; // Draw the curve based on draw mode.
If drawmode=0
Plot x#,y#
Else
Line x#,y#,b3x#(t#-detailinc#,a1#,b1#,a2#,b2#,a3#,b3#),b3y#(t#-detailinc#,a1#,b1#,a2#,b2#,a3#,b3#)
EndIf
; // Increment the t# value
t#=t#+detailinc#
; // End that loop
Until t#=>endt#
; // Draw Cursor
Line MouseX()-4,MouseY(),MouseX()+4,MouseY()
Line MouseX(),MouseY()-4,MouseX(),MouseY()+4
; // Flip Back and Front Buffers
Flip False
; // End loop!
Wend
End
; ////////////////////////////////////////////////////////
; // Bezier Functions
; How it works is you enter the control points and the time
; and it'll give you the coordinate along the curve based
; on the time. How the time works is that at 0 it's at the
; beginning point and at 1 at the end point. So basically,
; the middle point is when t#=0.5. One of the neat things
; about this is that if you just increase the function by
; one dimension then you can create 3d bezier curves.
; // 4 Point
; The parameters for this one:
; t# = distance along curve (0.0 To 1.0)
; (x1#,y1#) = Coordinates for start point
; (x2#,y2#) = Coordinates for first control point
; (x3#,y3#) = Coordinates for second control point
; (x4#,y4#) = Coordinates for end point
; // X Coordinate Function
Function b4x#(t#,x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#)
Return x1#*(1-t#)^3 + 3*x2#*t#*(1-t#)^2 + 3*x3#*t#^2*(1-t#) + x4#*t#^3
End Function
; // Y Coordinate Function
Function b4y#(t#,x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#)
Return y1#*(1-t#)^3 + 3*y2#*t#*(1-t#)^2 + 3*y3#*t#^2*(1-t#) + y4#*t#^3
End Function
; // 3 Point
; The only difference in this one is that the two control
; points are merged into one if you don't need two. Yes, you
; could have done it on your own but knowing programmers you
; probably would have been too lazy to ;).
; The parameters for this one:
; t# = distance along curve (0.0 To 1.0)
; (x1#,y1#) = Coordinates for start point
; (x2#,y2#) = Coordinates for first control point
; (x3#,y3#) = Coordinates for end point
; // X Coordinate Function
Function b3x#(t#,x1#,y1#,x2#,y2#,x3#,y3#)
Return x1#*(1-t#)^3 + 3*x2#*t#*(1-t#)^2 + 3*x2#*t#^2*(1-t#) + x3#*t#^3
End Function
; // Y Coordinate Function
Function b3y#(t#,x1#,y1#,x2#,y2#,x3#,y3#)
Return y1#*(1-t#)^3 + 3*y2#*t#*(1-t#)^2 + 3*y2#*t#^2*(1-t#) + y3#*t#^3
End Function
; ////////////////////////////////////////////////////////
; // Don't worry about any of this, this is just the control point selection code.
.control_point_selection
Color 128,128,128
Line x1#,y1#,x2#,y2#
Line x2#,y2#,x3#,y3#
Line x3#,y3#,x4#,y4#
Line a1#,b1#,a2#,b2#
Line a2#,b2#,a3#,b3#
If md=True And mdid=1
x1#=MouseX()
y1#=MouseY()
Color 255,0,0
size=4
Else
If mouseover(x1#-2,y1#-2,x1#+2,y1#+2)=True
Color 0,255,0
size=4
Else
Color 255,255,0
size=2
EndIf
EndIf
Oval x1#-size,y1#-size,size*2+1,size*2+1
If md=True And mdid=2
x2#=MouseX()
y2#=MouseY()
Color 255,0,0
size=4
Else
If mouseover(x2#-2,y2#-2,x2#+2,y2#+2)=True
Color 0,255,0
size=4
Else
Color 255,255,0
size=2
EndIf
EndIf
Oval x2#-size,y2#-size,size*2+1,size*2+1
If md=True And mdid=3
x3#=MouseX()
y3#=MouseY()
Color 255,0,0
size=4
Else
If mouseover(x3#-2,y3#-2,x3#+2,y3#+2)=True
Color 0,255,0
size=4
Else
Color 255,255,0
size=2
EndIf
EndIf
Oval x3#-size,y3#-size,size*2+1,size*2+1
If md=True And mdid=4
x4#=MouseX()
y4#=MouseY()
Color 255,0,0
size=4
Else
If mouseover(x4#-2,y4#-2,x4#+2,y4#+2)=True
Color 0,255,0
size=4
Else
Color 255,255,0
size=2
EndIf
EndIf
Oval x4#-size,y4#-size,size*2+1,size*2+1
If md=True And mdid=5
a1#=MouseX()
b1#=MouseY()
Color 255,0,0
size=4
Else
If mouseover(a1#-2,b1#-2,a1#+2,b1#+2)=True
Color 0,0,255
size=4
Else
Color 0,255,255
size=2
EndIf
EndIf
Rect a1#-size,b1#-size,size*2+1,size*2+1
If md=True And mdid=6
a2#=MouseX()
b2#=MouseY()
Color 255,0,0
size=4
Else
If mouseover(a2#-2,b2#-2,a2#+2,b2#+2)=True
Color 0,0,255
size=4
Else
Color 0,255,255
size=2
EndIf
EndIf
Rect a2#-size,b2#-size,size*2+1,size*2+1
If md=True And mdid=7
a3#=MouseX()
b3#=MouseY()
Color 255,0,0
size=4
Else
If mouseover(a3#-2,b3#-2,a3#+2,b3#+2)=True
Color 0,0,255
size=4
Else
Color 0,255,255
size=2
EndIf
EndIf
Rect a3#-size,b3#-size,size*2+1,size*2+1
If mouseover(x1#-2,y1#-2,x1#+2,y1#+2)=True
If md=True
mdid=1
Else
If MouseDown(1)=True Then md=True
EndIf
EndIf
If mouseover(x2#-2,y2#-2,x2#+2,y2#+2)=True
If md=True
mdid=2
Else
If MouseDown(1)=True Then md=True
EndIf
EndIf
If mouseover(x3#-2,y3#-2,x3#+2,y3#+2)=True
If md=True
mdid=3
Else
If MouseDown(1)=True Then md=True
EndIf
EndIf
If mouseover(x4#-2,y4#-2,x4#+2,y4#+2)=True
If md=True
mdid=4
Else
If MouseDown(1)=True Then md=True
EndIf
EndIf
If mouseover(a1#-2,b1#-2,a1#+2,b1#+2)=True
If md=True
mdid=5
Else
If MouseDown(1)=True Then md=True
EndIf
EndIf
If mouseover(a2#-2,b2#-2,a2#+2,b2#+2)=True
If md=True
mdid=6
Else
If MouseDown(1)=True Then md=True
EndIf
EndIf
If mouseover(a3#-2,b3#-2,a3#+2,b3#+2)=True
If md=True
mdid=7
Else
If MouseDown(1)=True Then md=True
EndIf
EndIf
If MouseDown(1)=False And md=True
md=False
mdid=0
EndIf
Color 255,255,0
Text x1#+8,y1#,"4p,Start",False,True
Text x2#+8,y2#,"4p,CP 1",False,True
Text x3#+8,y3#,"4p,CP 2",False,True
Text x4#+8,y4#,"4p,End",False,True
Color 0,255,255
Text a1#+8,b1#,"3p,Start",False,True
Text a2#+8,b2#,"3p,CP",False,True
Text a3#+8,b3#,"3p,End",False,True
Return
; // The end of that needlessly long control point selection code!
; // This is just a small mouse over check function I whipped up.
Function mouseover(x1,y1,x2,y2)
If MouseX()>x1 And MouseX()<x2
If MouseY()>y1 And MouseY()<y2
Return True
Else
Return False
EndIf
Else
Return False
EndIf
End Function
|