Java Geometry Algorithm makeNeighbor(Point theHex, int direction)

Here you can find the source of makeNeighbor(Point theHex, int direction)

Description

This method will return a point that is the neighbor of the given point in the given direction.

License

Open Source License

Parameter

Parameter Description
theHex The location of the hexagon whose neighbor is sought.
direction The direction to get the neighbor. Valid range is 0 to 5, with 0 meaning up and advancing clockwise around the hex.

Return

a new point that is the neighbor of the given hex in the requested direction.

Declaration

public static Point makeNeighbor(Point theHex, int direction) 

Method Source Code

//package com.java2s;
/*// w  ww  .  j a  v a2s . c o  m
 * JOGRE (Java Online Gaming Real-time Engine) - API
 * Copyright (C) 2006  Richard Walter (rwalter42@yahoo.com)
 * http://jogre.sourceforge.org
    
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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 the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Point;

public class Main {
    private static int[] neighborCols = { 0, 1, 1, 0, -1, -1 };
    private static int[][] neighborRows = { { -1, -1, 0, 1, 0, -1 }, { -1, 0, 1, 1, 1, 0 } };

    /**
     * This method will return a point that is the neighbor of the given point
     * in the given direction.
     *
     * @param theHex         The location of the hexagon whose neighbor is sought.
     * @param direction      The direction to get the neighbor.  Valid range is
     *                       0 to 5, with 0 meaning up and advancing clockwise
     *                       around the hex.
     * @return a new point that is the neighbor of the given hex in the requested
     *         direction.
     */
    public static Point makeNeighbor(Point theHex, int direction) {
        int colPolarity = (theHex.x & 0x01);
        return new Point(theHex.x + neighborCols[direction], theHex.y + neighborRows[colPolarity][direction]);
    }
}

Related

  1. invVec(final Point2D v)
  2. lonLatToString(Point2D.Double pt)
  3. makeCircle(double xCenter, double yCenter, double r, int nPoints)
  4. makeCornerTo(GeneralPath gp, Point2D cornerPoint, Point2D nextCornerPoint, float radius)
  5. makeLine(Point2D.Double center, Point2D.Double north, Point2D.Double east)
  6. manhattanDistance(Point a, Point b)
  7. mirrorMoveVertically(Point move, int size)
  8. nearestColinearPoint(final double x1, final double y1, final double x2, final double y2, double x, double y)
  9. nearestColinearPoint(final Line2D segment, final Point2D point)