It is currently Thu Jun 20, 2013 12:00 am Advanced search

distance between tiles

Topics about starter packages, visualizer or any other third party tools.
Please submit new language requests in the Language Request Forum.

distance between tiles

Postby kaputt » Fri Sep 30, 2011 9:03 pm

Hi, I'm newbie with the ants. I'm just downloaded the Java's starter package. I've found this on the source code that I don't understand:
Why the distance is rowDelta * rowDelta + colDelta * colDelta?
I think that the distance should be rowDelta + colDelta, isn't it?

The starter package looks like this:
Code: Select all
   /**
     * Calculates distance between two locations on the game map.
     *
     * @param t1 one location on the game map
     * @param t2 another location on the game map
     *
     * @return distance between <code>t1</code> and <code>t2</code>
     */
    public int getDistance(Tile t1, Tile t2) {
        int rowDelta = Math.abs(t1.getRow() - t2.getRow());
        int colDelta = Math.abs(t1.getCol() - t2.getCol());
        rowDelta = Math.min(rowDelta, rows - rowDelta);
        colDelta = Math.min(colDelta, cols - colDelta);
        return rowDelta * rowDelta + colDelta * colDelta; // is that right?
    }
kaputt
Cadet
 
Posts: 4
Joined: Sun Oct 17, 2010 8:00 pm

Re: distance between tiles

Postby athena » Sat Oct 01, 2011 3:10 am

That's the direct distance squared. It is useful for checking vision and attack range. The reason for squaring the distance is to avoid the expensive sqrt call (sqrt[rowDelta^2+colDelta^2]).

In other cases you will need the manhatten distance which is rowDelta + colDelta which is the amount of turns it would take an ant to move between two specific locations.
User avatar
athena
Lieutenant-Colonel
 
Posts: 41
Joined: Wed Sep 29, 2010 10:37 pm
Location: Copenhagen, Denmark

Re: distance between tiles

Postby kaputt » Sat Oct 01, 2011 8:31 am

I've just understood. It's like the view radius, attack radius and spwan radius that are squared too.

I will rename the distance to distance2 (equally that viewRadius2, attackRadius2 and spawnRadius2) and I will implement the distanceManhattan to calculate the amount of turns.

Thank you very much!
kaputt
Cadet
 
Posts: 4
Joined: Sun Oct 17, 2010 8:00 pm

Re: distance between tiles

Postby Antonio » Thu Feb 02, 2012 4:24 pm

Is there any way to calculate distance between two places? I have faced in a problem that, my teacher has gave me a complicated sum of calculating distance between two places. If you have any solution of this problem, please share that here.
Antonio
Cadet
 
Posts: 1
Joined: Thu Feb 02, 2012 4:13 pm


Return to Starter Packages & Tools

Who is online

Users browsing this forum: No registered users and 1 guest

cron