Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hexagonal coordinate.
- // Stores as 3d coordinate system.
- // Assuming our hexagon is x+y+z = 0; plane intersection of a cube & origin
- function displacement(dx,dy,dz){
- return {
- dx:dx,
- dy:dy,
- dz:dz
- };
- }
- var neighbours = [
- displacement( 1, -1, 0),
- displacement( 1, 0, -1),
- displacement( 0, 1, -1),
- displacement( -1, 1, 0),
- displacement( -1, 0, 1),
- displacement( 0, -1, 1)
- ];
- var diagonals = [
- displacement( 2, -1, -1),
- displacement( 1, 1, -2),
- displacement( -1, 2, -1),
- displacement( -2, 1, 1),
- displacement( -1, -1, +2),
- displacement( 1, -2, 1)
- ];
- function coordinate(x,z){
- this.x = x;
- this.z = z;
- this.y = -(x+z);
- this.toset = function(){
- return {x:x,z:z}
- }
- }
- // Static Methods
- // to round x,y,z given into the coordinate space
- coordinate.round = function(x,y,z){
- var rx = Math.round(x),
- ry = Math.round(y),
- rz = Math.round(z),
- x_err = Math.abs(rx-x),
- y_err = Math.abs(ry-y),
- z_err = Math.abs(rz-z);
- if(x_err > y_err && x_err > z_err){
- rx = -(ry+rz);
- }else if(y_err > z_err){
- ry = -(rx+rz);
- }else{
- rz = -(rx+ry);
- }
- return new Coordinate(rx,rz);
- }
- coordinate.areEqual = function(coor1,coor2){
- if( !!coor1 || !!coor2 )
- return false;
- return coor1.x === coor2.x &&
- coor1.y === coor2.y &&
- coor1.z === coor2.z;
- }
- // Are they equal
- coordinate.prototype.equal = function(coor2){
- return coordinate.areEqual(this,coor2);
- }
- // Gets neighbour coordinate from coordinate,
- coordinate.prototype.getNeighbour = function ( dir ){
- var disp = neighbours[dir];
- return new coordinate(
- this.x + disp.dx,
- this.y + disp.dy,
- this.z + disp.dz );
- }
- // Gets getDiagonal coordinate from coordinate
- coordinate.prototype.getDiagonal = function ( dir, len ){
- len = len || 1;
- var disp = diagonals[dir];
- disp.dx *= len;
- disp.dy *= len;
- disp.dz *= len;
- return new coordinate(
- this.x + disp.dx,
- this.y + disp.dy,
- this.z + disp.dz
- );
- }
- //Computes distance from a point.
- //We can do /2 method but i prefer this
- //Makes more sense.
- coordinate.prototype.distanceFrom = function( coor2 ){
- return Math.max( Math.abs(this.x - coor2.x),
- Math.abs(this.y - coor2.y),
- Math.abs(this.z - coor2.z) );
- }
- coordinate.prototype.lineTo = function( coor2 ){
- if( this.equal(coor2) )
- return [];
- var change = new displacement(
- coor2.x - this.x,
- coor2.y - this.y,
- coor2.z - this.z
- ),
- N = Math.max(change.dx,change.dy,change.dz),
- fraction = 0,
- complement = 0,x,y,z,
- result = [],
- prev = null,p;
- for(var i = 0; i <= N; i++ ){
- // Ah fuck this is insanely stupid but oh well
- // TODO: find a better idea.
- fraction = i/N;
- complement = 1-fraction;
- x = this.x * fraction + coor2 * complement;
- y = this.x * fraction + coor2 * complement;
- z = this.x * fraction + coor2 * complement;
- p = coordinate.round();
- if( !coordinate.areEqual(p,prev) ){
- result.push(p);
- prev = p;
- }
- }
- return result;
- }
- coordinate.prototype.toAxial = function(){
- return {
- q:this.x,
- r:this.z
- };
- };
- coordinate.prototype.fromAxial = function( q, r ) {
- this.x = q;
- this.z = r;
- this.y = -(x+z);
- };
- coordinate.prototype.toEvenQoffset = function(){
- return {
- q: x,
- r: z + (x + x&1)/2
- }
- };
- coordinate.prototype.fromEvenQoffset = function( q, r ) {
- this.x = q;
- this.z = r - (q + q&1)/2;
- this.y = -(x+z);
- };
- coordinate.prototype.toOddQoffset = function(){
- return {
- q: x,
- r: z + (x - x&1)/2
- }
- };
- coordinate.prototype.fromOddQoffset = function( q, r ) {
- this.x = q;
- this.z = r - (q - q&1)/2;
- this.y = -(x+z);
- };
- coordinate.prototype.toEvenRoffset = function(){
- return {
- q: x + (z + z&1)/2,
- r: z
- }
- };
- coordinate.prototype.fromEvenRoffset = function( q, r ) {
- this.x = q - (r + r&1)/2;
- this.z = r;
- this.y = -(x+z);
- };
- coordinate.prototype.toOddRoffset = function() {
- return {
- q: x + (x - x&1)/2,
- r: z
- }
- };
- coordinate.prototype.fromOddRoffset = function( q, r ) {
- this.x = q - (r - r&1)/2;
- this.z = r;
- this.y = -(x+z);
- };
- module.exports = coordinate;
Advertisement
Add Comment
Please, Sign In to add comment