Pulled tile calculations out into a handy utility method.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3082 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2004-08-17 00:48:40 +00:00
parent cb5c7c5d22
commit b7d43b786f
2 changed files with 29 additions and 16 deletions
+24 -1
View File
@@ -1,5 +1,5 @@
//
// $Id: GeomUtil.java,v 1.7 2004/02/25 14:42:30 mdb Exp $
// $Id: GeomUtil.java,v 1.8 2004/08/17 00:48:40 mdb Exp $
package com.threerings.geom;
@@ -207,4 +207,27 @@ public class GeomUtil
}
return source;
}
/**
* Returns the rectangle containing the specified tile in the supplied
* larger rectangle. Tiles go from left to right, top to bottom.
*/
public static Rectangle getTile (
int width, int height, int tileWidth, int tileHeight, int tileIndex)
{
// figure out from whence to crop the tile
int tilesPerRow = width / tileWidth;
// if we got a bogus region, return bogus tile bounds
if (tilesPerRow == 0) {
return new Rectangle(0, 0, width, height);
}
int row = tileIndex / tilesPerRow;
int col = tileIndex % tilesPerRow;
// crop the tile-sized image chunk from the full image
return new Rectangle(
tileWidth*col, tileHeight*row, tileWidth, tileHeight);
}
}