package Current.basic; /** A HexPosition stores a point in the Hexagoan Lattice * A point is stored as a list of 3 integers p[0], p[1], and p[2] * The vertices of a hexagonal grid admit a natural 2-coloring. We * distinguish the colors by the terms even and odd. Even vertices * are stored as triples with the property that *
p[0]+p[1]+p[2] = 0
* and odd vertices satisfy *
p[0]+p[1]+p[2] = 1
*

* Really, A HexPosition represents an element of the automorphism * group (which is ablelian) of the folding map from the universal * abelian cover to the triangle AC->T. That is a HexPosition is a * map AC->AC. * */ public class HexPosition { /** a list of 3 integers */ public int p[]; /** Initialize to the identity element (0,0,0) */ public HexPosition(){ p=new int[3]; } // public static HexPosition origin() { // int[] x={0,0,0}; // return new HexPosition(x); // } public HexPosition(int[] p) { this.p=p; } /** copy constructor */ public HexPosition(HexPosition hp){ p=new int[3]; p[0]=hp.p[0]; p[1]=hp.p[1]; p[2]=hp.p[2]; } /** Return true if the vertex represented is even. *
The vertices of the hexagonal grid can be colored black and * white, so no two adjacent vertices are the same color. * Even vertices are those that are the same color as the origin. */ public final boolean even(){ return (p[0]+p[1]+p[2])%2==0; } public final int get(int i) { return p[i]; } /** Group mutliplication */ public static HexPosition multiply(HexPosition hp1, HexPosition hp2) { HexPosition ret=new HexPosition(); if (hp1.even()) { ret.p[0]=hp1.p[0]+hp2.p[0]; ret.p[1]=hp1.p[1]+hp2.p[1]; ret.p[2]=hp1.p[2]+hp2.p[2]; } else { ret.p[0]=hp1.p[0]-hp2.p[0]; ret.p[1]=hp1.p[1]-hp2.p[1]; ret.p[2]=hp1.p[2]-hp2.p[2]; } return ret; } public final void rightMultiplyBy(HexPosition hp) { if (even()) { p[0]+=hp.p[0]; p[1]+=hp.p[1]; p[2]+=hp.p[2]; } else { p[0]-=hp.p[0]; p[1]-=hp.p[1]; p[2]-=hp.p[2]; } } public final void leftMultiplyBy(HexPosition hp) { if (hp.even()) { p[0]+=hp.p[0]; p[1]+=hp.p[1]; p[2]+=hp.p[2]; } else { p[0]=hp.p[0]-p[0]; p[1]=hp.p[1]-p[1]; p[2]=hp.p[2]-p[2]; } } /** Move the vertex along the edge marked by i (i=0,1,2)*/ public final void shift(int i){ if (even()) p[i]++; else p[i]--; } /** Move the vertex along the edge marked by i (i=0,1,2)*/ public final void shiftback(int i){ p[i]=1-p[i]; p[(i+1)%3]=-p[(i+1)%3]; p[(i+2)%3]=-p[(i+2)%3]; } /** Compute the path distance to the origin*/ public final int dist(){ int ret=0; if (p[0]>=0) ret+=p[0]; else ret-=p[0]; if (p[1]>=0) ret+=p[1]; else ret-=p[1]; if (p[2]>=0) ret+=p[2]; else ret-=p[2]; return ret; } public String toString(){ return "("+p[0]+", "+p[1]+", "+p[2]+")"; } }