Tileset Tile Indeces

Chrono23
10-09-2005, 10:20 AM
I'm working on a routine that takes a single integer value as the index for a tile (the tile set is 5 tiles wide by an infitite number long) what I';m trying to do is pass the index number down to a rectangle class that will return the corresponding bitmap with the desired tile. However, I'm not sure how to calculate the x and y values from just one index variable. I'm thinking something like modulus is probably used, but I'm in the dark. Any help? I've included what I have for my souce code so far:


Public Function GrabTile(ByVal Index As Integer) As Bitmap
Dim srcRect As New Rectangle 'rect to copy from the bitmap object
Dim retBmp As Bitmap 'the bitmap to return
Dim g As Graphics = Graphics.FromImage(retBmp) 'this will draw onto retBmp

'set the x,y coordinates of the rect to correspond with the sequential index number
srcrect.X = ???
srcRect.Y = ???
srcRect.Width = TILEWIDTH
srcRect.Height = TILEHEIGHT

'now draw the tile into the retBmp and return it
g.DrawImage(bmpTilemap, 0, 0, srcRect, GraphicsUnit.Pixel)
Return retBmp
End Function

bear24rw
10-09-2005, 02:24 PM
The way i would do it is have the tiles labeled like so:

1 2 3 4 5
6 7 8 9 10
...

Then say, you passed 6...
6 / 5 (because there is 5 tiles across) would give you 1.2
that means, 2nd row and its the frist tile....

That should work for all of them, this is untested however


Well, as it turns out i just got lucky with 6 cause it works out, but if you try it with anyother number... say 7, it doesnt work...

What about this...

Say you passed 8...
8/5 = 2 'after rounding
(2*5)-8 =2

so now...
8/5 = row its in... so we know that 8 is in 2nd row
(row*5) - 8 = how far in it is, starting with 0, so in this case its 3 in

This works with all the numbers i tried...

im not sure how you round with VB though


Seems like im just getting lucky today... cause i just tried it with 2, 10 and 7 and they didn't work... :(
Maybe my ideas will help you out, cause im stumped...

Check this out... i think i got it

Dim a as Double
Dim b as Integer
a = index / 5
b = Convert.ToInt32(a) 'chop of decimal

b,(5 * (a - b)) -1 is your cordinates

So if you wanted to put it in your code...

Public Function GrabTile(ByVal Index As Integer) As Bitmap
Dim srcRect As New Rectangle 'rect to copy from the bitmap object
Dim retBmp As Bitmap 'the bitmap to return
Dim g As Graphics = Graphics.FromImage(retBmp) 'this will draw onto retBmp

Dim a as Double
Dim b as Integer

a = Index / 5
b = Convert.ToInt32(a) 'chop of decimal

'set the x,y coordinates of the rect to correspond with the sequential index number
srcrect.X = b * TILEWIDTH
srcRect.Y = ((5 * (a - b)) -1) * TILEHEIGHT ' -1 for a zero based index
srcRect.Width = TILEWIDTH
srcRect.Height = TILEHEIGHT

'now draw the tile into the retBmp and return it
g.DrawImage(bmpTilemap, 0, 0, srcRect, GraphicsUnit.Pixel)
Return retBmp
End Function


Hope that helps

Iceplug
10-10-2005, 06:15 AM
I think bear24rw has the right idea, but to simplify, let there be 0s (that makes calculations easier).

0 1 2 3 4 5 6 7
8 9 A B C D E F
(forgive me for hexadecimal representation of the indices :) )

So, if you had R rows with C items in each row (C columns)
then, if the selected row Y were 0, Index = X, where X is the selected column
If Y = 1, then you've passed C items already, so you add C to the result, giving you
Index = C + X
Similarly, if Y = 2, you'd passed 2*C items, and you'd end up with Index = 2 * C + X
The pattern should be easy to see now:

Index = Y * C + X, where C is the number of elements in one row.

To get Y and X from an index, first consider Y.
If Index is any number from 0 to C - 1, then Y = 0 and X = Index
If you go up to the next row, C is added to both boundaries, so therefore if
Index is from C to 2 *C - 1, Y = 1 and X = Index - C
Likewise if Index is from 2 * C to 3 * C - 1, Y = 2 and X = Index - 2 * C

Now, Index \ C (integer division, we don't need trailing decimals in this case) will easily give you the value of Y. (Treating the number of items in a row as a 'stride')
From there, you can either do
X = Index - Y * C,
or simply X = Index Mod C, since Index - Y * C is merely the remainder of Index / C.

In summary:
Index = Y * C + X
Y = Index \ C
X = Index Mod C
:)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum