From 060c43069079fdc0eba79443fac5ee7b94bd26b8 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Thu, 1 Oct 2020 13:37:58 +0200 Subject: [PATCH] fixed wrong axes --- buffer.go | 32 ++++++++++++++++---------------- buffer_sub_test.go | 4 +++- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/buffer.go b/buffer.go index af46f28..f5e80d8 100644 --- a/buffer.go +++ b/buffer.go @@ -14,11 +14,11 @@ type Buffer struct { // NewBuffer makes a new buffer with the given dimensions func NewBuffer(width, height int) *Buffer { - b := make([][]rune, width) - for x := range b { - b[x] = make([]rune, height) - for y := range b[x] { - b[x][y] = ' ' + b := make([][]rune, height) + for y := range b { + b[y] = make([]rune, width) + for x := range b[y] { + b[y][x] = ' ' } } @@ -31,11 +31,11 @@ func NewBuffer(width, height int) *Buffer { } func (b *Buffer) Set(x, y int, c rune) { - b.data[x][y] = c + b.data[y][x] = c } func (b *Buffer) Get(x, y int) rune { - return b.data[x][y] + return b.data[y][x] } func (b *Buffer) Size() (w, h int) { @@ -52,8 +52,8 @@ func (b *Buffer) Height() int { // Draw calls drawFunc for every rune in this buffer func (b *Buffer) Draw(drawFunc func(x, y int, c rune)) { - for x, row := range b.data { - for y, char := range row { + for y, col := range b.data { + for x, char := range col { drawFunc(x, y, char) } } @@ -61,11 +61,11 @@ func (b *Buffer) Draw(drawFunc func(x, y int, c rune)) { func (b *Buffer) String() string { s := new(strings.Builder) - for ri, row := range b.data { - for _, char := range row { + for ci, col := range b.data { + for _, char := range col { s.WriteRune(char) } - if ri != len(b.data)-1 { + if ci != len(b.data)-1 { s.WriteRune('\n') } } @@ -84,10 +84,10 @@ func (b *Buffer) Sub(x, y, w, h int) *Buffer { h = limit(h, 1, b.height-y) // make slice references - data := make([][]rune, w) - for dx := x; dx < x+w-1; dx++ { - row := b.data[dx] - data[dx] = row[y : y+w] + data := make([][]rune, h) + for dy := y; dy < y+h-1; dy++ { + col := b.data[dy] + data[dy] = col[x : x+w] } // make buffer diff --git a/buffer_sub_test.go b/buffer_sub_test.go index c5df613..df27cd1 100644 --- a/buffer_sub_test.go +++ b/buffer_sub_test.go @@ -2,15 +2,17 @@ package buf2d import ( "fmt" + "strings" "testing" ) func TestSub(t *testing.T) { b := NewBuffer(10, 10) s := b.Sub(1, 1, b.Width()-1, b.Height()-1) - b.Set(5, 5, 'a') + b.Set(5, 1, 'a') s.Set(5, 5, 'b') fmt.Println(b) + fmt.Println(strings.Repeat("-", 20)) fmt.Println(s) }