graph.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var Graph = function(width, height) {
  2. this.width = width;
  3. this.height = height;
  4. this.cells = [];
  5. this.removedEdges = [];
  6. var self = this;
  7. this.getCellAt = function (x, y) {
  8. if(x >= this.width || y >= this.height || x < 0 || y < 0) {
  9. return null;
  10. }
  11. if(!this.cells[x]) {
  12. return null;
  13. }
  14. return this.cells[x][y];
  15. };
  16. this.getCellDistance = function (cell1, cell2) {
  17. var xDist = Math.abs(cell1.x - cell2.x);
  18. var yDist = Math.abs(cell1.y - cell2.y);
  19. return Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));
  20. },
  21. // Returns true if there is an edge between cell1 and cell2
  22. this.areConnected = function(cell1, cell2) {
  23. if(!cell1 || !cell2) {
  24. return false;
  25. }
  26. if(Math.abs(cell1.x - cell2.x) > 1 ||
  27. Math.abs(cell1.y - cell2.y) > 1) {
  28. return false;
  29. }
  30. var removedEdge = _.detect(this.removedEdges, function(edge) {
  31. return _.include(edge, cell1) && _.include(edge, cell2);
  32. });
  33. return removedEdge == undefined;
  34. };
  35. this.cellUnvisitedNeighbors = function(cell) {
  36. return _.select(this.cellConnectedNeighbors(cell), function(c) {
  37. return !c.visited;
  38. });
  39. };
  40. // Returns all neighbors of this cell that ARE separated by an edge (maze line)
  41. this.cellConnectedNeighbors = function(cell) {
  42. return _.select(this.cellNeighbors(cell), function(c) {
  43. return self.areConnected(cell, c);
  44. });
  45. };
  46. // Returns all neighbors of this cell that are NOT separated by an edge
  47. // This means there is a maze path between both cells.
  48. this.cellDisconnectedNeighbors = function (cell) {
  49. return _.reject(this.cellNeighbors(cell), function(c) {
  50. return self.areConnected(cell, c);
  51. });
  52. }
  53. // Returns all neighbors of this cell, regardless if they are connected or not.
  54. this.cellNeighbors = function (cell) {
  55. var neighbors = [];
  56. var topCell = this.getCellAt(cell.x, cell.y - 1);
  57. var rightCell = this.getCellAt(cell.x + 1, cell.y);
  58. var bottomCell = this.getCellAt(cell.x, cell.y + 1);
  59. var leftCell = this.getCellAt(cell.x - 1, cell.y);
  60. if(cell.y > 0 && topCell) {
  61. neighbors.push(topCell);
  62. }
  63. if(cell.x < this.width && rightCell) {
  64. neighbors.push(rightCell);
  65. }
  66. if(cell.y < this.height && bottomCell) {
  67. neighbors.push(bottomCell);
  68. }
  69. if(cell.x > 0 && leftCell) {
  70. neighbors.push(leftCell);
  71. }
  72. return neighbors;
  73. }
  74. this.removeEdgeBetween = function(cell1, cell2) {
  75. this.removedEdges.push([cell1, cell2]);
  76. };
  77. for(var i = 0; i < this.width; i++) {
  78. this.cells.push([]);
  79. row = this.cells[i];
  80. for(var j = 0; j < this.height; j++) {
  81. var cell = new Cell(i, j, this);
  82. row.push(cell);
  83. }
  84. }
  85. };