Java Distance Calculate distance(int x, int y, int targetX, int targetY, int minDist, int[] outCoords)

Here you can find the source of distance(int x, int y, int targetX, int targetY, int minDist, int[] outCoords)

Description

Calculates the distance between real position and a specific point in the grid

License

Open Source License

Parameter

Parameter Description
x real X-coord
y real Y-coord
targetX X target point (grid)
targetY Y target point (grid)
minDist current minimum distance
outCoords the coordinates associated with the minimum distance found

Return

The real distance or the current minDist if the point is out of the grid or empty

Declaration

private static int distance(int x, int y, int targetX, int targetY, int minDist, int[] outCoords) 

Method Source Code

//package com.java2s;
/*/*from  w w w . j a v a  2s .co m*/
 *                 [[ Frozen-Bubble ]]
 *
 * Copyright (c) 2000-2003 Guillaume Cottenceau.
 * Java sourcecode - Copyright (c) 2003 Glenn Sanson.
 * Additional source - Copyright (c) 2013 Eric Fortin.
 *
 * This code is distributed under the GNU General Public License
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 or 3, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.
 * 675 Mass Ave
 * Cambridge, MA 02139, USA
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * Java version:
 *    Glenn Sanson <glenn.sanson at free.fr>
 *      (whole Java sourcecode, including JIGA classes
 *             http://glenn.sanson.free.fr/jiga/)
 *
 * Android port:
 *    Pawel Aleksander Fedorynski <pfedor@fuw.edu.pl>
 *    Eric Fortin <videogameboy76 at yahoo.com>
 *    Copyright (c) Google Inc.
 *
 *          [[ http://glenn.sanson.free.fr/fb/ ]]
 *          [[ http://www.frozen-bubble.org/   ]]
 */

public class Main {
    /**
     * Calculates the distance between real position and a specific point in the grid
     * @param x real X-coord
     * @param y real Y-coord
     * @param targetX X target point (grid)
     * @param targetY Y target point (grid)
     * @param minDist current minimum distance
     * @param outCoords the coordinates associated with the minimum distance found
     * @return The real distance or the current minDist if the point is out of the grid or empty
     */
    private static int distance(int x, int y, int targetX, int targetY, int minDist, int[] outCoords) {
        int distance = minDist;

        if (targetX >= 0 && targetX < 8 && targetY >= 0 && targetY < 13) {
            int dx = (targetX << 5) - ((targetY % 2) << 4) - x;
            int dy = targetY * 28 - y;

            distance = dx * dx + dy * dy;
            if (distance < minDist) {
                outCoords[0] = targetX;
                outCoords[1] = targetY;
            } else {
                distance = minDist;
            }
        }

        return distance;
    }
}

Related

  1. distance(int first, int second)
  2. distance(int fx, int fy, int sx, int sy)
  3. distance(int one, int two)
  4. distance(int px, int py, int x1, int y1, int x2, int y2)
  5. distance(int q1, int r1, int q2, int r2)
  6. Distance(int X1, int X2, int Y1, int Y2)
  7. distance(int x1, int y1, int x2, int y2)
  8. distance(int x1, int y1, int x2, int y2)
  9. distance(int x1, int y1, int x2, int y2)