Thus spake pgeerkens:
Okay, maybe Reversed can be left out of this chain, but that is still
384 cases to be checked on.
I do not consider this to be a large number of tests. Something I’m
working on presently in my day job will have millions of tests (which
were produced by another program I wrote).
A few hundred test cases is something that’s easily handleable with JUnit’s
parameterized tests. For testing coordinate systems, each test amounts to
one line of code.
Here’s a first cut of how I think the CoordinateSystem interface should
look:
public interface CoordinateSystem {
/** Maps a point in the plane to the coordinate containing it. */
public toCoordinate(Point p);
/** Maps a coordinate to the point in the plane which is its center. */
public toPoint(Coordinate c);
}
We probably also need a CoordinateFormatter, so that we don’t have to
store alphabetic coordinates as text:
public interface CoordinateFormatter {
public String format(Coordinate c);
}
Here’s a simple implementation which prints standard numeric coordinates:
public class ZeroPaddedNumericCoordinateFormatter {
public String format(Coordinate c) {
return String.format(“%02d%02d”, c.row, c.col);
}
}
Or, more generally we could have a “basic” configurable one, which can
handle anything that printf format strings can:
public class GeneralCoordinateFormatter {
protected final String fstr;
public GeneralCoordinateFormater(String fstr) {
this.fstr = fstr;
}
public String format(Coordinate c) {
return String.format(fstr, c.row, c.col);
}
}
I haven’t specified what Coordinate should look like yet, mainly because
I have a worry about whether we will want any three-axis coordinate systems.
(one axis crosses each pair of parallel hexsides, i.e., the axes are offset
120 degrees from each other), but I suspect that there must be some. I have
this vague memory of some space-combat game I saw at a convention once using
a three-axis system, but I can’t recall what it was anymore.
It might even be that we want Coordinate to be in a canonical form, and
have the formatter do all the translation work, so that we can eliminate
all of the variation between different types of grids in the rest of the
code. It’s not too hard to convert two-axis coordinates to three-, so
maybe Coordinate can be a two-field data object. (Or, conversely, maybe
it would be easier to use three-axis coordinates internally, though that
third coordinate would be useless for square grids…) Hmm.
(BTW: I’ve spent quite a lot of time thinking about grids. See here, e.g.:
nomic.net/~uckelman/mkhexgrid/)
–
J.