diff --git a/coord.go b/coord.go index 91698ae..ba9d536 100644 --- a/coord.go +++ b/coord.go @@ -86,6 +86,11 @@ func (c Coord) GetCoordsAround() Coords { } } +// GetCoordAt returns the coord at the given offset from the current coord. +func (c Coord) GetCoordAt(xOffset, yOffset int) Coord { + return Coord{c.X + xOffset, c.Y + yOffset} +} + // Coords is an array of coordinates. type Coords []Coord diff --git a/coord_test.go b/coord_test.go index 7803152..2c6b5e5 100644 --- a/coord_test.go +++ b/coord_test.go @@ -187,3 +187,91 @@ func Test_Coord_ConnectsTo(t *testing.T) { } }) } + +func Test_Coord_GetCoordAt(t *testing.T) { + base := Coord{1, 1} + type input struct { + xOffset int + yOffset int + } + testCases := []struct { + description string + input input + expected Coord + }{ + { + description: "works top", + input: input{ + xOffset: 0, + yOffset: 1, + }, + expected: Coord{1, 2}, + }, + { + description: "works top right", + input: input{ + xOffset: 1, + yOffset: 1, + }, + expected: Coord{2, 2}, + }, + { + description: "works right", + input: input{ + xOffset: 1, + yOffset: 0, + }, + expected: Coord{2, 1}, + }, + { + description: "works bot right", + input: input{ + xOffset: 1, + yOffset: -1, + }, + expected: Coord{2, 0}, + }, + { + description: "works bot", + input: input{ + xOffset: 0, + yOffset: -1, + }, + expected: Coord{1, 0}, + }, + { + description: "works bot left", + input: input{ + xOffset: -1, + yOffset: -1, + }, + expected: Coord{0, 0}, + }, + { + description: "works left", + input: input{ + xOffset: -1, + yOffset: 0, + }, + expected: Coord{0, 1}, + }, + { + description: "works top left", + input: input{ + xOffset: -1, + yOffset: 1, + }, + expected: Coord{0, 2}, + }, + } + + Convey("Coord.GetCoordAt()", t, func() { + for i, tc := range testCases { + Convey(fmt.Sprintf("%d: %s", i, tc.description), func() { + res := base.GetCoordAt(tc.input.xOffset, tc.input.yOffset) + + So(res, ShouldResemble, tc.expected) + }) + } + }) +} diff --git a/render.go b/render.go index ee45fa0..3aa186f 100644 --- a/render.go +++ b/render.go @@ -25,7 +25,7 @@ func GetRender(s *Surface) string { rowVals := make([]string, 0, len(rows)) for i, row := range rows { - row = append([]string{fmt.Sprintf("%01d | ", s.height-i-1)}, row...) + row = append([]string{fmt.Sprintf("%02d | ", s.height-i-1)}, row...) rowVals = append(rowVals, strings.Join(row, " ")) }