Skip to content

Commit

Permalink
feat: Add GetCoordAt()
Browse files Browse the repository at this point in the history
  • Loading branch information
minitauros committed Jul 19, 2022
1 parent 2bbec2d commit 671e560
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
5 changes: 5 additions & 0 deletions coord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
88 changes: 88 additions & 0 deletions coord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
})
}
2 changes: 1 addition & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " "))
}

Expand Down

0 comments on commit 671e560

Please sign in to comment.