fixed wrong axes

This commit is contained in:
Timon Ringwald 2020-10-01 13:37:58 +02:00
parent 2b37461b54
commit 060c430690
2 changed files with 19 additions and 17 deletions

View File

@ -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

View File

@ -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)
}