fix operation order for rectangle resize function, add tests

This commit is contained in:
Alex 2017-07-29 00:35:00 +12:00
parent 96df742331
commit 3e3a9aaa48
2 changed files with 33 additions and 2 deletions

View File

@ -236,8 +236,8 @@ func (r Rect) Resized(anchor, size Vec) Rect {
}
fraction := Vec{size.X / r.W(), size.Y / r.H()}
return Rect{
Min: anchor.Add(r.Min.Sub(anchor)).ScaledXY(fraction),
Max: anchor.Add(r.Max.Sub(anchor)).ScaledXY(fraction),
Min: anchor.Add(r.Min.Sub(anchor).ScaledXY(fraction)),
Max: anchor.Add(r.Max.Sub(anchor).ScaledXY(fraction)),
}
}

31
geometry_test.go Normal file
View File

@ -0,0 +1,31 @@
package pixel_test
import (
"testing"
"github.com/faiface/pixel"
)
func TestResizeRect(t *testing.T) {
testCases := []pixel.Rect{
pixel.R(-10, -10, 10, 10),
pixel.R(10, 10, 30, 30),
}
answers := []pixel.Rect{
pixel.R(-5, -5, 5, 5),
pixel.R(15, 15, 25, 25),
}
for i, rect := range testCases {
answer := answers[i]
// resize rectangle by 50% anchored at it's current center point
resizedRect := rect.Resized(rect.Center(), rect.Size().Scaled(0.5))
if resizedRect != answer {
t.Errorf("Rectangle resize was incorrect, got %v, want: %v.", resizedRect, answer)
}
}
}