Requesting Catmull Rom tutorial

BlitzMax Forums/BlitzMax Beginners Area/Requesting Catmull Rom tutorial

I'm very interested in learning the Catmull Rom. Although everywhere I go there seems to be too much info. If someone could just paste a code snippet of a 4 point catmull rom, I could probably pick it up from there and add it to my spline editor.

I'm very interested in learning the Catmull rom method. I've seen marvelous uses for it..

Look: http://www.mvps.org/directx/articles/catmull/

Look at that cool highway!!

There's so much you can do with it... This is a must learn for everyone! :)

Java code here:

http://www.cse.unsw.edu.au/~lambert/splines/

Thanks for that Angel, but I'm really looking for a BlitzMax demo that has the following:

one catmull rom spline with 4 points

0 and 4 are those one points that change the way it bends

1 and 2 are the control points

and thats it.. Like something that would be just a few lines of code and nothing extravagant like the spline demos out there. The problem with most spline demos is the programmer makes them so intricate and so advanced that a person like me wont understand the code unless it is studied for hours.

So again, if anybody out there hears my plea. I just ask that an expereinced programmer makes a short blitzmax spline demo with no bells or whistles, so that the basics of a cspline can be discovered.

That's all I am asking.. Just for a very very very BASIC cspline demo.. :)

Please someoen, please do this for the community. I promise I'll take your demo and add it into my spline editor, and I'll create a wonderful tutorial so that everyone can learn csplines without having to study for hours if not days!

Warpy saved the day again.. Man he's a great guy:

For t#=0 To 1 Step .01
   x#=.5*((2*p1.x)+(p2.x-p0.x)*t+(2*p0.x-5*p1.x+4*p2.x-p3.x)*t*t+(3*p1.x-p0.x-3*p2.x+p3.x)*t*t*t)
   y#=.5*((2*p1.y)+(p2.y-p0.y)*t+(2*p0.y-5*p1.y+4*p2.y-p3.y)*t*t+(3*p1.y-p0.y-3*p2.y+p3.y)*t*t*t)
   DrawRect x,y,1,1
Next


THIS is the Catmull Spline :)

Even I have warpycode (tm) somewhere.

crikey, you do? That's amazing!

btw, the entirety of the little catmull rom demo I whipped up is at http://www.blitzbasic.com/codearcs/codearcs.php?code=1522
The rest of the bumf around it might give you some good ideas about how to use it for something useful :)

Here's an adapted version of this code wrapped into a little experimental program.

'Catmull rom splines
'Adapted from code by Warpy and Matt McFarland

'Press Space for a totally new random spline
'Press left arrow to control the previous control point
'Press right arrow to control the next control point
'Press Escape to exit
'Move  mouse to see it adapt

Strict

Type Point
	Field x:Int
	Field y:Int
End Type
Const Accuracy:Double=0.05		'Lower has more line segments
	
SeedRnd(MilliSecs())	'Different each time
SetGraphicsDriver GLMax2DDriver()
Graphics 640,480,0
SetBlend LIGHTBLEND
Local DoAnother:Int
Local ControlPoint:Int
Local Counter:Int
glEnable(GL_LINE_SMOOTH)	'Quick antaliasing hack
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)       'Best antialiasing
glLineWidth(3.0)     '3-pixel width lines
Repeat
	DoAnother=False
	Local NumPoints:Int=Rand(4,10)	'Whatever you like >3
	Local Points:Point[NumPoints]
	For Local p:Int=0 To NumPoints-1
		Points[p]=New Point
		Points[p].x=Rand(20,620)
		Points[p].y=Rand(20,460)
	Next
	ControlPoint=NumPoints/2
	Repeat
		Cls
		Points[ControlPoint].x=MouseX()
		Points[ControlPoint].y=MouseY()
		'Draw controls
		For Local p:Int=0 To NumPoints-1
			SetColor $88,$88,$88
			DrawLine Points[p].x-7,Points[p].y-7,Points[p].x+7,Points[p].y+7
			DrawLine Points[p].x-7,Points[p].y+7,Points[p].x+7,Points[p].y-7
			SetColor $00,$44,$88
			DrawLine Points[p].x,Points[p].y,Points[Min(NumPoints-1,p+1)].x,Points[Min(NumPoints-1,p+1)].y
			DrawText p,Points[p].x+5,Points[p].y+5
		Next
		'Draw segments
		Local PrevX:Double,PrevY:Double
		SetColor $FF,$FF,$FF
		Counter=0
		For Local S:Int=0 To NumPoints-4
			For Local T:Double=0 To 1 Step Accuracy
				Local X:Double=.5:Double*((2*Points[S+1].x)+(Points[S+2].x-Points[S].x)*T+(2*Points[S].x-5*Points[S+1].x+4*Points[S+2].x-Points[S+3].x)*T*T+(3*Points[S+1].x-Points[S].x-3*Points[S+2].x+Points[S+3].x)*T*T*T)
				Local Y:Double=.5:Double*((2*Points[S+1].y)+(Points[S+2].y-Points[S].y)*T+(2*Points[S].y-5*Points[S+1].y+4*Points[S+2].y-Points[S+3].y)*T*T+(3*Points[S+1].y-Points[S].y-3*Points[S+2].y+Points[S+3].y)*T*T*T)
				If PrevX=0 And PrevY=0
					PrevX=X
					PrevY=Y
				EndIf
				DrawLine PrevX,PrevY,X,Y,False
				PrevX=X
				PrevY=Y
				Counter:+1
			Next
		Next
		DrawLine PrevX,PrevY,Points[NumPoints-2].x,Points[NumPoints-2].y
		DrawText "Edges: "+String(Counter),0,10
		DrawText "Controls: "+String(NumPoints),0,20
		DrawText "Curves: "+String(NumPoints-3),0,30
		Flip
		If KeyHit(KEY_SPACE) Then DoAnother=True
		If KeyHit(KEY_LEFT) Then ControlPoint=Max(0,ControlPoint-1)
		If KeyHit(KEY_RIGHT) Then ControlPoint=Min(NumPoints-1,ControlPoint+1)
	Until KeyHit(KEY_ESCAPE) Or DoAnother=True
Until DoAnother=False


Just thought I'd post a similar demo using cubic bezier curves (4 points per curve), adapted from the bezier code by Wedoe.

'Cubic Bezier splines
'Adapted from code Wedoe

'Press Space for a totally new random spline
'Press left arrow to control the previous control point
'Press right arrow to control the next control point
'Press Escape to exit
'Move  mouse to see it adapt

Strict

Type Point
	Field x:Int
	Field y:Int
End Type
Const Accuracy:Double=0.03		'Lower has more line segments
	
SeedRnd(MilliSecs())	'Different each time
SetGraphicsDriver GLMax2DDriver()
Graphics 640,480,0
SetBlend LIGHTBLEND
Local DoAnother:Int
Local ControlPoint:Int
Local Counter:Int
glEnable(GL_LINE_SMOOTH)	'Quick antaliasing hack
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
glLineWidth(3.0)
Repeat
	DoAnother=False
	Local NumPoints:Int=Rand(4,16)	'Whatever >3
	NumPoints=NumPoints-(NumPoints Mod 3)+1	'3 points per bezier plus 1, 4th point of each bez is shared
	Local Points:Point[NumPoints]
	For Local p:Int=0 To NumPoints-1
		Points[p]=New Point
		Points[p].x=Rand(20,620)
		Points[p].y=Rand(20,460)
	Next
	ControlPoint=NumPoints/2
	Repeat
		Cls
		Points[ControlPoint].x=MouseX()
		Points[ControlPoint].y=MouseY()
		'Draw controls
		For Local p:Int=0 To NumPoints-1
			SetColor $88,$88,$88
			DrawLine Points[p].x-7,Points[p].y-7,Points[p].x+7,Points[p].y+7
			DrawLine Points[p].x-7,Points[p].y+7,Points[p].x+7,Points[p].y-7
			SetColor $00,$44,$88
			DrawLine Points[p].x,Points[p].y,Points[Min(NumPoints-1,p+1)].x,Points[Min(NumPoints-1,p+1)].y
			DrawText p,Points[p].x+5,Points[p].y+5
		Next
		'Draw segments
		Local PrevX:Double,PrevY:Double
		SetColor $FF,$FF,$FF
		Counter=0
		For Local S:Int=0 To NumPoints-4 Step 3
			For Local T:Double=0 To 1 Step Accuracy
				Local X:Double=Points[s].x*(1-T)^3 + 3*Points[s+1].x*(1-T)^2*T + 3*Points[s+2].x*(1-T)*T^2 + Points[s+3].x*T^3
				Local Y:Double=Points[s].y*(1-T)^3 + 3*Points[s+1].y*(1-T)^2*T + 3*Points[s+2].y*(1-T)*T^2 + Points[s+3].y*T^3
				If PrevX=0 And PrevY=0
					PrevX=X
					PrevY=Y
				EndIf
				DrawLine PrevX,PrevY,X,Y,False
				PrevX=X
				PrevY=Y
				Counter:+1
			Next
		Next
		DrawLine PrevX,PrevY,Points[NumPoints-1].x,Points[NumPoints-1].y
		DrawText "Edges: "+String(Counter),0,10
		DrawText "Controls: "+String(NumPoints),0,20
		DrawText "Curves: "+String(NumPoints/3),0,30
		Flip
		If KeyHit(KEY_SPACE) Then DoAnother=True
		If KeyHit(KEY_LEFT) Then ControlPoint=Max(0,ControlPoint-1)
		If KeyHit(KEY_RIGHT) Then ControlPoint=Min(NumPoints-1,ControlPoint+1)
	Until KeyHit(KEY_ESCAPE) Or DoAnother=True
Until DoAnother=False