Created
July 13, 2013 22:41
-
-
Save shadda/5992488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() | |
{ | |
var NEIGHBORS_DI = [0, 1, 1, 0, -1,-1]; | |
var NEIGHBORS_DJ = [ | |
[-1, -1, 0, 1, 0, -1], | |
[-1, 0, 1, 1, 1, 0] | |
]; | |
var NUM_NEIGHBORS = 6; | |
var HexTile = function(radius) | |
{ | |
this.mX = 0; //Cell's left coordinate | |
this.mY = 0; //Cell's top coordinate | |
this.mI = 0; //Cell's horizontal grid coordinate | |
this.mJ = 0; //Cell's vertical grid coordinate | |
this.RADIUS = radius; | |
this.WIDTH = this.RADIUS * 2; | |
this.HEIGHT = parseInt(radius * Math.sqrt(3)); | |
this.SIDE = this.RADIUS * 3 / 2; | |
this.CORNERS_DX = [ | |
this.RADIUS / 2, | |
this.SIDE, | |
this.WIDTH, | |
this.SIDE, | |
this.RADIUS / 2, | |
0 | |
]; | |
this.CORNERS_DY = [ | |
0, 0, | |
this.HEIGHT / 2, | |
this.HEIGHT, | |
this.HEIGHT, | |
this.HEIGHT / 2 | |
]; | |
} | |
HexTile.prototype.getLeft = function() | |
{ | |
return this.mX; | |
} | |
HexTile.prototype.getTop = function() | |
{ | |
return this.mY; | |
} | |
HexTile.prototype.getCenterX = function() | |
{ | |
return this.mX + this.RADIUS; | |
} | |
HexTile.prototype.getCenterY = function() | |
{ | |
return this.mY + this.HEIGHT / 2; | |
} | |
HexTile.prototype.getIndexJ = function() | |
{ | |
return this.mJ; | |
} | |
HexTile.prototype.getNeighbor = function(neighborId_x) | |
{ | |
return this.mI + NEIGHBORS_DI[neighborId_x]; | |
} | |
HexTile.prototype.getNeighborJ = function(neighborId_x) | |
{ | |
return this.mJ + NEIGHBORS_DJ[this.mI % 2][neighborId_x]; | |
} | |
HexTile.prototype.computeCorners = function(cornersX, cornersY) | |
{ | |
for(var k = 0; k < NUM_NEIGHBORS; k++) | |
{ | |
cornersX[k] = mX + this.CORNERS_DX[k]; | |
cornersY[k] = mY + this.CORNERS_DY[k]; | |
} | |
return [cornersX, cornersY]; | |
} | |
HexTile.prototype.setCellIndex = function(i, j) | |
{ | |
this.mI = i; | |
this.mJ = j; | |
this.mX = i * this.SIDE; | |
this.mY = this.HEIGHT * (2 * j + (i % 2)) / 2; | |
} | |
HexTile.prototype.setCellByPoint = function(x, y) | |
{ | |
var ci = parseInt(Math.floor( parseFloat(x) / parseFloat(this.SIDE) )); | |
var cx = x - this.SIDE * ci; | |
var ty = y - (ci % 2) * this.HEIGHT / 2; | |
var cj = parseInt(Math.floor(parseFloat(ty) / parseFloat(this.HEIGHT))); | |
var cy = ty - this.HEIGHT * cj; | |
if(cx > Math.abs(this.RADIUS / 2 - this.RADIUS * cy / this.HEIGHT)) | |
{ | |
this.setCellIndex(ci, cj); | |
} | |
else | |
{ | |
this.setCellIndex(ci - 1, cj + (ci % 2) - ((cy < this.HEIGHT / 2) ? 1 : 0)); | |
} | |
} | |
HexMap = function(rows, columns) | |
{ | |
this.rows = rows; | |
this.columns = columns; | |
this.$_radius = 16; | |
} | |
HexMap.prototype.drawGrid = function(canvas) | |
{ | |
var posX; | |
var posY; | |
var coord; | |
var self = this; | |
for(var row = 0; row < this.rows; row++) | |
{ | |
for(var col = 0; col < this.columns; col++) | |
{ | |
posY = this.$_y_offset + row * this.$_radius; | |
posX = (row & 1) * (this.$_hexsize + this.$_half_hexsize) + this.$_x_offset + 2 * col * (this.$_hexsize + this.$_half_hexsize); | |
var tile = new HexTile(this.$_radius); | |
tile.setCellIndex(col, row); | |
this.drawHex(canvas, tile); | |
} | |
} | |
} | |
HexMap.prototype.findHex = function(position) | |
{ | |
var mX = position[0]; | |
var mY = position[1]; | |
var tile = new HexTile(this.$_radius); | |
tile.setCellByPoint(mX, mY); | |
return tile; | |
} | |
HexMap.prototype.drawHex = function(canvas, tile, options) | |
{ | |
var defaults = { | |
fillStyle: 'black', | |
strokeStyle: 'white', | |
strokWidth: 2, | |
x: tile.getCenterX() , y: tile.getCenterY(), | |
radius: tile.RADIUS, | |
sides: 6 | |
}; | |
var options = $.extend(defaults, options); | |
canvas.drawPolygon(options); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment