Add .toCoordinates() method to grid
Calculates the 2-D array coordinates from the corresponding list index passed in. Relies on grid width to calculate coordinates. Does not check against grid size constraints.
This commit is contained in:
parent
6c3fcbe7b0
commit
5a72783d57
2 changed files with 32 additions and 2 deletions
|
@ -35,7 +35,7 @@ class Grid<E> extends DelegatingList<E> {
|
||||||
|
|
||||||
void set(int x, int y, E value) => setElement(x, y, value);
|
void set(int x, int y, E value) => setElement(x, y, value);
|
||||||
|
|
||||||
/// Calculate list index from coordinates.
|
/// Calculate list index from coordinates
|
||||||
///
|
///
|
||||||
/// Can be used to get the correct index from coordinates passed in.
|
/// Can be used to get the correct index from coordinates passed in.
|
||||||
/// Will only calculate the index, not take into consideration any grid size
|
/// Will only calculate the index, not take into consideration any grid size
|
||||||
|
@ -44,5 +44,12 @@ class Grid<E> extends DelegatingList<E> {
|
||||||
? throw RangeError("Coordinates for Grid Indexing must not be negative.")
|
? throw RangeError("Coordinates for Grid Indexing must not be negative.")
|
||||||
: y * width + x;
|
: y * width + x;
|
||||||
|
|
||||||
Point<int> toCoords(int index) => Point<int>(index % width, index ~/ width);
|
/// Calculate coordinates from list index
|
||||||
|
///
|
||||||
|
/// Calculates the 2-D array coordinates from the corresponding list index
|
||||||
|
/// passed in. Relies on grid width to calculate coordinates. Does not check
|
||||||
|
/// against grid size constraints; use [set] for that (generally recommended).
|
||||||
|
Point<int> toCoordinates(int index) => (index < 0)
|
||||||
|
? throw RangeError("Index for Grid Coordinates must not be negative")
|
||||||
|
: Point<int>(index % width, index ~/ width);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:rules_of_living/src/Grid.dart';
|
import 'package:rules_of_living/src/Grid.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
@ -115,4 +117,25 @@ void main() {
|
||||||
expect(sut.get(2, 2), "ready.");
|
expect(sut.get(2, 2), "ready.");
|
||||||
}, tags: const ["happy"]);
|
}, tags: const ["happy"]);
|
||||||
});
|
});
|
||||||
|
group("toCoords", () {
|
||||||
|
Grid sut;
|
||||||
|
setUp(() {
|
||||||
|
sut = Grid(3, 3);
|
||||||
|
});
|
||||||
|
test("throws RangeError on negative index argument", () {
|
||||||
|
expect(() => sut.toCoordinates(-1), throwsA(isRangeError));
|
||||||
|
}, tags: const ["bad"]);
|
||||||
|
test("calculates correct index for first element", () {
|
||||||
|
expect(sut.toCoordinates(0), equals(Point(0, 0)));
|
||||||
|
}, tags: const ["happy"]);
|
||||||
|
test("calculates correct index for last element", () {
|
||||||
|
expect(sut.toCoordinates(8), equals(Point(2, 2)));
|
||||||
|
}, tags: const ["happy"]);
|
||||||
|
test("calculates correct index for last element on first row", () {
|
||||||
|
expect(sut.toCoordinates(2), equals(Point(2, 0)));
|
||||||
|
}, tags: const ["happy"]);
|
||||||
|
test("calculates correct index for example element", () {
|
||||||
|
expect(sut.toCoordinates(6), equals(Point(0, 2)));
|
||||||
|
}, tags: const ["happy"]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue