I needed a quick way to calculated the position of a sprite on a sprite sheet if I knew what number sprite it was. Say you had a sheet that was 40x40 and you wanted the 22nd sprite on it. Something like that.
I came up with two ways to get it, one was with a double for loop and the other was just using some multiplication. It doesn't do anything with the xpos, ypos variables other than print the last two calculated. To get the location of the sprite on the sheet you could just multiply these two numbers by the width and height of the sprite cells respectively. This was just a speed test. My second subroutine that relied on pure calculation won out.
I came up with two ways to get it, one was with a double for loop and the other was just using some multiplication. It doesn't do anything with the xpos, ypos variables other than print the last two calculated. To get the location of the sprite on the sheet you could just multiply these two numbers by the width and height of the sprite cells respectively. This was just a speed test. My second subroutine that relied on pure calculation won out.
Code: (Select All)
''Grid finder - locate a point on a grid from a linear number counting from the origin
''count could run right to left and up to down like pixels on a screen
Option _Explicit
Randomize Timer
''define grid parameters
Const GX = 500 ''width
Const GY = 500 ''height
''initialize points on the grid - load the array
Dim g_points&(30000)
Dim i As Long
For i = 0 To UBound(g_points&)
g_points&(i) = Int(Rnd * (GX * GY)) + 1
Next
''setup
Dim start_time As Double
Dim end_time As Double
Dim xpos As Long
Dim ypos As Long
''double loop test
Print "Double Loop Test Start."
start_time = Timer
DoubleLoop g_points&(), xpos, ypos, GX, GY
Print "Last coordinates calculated: "; g_points&(UBound(g_points&)); xpos; ypos
end_time = Timer
Print end_time - start_time
''calculation test
Print "Calculation Test Start."
start_time = Timer
GridCalc g_points&(), xpos, ypos, GX, GY
Print "Last coordinates calculated: "; g_points&(UBound(g_points&)); xpos; ypos
end_time = Timer
Print end_time - start_time
Sleep
End
''------------------Subroutines-------------------
Sub DoubleLoop (points&(), xcoord&, ycoord&, gridsizex&, gridsizey&)
''find the position of a linear value on a grid using a double loop
Dim i&
Dim x&
Dim y&
Dim count&
''parse the array to get the values for each point
For i& = 0 To UBound(points&)
For y& = 0 To gridsizey& - 1
For x& = 0 To gridsizex& - 1
''Calculate the position of the value on the grid
count& = count& + 1
If count& = points&(i&) Then
xcoord& = x&
ycoord& = y&
''reset the count
count& = 0
GoTo xyloop_exit
End If
Next x&
Next y&
xyloop_exit:
Next i&
End Sub ''DoubleLoop
Sub GridCalc (points&(), xcoord&, ycoord&, gridsizex&, gridsizey&)
''find the position of a linear value on a grid using math
Dim i&
''parse the array to get the values for each point
For i& = 0 To UBound(points&)
ycoord& = Int(points&(i&) / gridsizey&)
xcoord& = points&(i&) - (ycoord& * gridsizex&) - 1 ''start at 0
Next i&
End Sub ''GridCalc


