# Isometric Tiles Algorithm

Printable View

• 03-31-2011, 12:22 AM
MrFish
Isometric Tiles Algorithm
I'm having a lot of trouble getting the row and column from a x, y coordinate on the screen. I've written an algorithm to get the x, y coordinate of a specified row and column but I can't get the reverse of this.

http://img690.imageshack.us/img690/5...ocoordsiso.jpg

Here is the algorithm for the row/column to x/y:

Code:

```public void mousePressed(MouseEvent e) {         int x = e.getX();         int y = e.getY();         int[] coords = DC.XYToCoords(x, y);         int r = coords[0];         int c = coords[1];         System.out.println(r + " " + c); }```
Code:

```public int[] coordsToXY(int r, int c) {         int totalRows = world.getTotalRows();         int totalColumns = world.getTotalColumns();         int width = (totalRows + totalColumns) * (tileWidth/2);         int height = (totalRows + totalColumns) * (tileHeight/2);                         //        Offset by Column    Offset by Row         int y = c*(tileHeight/2) + r*(tileHeight/2);                         //        Offset by Column  Offset by Row    Offset by Isometric    Center of Tile         int x = c*(tileWidth/2) - r*(tileWidth/2)    +    width/2    -      tileWidth/2;                         int[] xy = new int[2];         xy[0] = x;         xy[1] = y;                         return xy; }```
I will show you how I've done the reverse but it isn't going to aid you specifically with the original problem but it may give you an idea.

Viewport gets calculates the starting x and y (sX, sY) of what a user could see so the display doesn't print useless unseen pixels.

Code:

```public void reloadView() {         int row = target.getRow();         int column = target.getColumn();         int yW = target.getWidth();         int yH = target.getHeight();         int tW = DC.getTileWidth();         int tH = DC.getTileHeight();         int totalRows = DC.getWorld().getTotalRows();         int width = DC.getClientWidth();         int height = DC.getClientHeight();         boolean flipped = target.isFlipped();                         int centerX = width/2;         int centerY = height/2;                         int[] xy = DC.coordsToXY(row, column);         int x = xy[0];         int y = xy[1];                         sX = (x-centerX)+(yW/2);         sY = (y-centerY)+(yH+tH)-(totalRows/2*tH); }```
Here is the actual draw method. It doesn't do much but you can see how I use the viewport and where I call the coordToXY() method.

Code:

```public void draw() {         //Get appropriate variables         PlayerManager PManager = DC.getPlayerManager();         Viewport viewport = DC.getViewport();         World world = DC.getWorld();         int width = DC.getClientWidth();         int height = DC.getClientHeight();                         viewport.reloadView();                         int sX = viewport.getSX();         int sY = viewport.getSY();                         //Get this graphic object         Graphics2D g = (Graphics2D)this.getGraphics();                 //We will be drawing everything to this for one quick draw action         BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);         Graphics2D g2 = (Graphics2D)img.getGraphics();                         //Clear screen         g2.clearRect(0, 0, width, height);                         BufferedImage worldImage = world.getImage();         BufferedImage playersImage = PManager.getImage();         g2.drawImage((Image)worldImage, 0, 0, width, height, sX, sY, sX+width, sY+height, null);         //g2.drawImage((Image)playersImage, 0, 0, width, height, sX, sY, sX+width, sY+height, null);                         //Now draw everything to the screen once.         g.drawImage((Image)img, 0, 0, null); }```
Thanks for any help anyone can give me!