grid...

Blitz3D Forums/Blitz3D Beginners Area/grid...

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

There ought to be a better way to snap to a grid than to do it this way:


Function snapToGrid(number,size=1)
	num = size
	Repeat
		If num*size > number Then Exit
		num = num*size
	Forever
	down = number Mod num
	
	num = 0
	Repeat 
		num = num+size
		If num > number Then Exit
	Forever
	up = num Mod number

	
	If up > down Then 
		num = number - down
	Else
		num = number + up
	EndIf
	Return num
End Function


edit: I think this is better...uses some globals and a type...the idea is that the grid starts at -width/2 and -depth/2...
Global Grid_width = 200
Global Grid_depth = 200
Global Grid_step = 1
Global med_size = 4
Global large_size = 8
.beginning
x = Input("x: ")
z = Input("z: ")
size = Input("size: ")
test.array = snapToGrid(x,z,size)
Print "result x: "+test\a[1]
Print "result z: "+test\a[2]
Goto beginning

Type array
	Field a[1000]
	Field s
End Type

Function snapToGrid.array(x,z,size=1)
	Select size
		Case 1
			size = Grid_step
		Case 2
			size = Grid_step*med_size
		Case 3
			size = Grid_step*large_size
	End Select
	
	numx = ((Floor(Float(x + grid_width/2)/size + .5)) * size) - grid_width/2
	numz = ((Floor(Float(z + grid_depth/2)/size + .5)) * size) - grid_depth/2
	
	a.array = New array
	a\s = 2
	a\a[1] = numx
	a\a[2] = numz
		
	Return a
End Function


Any ideas?