From 35ebc39ba6ff6bad50abbd433ceb2ba5c8df318d Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 29 Jul 2017 00:35:00 +1200 Subject: [PATCH] fix operation order for rectangle resize function, add tests --- geometry.go | 4 ++-- geometry_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 geometry_test.go diff --git a/geometry.go b/geometry.go index 99514cc..52dfa73 100644 --- a/geometry.go +++ b/geometry.go @@ -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)), } } diff --git a/geometry_test.go b/geometry_test.go new file mode 100644 index 0000000..4cd4464 --- /dev/null +++ b/geometry_test.go @@ -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) + } + } + +}