Java Distance Calculate computeDistanceMap(int[][] image)

Here you can find the source of computeDistanceMap(int[][] image)

Description

compute Distance Map

License

Open Source License

Declaration

public static int[][] computeDistanceMap(int[][] image) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 ** ANGRYBIRDS AI AGENT FRAMEWORK//from  www. j  a va 2 s . c om
 ** Copyright (c) 2013,XiaoYu (Gary) Ge, Stephen Gould,Jochen Renz
 **  Sahan Abeyasinghe, Jim Keys, Kar-Wai Lim, Zain Mubashir,  Andrew Wang, Peng Zhang
 ** All rights reserved.
 **This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. 
 **To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ 
 *or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
 *****************************************************************************/

public class Main {
    public static int[][] computeDistanceMap(int[][] image) {

        // first pass: top-left to bottom-right
        for (int y = 0; y < image.length; y++) {
            for (int x = 0; x < image[y].length; x++) {
                if (image[y][x] != 0) {
                    image[y][x] = 0;
                } else {
                    image[y][x] = image.length + image[y].length;
                    if (y > 0)
                        image[y][x] = Math.min(image[y][x],
                                image[y - 1][x] + 1);
                    if (x > 0)
                        image[y][x] = Math.min(image[y][x],
                                image[y][x - 1] + 1);
                }
            }
        }

        // second pass: bottom-right to top-left
        for (int y = image.length - 1; y >= 0; y--) {
            for (int x = image[y].length - 1; x >= 0; x--) {
                if (y < image.length - 1)
                    image[y][x] = Math
                            .min(image[y][x], image[y + 1][x] + 1);
                if (x < image[y].length - 1)
                    image[y][x] = Math
                            .min(image[y][x], image[y][x + 1] + 1);
            }
        }

        return image;
    }
}

Related

  1. computeDistance(int p1x, int p1y, int p2x, int p2y)
  2. computeDistance(long[] a, long[] b)
  3. computeDistanceAndBearingFast(double lat1, double lon1, double lat2, double lon2, float[] results)
  4. computeDistanceImpl(final String firstStringInAlgorithm, final String secondStringInAlgorithm)
  5. computeDistanceInMiles(Double lat1, Double lon1, Double lat2, Double lon2)
  6. computeDistanceSqr(double t, double b1, double b2, double b3, double b4, double b5, double b6)
  7. computeDistanceUsingRadians(double lat1Radians, double lng1Radians, double lat2Radians, double lng2Radians)
  8. dist(double lat1, double long1, double lat2, double long2)
  9. dist(double x1, double y1, double z1, double x2, double y2, double z2)