1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: AbstractFlatHexagonDirectionGroup.java,v 1.3 2005/09/10 11:23:52 mat007 Exp $ 29 */ 30 31 package jtge.util.grid.directiongroup; 32 33 import jtge.util.grid.Coordinate; 34 import jtge.util.grid.direction.Direction; 35 import jtge.util.grid.direction.East; 36 import jtge.util.grid.direction.North; 37 import jtge.util.grid.direction.NorthEast; 38 import jtge.util.grid.direction.NorthWest; 39 import jtge.util.grid.direction.South; 40 import jtge.util.grid.direction.SouthEast; 41 import jtge.util.grid.direction.SouthWest; 42 import jtge.util.grid.direction.West; 43 44 /*** 45 * Abstract Hexagon Direction Group for Flat Hexagon Grid. 46 * 47 * @author Jean-Laurent Fabre de Morlhon 48 * @version $Id: AbstractFlatHexagonDirectionGroup.java,v 1.3 2005/09/10 11:23:52 mat007 Exp $ 49 */ 50 public abstract class AbstractFlatHexagonDirectionGroup extends AbstractDirectionGroup 51 { 52 /*** 53 * Create an abstract flat hexagon direction group. 54 */ 55 public AbstractFlatHexagonDirectionGroup() 56 { 57 add( Direction.NORTH ); 58 add( Direction.NORTH_EAST ); 59 add( Direction.SOUTH_EAST ); 60 add( Direction.SOUTH ); 61 add( Direction.SOUTH_WEST ); 62 add( Direction.NORTH_WEST ); 63 } 64 65 /*** 66 * Test if a given coordinate must be shifted. 67 * 68 * @param coordinate the coordinate 69 * @return whether the coordinate must be shifted or not 70 */ 71 protected abstract boolean shift( final Coordinate coordinate ); 72 73 /*** 74 * {@inheritDoc} 75 */ 76 public final Coordinate transform( final Coordinate coordinate, final NorthEast direction ) 77 { 78 if( coordinate == null ) 79 return null; 80 if( !shift( coordinate ) ) 81 return direction.transform( coordinate ); 82 final Coordinate result = (Coordinate)coordinate.clone(); 83 result.setX( result.getX() + 1 ); 84 return result; 85 } 86 87 /*** 88 * {@inheritDoc} 89 */ 90 public final Coordinate transform( final Coordinate coordinate, final NorthWest direction ) 91 { 92 if( coordinate == null ) 93 return null; 94 if( !shift( coordinate ) ) 95 return direction.transform( coordinate ); 96 final Coordinate result = (Coordinate)coordinate.clone(); 97 result.setX( result.getX() - 1 ); 98 return result; 99 } 100 101 /*** 102 * {@inheritDoc} 103 */ 104 public final Coordinate transform( final Coordinate coordinate, final SouthEast direction ) 105 { 106 if( coordinate == null ) 107 return null; 108 if( shift( coordinate ) ) 109 return direction.transform( coordinate ); 110 final Coordinate result = (Coordinate)coordinate.clone(); 111 result.setX( result.getX() + 1 ); 112 return result; 113 } 114 115 /*** 116 * {@inheritDoc} 117 */ 118 public final Coordinate transform( final Coordinate coordinate, final SouthWest direction ) 119 { 120 if( coordinate == null ) 121 return null; 122 if( shift( coordinate ) ) 123 return direction.transform( coordinate ); 124 final Coordinate result = (Coordinate)coordinate.clone(); 125 result.setX( result.getX() - 1 ); 126 return result; 127 } 128 129 /*** 130 * {@inheritDoc} 131 */ 132 public final Coordinate transform( final Coordinate coordinate, final North direction ) 133 { 134 return direction.transform( coordinate ); 135 } 136 137 /*** 138 * {@inheritDoc} 139 */ 140 public final Coordinate transform( final Coordinate coordinate, final South direction ) 141 { 142 return direction.transform( coordinate ); 143 } 144 145 /*** 146 * {@inheritDoc} 147 */ 148 public final Coordinate transform( final Coordinate coordinate, final West direction ) 149 { 150 throw new UnsupportedOperationException(); 151 } 152 153 /*** 154 * {@inheritDoc} 155 */ 156 public final Coordinate transform( final Coordinate coordinate, final East direction ) 157 { 158 throw new UnsupportedOperationException(); 159 } 160 }