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 // NewBuffer makes a new buffer with the given dimensions
func NewBuffer(width, height int) *Buffer { func NewBuffer(width, height int) *Buffer {
b := make([][]rune, width) b := make([][]rune, height)
for x := range b { for y := range b {
b[x] = make([]rune, height) b[y] = make([]rune, width)
for y := range b[x] { for x := range b[y] {
b[x][y] = ' ' b[y][x] = ' '
} }
} }
@ -31,11 +31,11 @@ func NewBuffer(width, height int) *Buffer {
} }
func (b *Buffer) Set(x, y int, c rune) { 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 { 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) { 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 // Draw calls drawFunc for every rune in this buffer
func (b *Buffer) Draw(drawFunc func(x, y int, c rune)) { func (b *Buffer) Draw(drawFunc func(x, y int, c rune)) {
for x, row := range b.data { for y, col := range b.data {
for y, char := range row { for x, char := range col {
drawFunc(x, y, char) drawFunc(x, y, char)
} }
} }
@ -61,11 +61,11 @@ func (b *Buffer) Draw(drawFunc func(x, y int, c rune)) {
func (b *Buffer) String() string { func (b *Buffer) String() string {
s := new(strings.Builder) s := new(strings.Builder)
for ri, row := range b.data { for ci, col := range b.data {
for _, char := range row { for _, char := range col {
s.WriteRune(char) s.WriteRune(char)
} }
if ri != len(b.data)-1 { if ci != len(b.data)-1 {
s.WriteRune('\n') s.WriteRune('\n')
} }
} }
@ -84,10 +84,10 @@ func (b *Buffer) Sub(x, y, w, h int) *Buffer {
h = limit(h, 1, b.height-y) h = limit(h, 1, b.height-y)
// make slice references // make slice references
data := make([][]rune, w) data := make([][]rune, h)
for dx := x; dx < x+w-1; dx++ { for dy := y; dy < y+h-1; dy++ {
row := b.data[dx] col := b.data[dy]
data[dx] = row[y : y+w] data[dy] = col[x : x+w]
} }
// make buffer // make buffer

View File

@ -2,15 +2,17 @@ package buf2d
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
) )
func TestSub(t *testing.T) { func TestSub(t *testing.T) {
b := NewBuffer(10, 10) b := NewBuffer(10, 10)
s := b.Sub(1, 1, b.Width()-1, b.Height()-1) 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') s.Set(5, 5, 'b')
fmt.Println(b) fmt.Println(b)
fmt.Println(strings.Repeat("-", 20))
fmt.Println(s) fmt.Println(s)
} }