Skip to content

Commit 99bfee7

Browse files
committed
Merge pull request #4 from possibly/master
Small patch, better README, areNeighbors()
2 parents 9e296df + 1be650a commit 99bfee7

File tree

3 files changed

+118
-11
lines changed

3 files changed

+118
-11
lines changed

README.md

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,54 @@ Example, distance = 2:
6161

6262
The function will return an array containing the values for the fields marked with an '*'. Notice that distance will change what cells count as neighbors.
6363

64+
### areNeighbours(x1, y1, x2, y2, [, distance])
65+
Returns true if the given coordinates are neighbors, false otherwise.
66+
67+
The distance between each coordinate must be within `distance` or one unit away from each other for the
68+
given coordinates to be considered neighbors.
69+
70+
For example, distance not set:
71+
72+
```
73+
0 1 2
74+
0 [A][ ][ ]
75+
1 [ ][B][ ]
76+
2 [ ][ ][ ]
77+
3 [ ][ ][ ]
78+
```
79+
80+
The first given coordinate (0,0) is marked with an `A`, the second (1,1), a `B`.
81+
A and B are neighbors.
82+
83+
If A and B were instead placed at (0,0) and (2,2) respectively, like this:
84+
85+
```
86+
0 1 2
87+
0 [A][ ][ ]
88+
1 [ ][ ][ ]
89+
2 [ ][ ][B]
90+
3 [ ][ ][ ]
91+
```
92+
93+
A and B are no longer neighbors.
94+
95+
Example, distance = 2:
96+
97+
```
98+
0 1 2
99+
0 [A][ ][ ]
100+
1 [ ][ ][ ]
101+
2 [ ][ ][B]
102+
3 [ ][ ][ ]
103+
```
104+
105+
A (0,0) and B (2,2) are neighbors.
106+
64107
### forEach(fn, [, thisArg])
65108
Executes a provided function once per array element.
66109

67110
`fn` is the function to execute for each element, taking three arguments:
68-
69-
currentValue
70-
The current element being processed in the array.
71-
index
72-
The object index, {x: x, y: y}, of the current element being processed in the 2d array.
73-
array
74-
The Fixed2DArray that forEach is being applied to.
75-
76-
thisArg
77-
Optional. Value to use as this when executing callback.
111+
* `currentValue`: The current element being processed in the array.
112+
* `index`: The object index, {x: x, y: y}, of the current element being processed in the 2d array.
113+
* `array`: The Fixed2DArray that forEach is being applied to.
114+
* `thisArg`: Optional. Value to use as this when executing callback.

lib/fixed-2d-array.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Fixed2DArray.prototype.getRow = function getRow(rowNumber){
5353
*
5454
* @param colNumber {number} number of the column where top left is (0,0).
5555
*/
56-
Fixed2DArray.prototype.getColumn = function getRow(colNumber){
56+
Fixed2DArray.prototype.getColumn = function getColumn(colNumber){
5757
try{
5858
this.validateCoords(colNumber,0);
5959
} catch(err){
@@ -175,4 +175,63 @@ Fixed2DArray.prototype.getNeighbours = function getNeighbours(x, y, distance){
175175
return returnArray;
176176
};
177177

178+
/**
179+
* Returns true if the given coordinates are neighbors, false otherwise.
180+
*
181+
* The distance between each coordinate must be within `distance` or one unit away from
182+
* each other for the given coordinates to be considered neighbors.
183+
*
184+
* For example, distance not set:
185+
*
186+
* 0 1 2
187+
* 0 [A][ ][ ]
188+
* 1 [ ][B][ ]
189+
* 2 [ ][ ][ ]
190+
* 3 [ ][ ][ ]
191+
*
192+
* The first given coordinate (0,0) is marked with an `A`, the second (1,1), a `B`.
193+
* A and B are neighbors.
194+
*
195+
* If A and B were instead placed at (0,0) and (2,2) respectively, like this:
196+
*
197+
* 0 1 2
198+
* 0 [A][ ][ ]
199+
* 1 [ ][ ][ ]
200+
* 2 [ ][ ][B]
201+
* 3 [ ][ ][ ]
202+
*
203+
* A and B are no longer neighbors.
204+
*
205+
* Example, distance = 2:
206+
*
207+
* 0 1 2
208+
* 0 [A][ ][ ]
209+
* 1 [ ][ ][ ]
210+
* 2 [ ][ ][B]
211+
* 3 [ ][ ][ ]
212+
*
213+
* A (0,0) and B (2,2) are neighbors.
214+
*
215+
* @param x1 {number} first x coordinate.
216+
* @param y1 {number} first y coordinate.
217+
* @param x2 {number} second x coordinate.
218+
* @param y2 {number} second y coordinate.
219+
* @param distance {number} Optional. Maximum distance for coordinates to be considered neighbors.
220+
*/
221+
Fixed2DArray.prototype.areNeighbours = function areNeighbours(x1, y1, x2, y2, distance){
222+
this.validateCoords(x1,y1);
223+
this.validateCoords(x2,y2);
224+
225+
if(typeof distance === 'undefined'){ distance = 1; }
226+
if(distance<=0){
227+
return false;
228+
}
229+
230+
if ( (Math.abs(x1 - x2) <= distance) && (Math.abs(y1 - y2) <= distance) ){
231+
return true;
232+
}else{
233+
return false;
234+
}
235+
};
236+
178237
module.exports = Fixed2DArray;

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,14 @@ test('get correct number of neighbours',function (t) {
6969
t.equal(fa.getNeighbours(2,2,-2).length,0);
7070
});
7171

72+
test('areNeighbors',function (t) {
73+
t.plan(6);
74+
var fa = new fixedArray(10,10);
75+
t.true(fa.areNeighbours(0,0,1,1));
76+
t.false(fa.areNeighbours(0,0,2,2));
77+
t.true(fa.areNeighbours(0,0,2,2,2));
78+
t.throws(function(){fa.areNeighbours(-1,-1,0,0);});
79+
t.throws(function(){fa.areNeighbours(0,0,-1,1);});
80+
t.equal(fa.areNeighbours(0,0,1,1,0),false);
81+
});
82+

0 commit comments

Comments
 (0)