Skip to content

Commit 0c68306

Browse files
committed
Merge pull request qiao#94 from tlhunter/master
Update Grid constructor to accept just a matrix argument
2 parents 24df2a9 + 00bb277 commit 0c68306

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

docs/contributor-guide/authors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ Rory O'Kane <https://github.com/roryokane><br>
1717
Stuart Lee <https://github.com/beeglebug><br>
1818
surrim <https://github.com/surrim><br>
1919
Tapio Vierros <https://github.com/tapio><br>
20+
Thomas Hunter II <https://github.com/tlhunter><br>
2021
Willem Mulder <https://github.com/willemmulder><br>
2122
Xueqiao Xu <https://github.com/qiao>

docs/user-guide/introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var matrix = [
1010
[1, 0, 0, 0, 1],
1111
[0, 0, 1, 0, 0],
1212
];
13-
var grid = new PF.Grid(5, 3, matrix);
13+
var grid = new PF.Grid(matrix);
1414
var finder = new PF.AStarFinder();
1515
//Find path from (1, 2) to (4, 2)
1616
var path = finder.findPath(1, 2, 4, 2, grid);
@@ -48,4 +48,4 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
4848
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
4949
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
5050
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
51-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

docs/user-guide/obstacles.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var walkabilityMatrix = [[0, 0, 0, 0, 0],
99
[0, 0, 0, 0, 0],
1010
[1, 1, 1, 1, 0],
1111
[0, 0, 0, 0, 0]];
12-
var grid = new PF.Grid(5, 7, matrix);
12+
var grid = new PF.Grid(matrix);
1313
```
1414

1515
The _walkabilityMatrix_ defines which cells are walkable and which have
@@ -41,4 +41,4 @@ PathFinding.js will find the following path:
4141

4242
Notice how the path moves diagonally where it can, thus making it shorter. This
4343
may not be always desirable and you may want to create a path without any
44-
diagonal movement. Read the next section to find out how to achieve that.
44+
diagonal movement. Read the next section to find out how to achieve that.

src/core/Grid.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ var DiagonalMovement = require('./DiagonalMovement');
44
/**
55
* The Grid class, which serves as the encapsulation of the layout of the nodes.
66
* @constructor
7-
* @param {number} width Number of columns of the grid.
7+
* @param {number|Array.<Array.<(number|boolean)>>} width_or_matrix Number of columns of the grid, or matrix
88
* @param {number} height Number of rows of the grid.
99
* @param {Array.<Array.<(number|boolean)>>} [matrix] - A 0-1 matrix
1010
* representing the walkable status of the nodes(0 or false for walkable).
1111
* If the matrix is not supplied, all the nodes will be walkable. */
12-
function Grid(width, height, matrix) {
12+
function Grid(width_or_matrix, height, matrix) {
13+
var width;
14+
15+
if (typeof width_or_matrix !== 'object') {
16+
width = width_or_matrix;
17+
} else {
18+
height = width_or_matrix.length;
19+
width = width_or_matrix[0].length;
20+
matrix = width_or_matrix;
21+
}
22+
1323
/**
1424
* The number of columns of the grid.
1525
* @type number

test/Grid.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('Grid', function() {
107107
[width, 0, false],
108108
[width, height, false],
109109
];
110-
110+
111111
asserts.forEach(function(v, i, a) {
112112
grid.isInside(v[0], v[1]).should.equal(v[2]);
113113
});
@@ -123,4 +123,33 @@ describe('Grid', function() {
123123
].sort(cmp))
124124
});
125125
});
126+
127+
describe('generate with matrix and no width or height', function() {
128+
var matrix, grid;
129+
130+
beforeEach(function() {
131+
matrix = [
132+
[1, 0, 0, 1],
133+
[0, 1, 0, 0],
134+
[0, 1, 0, 0],
135+
[0, 0, 0, 0],
136+
[1, 0, 0, 1],
137+
];
138+
139+
grid = new Grid(matrix);
140+
});
141+
142+
it('should have correct size', function() {
143+
var height = matrix.length;
144+
var width = matrix[0].length;
145+
146+
grid.width.should.equal(width);
147+
grid.height.should.equal(height);
148+
149+
grid.nodes.length.should.equal(height);
150+
for (var i = 0; i < height; ++i) {
151+
grid.nodes[i].length.should.equal(width);
152+
}
153+
});
154+
});
126155
});

0 commit comments

Comments
 (0)