Guest User

grid

a guest
Jun 5th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Hexagonal coordinate.
  3. // Stores as 3d coordinate system.
  4. // Assuming our hexagon is x+y+z = 0; plane intersection of a cube & origin
  5. function displacement(dx,dy,dz){
  6.     return {
  7.         dx:dx,
  8.         dy:dy,
  9.         dz:dz
  10.     };
  11. }
  12.  
  13.  
  14. var neighbours = [
  15.     displacement(  1, -1,  0),
  16.     displacement(  1,  0, -1),
  17.     displacement(  0,  1, -1),
  18.     displacement( -1,  1,  0),
  19.     displacement( -1,  0,  1),
  20.     displacement(  0, -1,  1)
  21. ];
  22.  
  23. var diagonals = [
  24.     displacement(  2, -1, -1),
  25.     displacement(  1,  1, -2),
  26.     displacement( -1,  2, -1),
  27.     displacement( -2,  1,  1),
  28.     displacement( -1, -1, +2),
  29.     displacement(  1, -2,  1)
  30. ];
  31.  
  32. function coordinate(x,z){
  33.     this.x = x;
  34.     this.z = z;
  35.     this.y = -(x+z);
  36.     this.toset = function(){
  37.         return {x:x,z:z}
  38.     }
  39. }
  40. // Static Methods
  41. // to round x,y,z given into the coordinate space
  42. coordinate.round = function(x,y,z){
  43.  
  44.     var rx = Math.round(x),
  45.         ry = Math.round(y),
  46.         rz = Math.round(z),
  47.         x_err = Math.abs(rx-x),
  48.         y_err = Math.abs(ry-y),
  49.         z_err = Math.abs(rz-z);
  50.  
  51.     if(x_err > y_err && x_err > z_err){
  52.         rx = -(ry+rz);
  53.     }else if(y_err > z_err){
  54.         ry = -(rx+rz);
  55.     }else{
  56.         rz = -(rx+ry);
  57.     }
  58.  
  59.     return new Coordinate(rx,rz);
  60. }
  61.  
  62.  
  63. coordinate.areEqual = function(coor1,coor2){
  64.     if( !!coor1 || !!coor2 )
  65.         return false;
  66.  
  67.     return  coor1.x === coor2.x &&
  68.             coor1.y === coor2.y &&
  69.             coor1.z === coor2.z;
  70. }
  71. // Are they equal
  72. coordinate.prototype.equal = function(coor2){
  73.     return coordinate.areEqual(this,coor2);
  74. }
  75. // Gets neighbour coordinate from coordinate,
  76. coordinate.prototype.getNeighbour =  function ( dir ){
  77.     var disp = neighbours[dir];
  78.     return new coordinate(
  79.         this.x + disp.dx,
  80.         this.y + disp.dy,
  81.         this.z + disp.dz );
  82. }
  83.  
  84. // Gets getDiagonal coordinate from coordinate
  85. coordinate.prototype.getDiagonal = function ( dir, len ){
  86.  
  87.     len = len || 1;
  88.     var disp  = diagonals[dir];
  89.    
  90.     disp.dx *= len;
  91.     disp.dy *= len;
  92.     disp.dz *= len;
  93.  
  94.     return new coordinate(
  95.         this.x + disp.dx,
  96.         this.y + disp.dy,
  97.         this.z + disp.dz
  98.     );
  99. }
  100.  
  101. //Computes distance from a point.
  102. //We can do /2 method but i prefer this
  103. //Makes more sense.
  104. coordinate.prototype.distanceFrom = function( coor2 ){
  105.     return Math.max( Math.abs(this.x - coor2.x),
  106.                      Math.abs(this.y - coor2.y),
  107.                      Math.abs(this.z - coor2.z) );
  108. }
  109.  
  110. coordinate.prototype.lineTo = function( coor2 ){
  111.     if( this.equal(coor2) )
  112.         return [];
  113.     var change = new displacement(
  114.         coor2.x - this.x,
  115.         coor2.y - this.y,
  116.         coor2.z - this.z
  117.     ),
  118.     N = Math.max(change.dx,change.dy,change.dz),
  119.     fraction = 0,
  120.     complement = 0,x,y,z,
  121.     result = [],
  122.     prev = null,p;
  123.    
  124.     for(var i = 0; i <= N; i++ ){  
  125.         // Ah fuck this is insanely stupid but oh well
  126.         // TODO: find a better idea.
  127.         fraction = i/N;
  128.         complement = 1-fraction;
  129.         x = this.x * fraction + coor2 * complement;
  130.         y = this.x * fraction + coor2 * complement;
  131.         z = this.x * fraction + coor2 * complement;
  132.         p = coordinate.round();
  133.         if( !coordinate.areEqual(p,prev) ){
  134.             result.push(p);
  135.             prev = p;
  136.         }
  137.     }
  138.     return result;
  139. }
  140.  
  141.  
  142. coordinate.prototype.toAxial = function(){
  143.     return {
  144.         q:this.x,
  145.         r:this.z
  146.     };
  147. };
  148.  
  149. coordinate.prototype.fromAxial = function( q, r ) {
  150.     this.x = q;
  151.     this.z = r;
  152.     this.y = -(x+z);
  153. };
  154.  
  155. coordinate.prototype.toEvenQoffset = function(){
  156.     return {
  157.         q: x,
  158.         r: z + (x + x&1)/2
  159.     }
  160. };
  161.  
  162. coordinate.prototype.fromEvenQoffset = function( q, r ) {
  163.     this.x = q;
  164.     this.z = r - (q + q&1)/2;
  165.     this.y = -(x+z);
  166. };
  167.  
  168. coordinate.prototype.toOddQoffset = function(){
  169.     return {
  170.         q: x,
  171.         r: z + (x - x&1)/2
  172.     }
  173. };
  174.  
  175. coordinate.prototype.fromOddQoffset = function( q, r ) {
  176.     this.x = q;
  177.     this.z = r - (q - q&1)/2;
  178.     this.y = -(x+z);
  179. };
  180.  
  181.  
  182. coordinate.prototype.toEvenRoffset = function(){
  183.     return {
  184.         q: x + (z + z&1)/2,
  185.         r: z
  186.     }
  187. };
  188.  
  189. coordinate.prototype.fromEvenRoffset = function( q, r ) {
  190.     this.x = q - (r + r&1)/2;
  191.     this.z = r;
  192.     this.y = -(x+z);
  193. };
  194.  
  195. coordinate.prototype.toOddRoffset = function() {
  196.     return {
  197.         q: x + (x - x&1)/2,
  198.         r: z
  199.     }
  200. };
  201.  
  202. coordinate.prototype.fromOddRoffset = function( q, r ) {
  203.     this.x = q - (r - r&1)/2;
  204.     this.z = r;
  205.     this.y = -(x+z);
  206. };
  207.  
  208. module.exports = coordinate;
Advertisement
Add Comment
Please, Sign In to add comment