diff --git a/scalepoint.go b/scalepoint.go new file mode 100644 index 0000000..c8d2ad7 --- /dev/null +++ b/scalepoint.go @@ -0,0 +1,14 @@ +package imgrow + +import ( + "image" +) + +func scalePoint(scalar int, point image.Point) image.Point { + x2, y2 := scaleXY(scalar, point.X, point.Y) + + return image.Point { + X: x2, + Y: y2, + } +} diff --git a/scalepoint_test.go b/scalepoint_test.go new file mode 100644 index 0000000..126deaf --- /dev/null +++ b/scalepoint_test.go @@ -0,0 +1,114 @@ +package imgrow + +import ( + "testing" + + "image" +) + +func TestScalePoint(t *testing.T) { + + tests := []struct{ + Scalar int + Point image.Point + Expected image.Point + }{ + { + Scalar: -2, + Point: image.Point{ + X: 5, + Y: 7, + }, + Expected: image.Point{ + X: -10, + Y: -14, + }, + }, + { + Scalar: -1, + Point: image.Point{ + X: 5, + Y: 7, + }, + Expected: image.Point{ + X: -5, + Y: -7, + }, + }, + { + Scalar: 0, + Point: image.Point{ + X: 5, + Y: 7, + }, + Expected: image.Point{ + X: 0, + Y: 0, + }, + }, + { + Scalar: 1, + Point: image.Point{ + X: 5, + Y: 7, + }, + Expected: image.Point{ + X: 5, + Y: 7, + }, + }, + { + Scalar: 2, + Point: image.Point{ + X: 5, + Y: 7, + }, + Expected: image.Point{ + X: 10, + Y: 14, + }, + }, + { + Scalar: 3, + Point: image.Point{ + X: 5, + Y: 7, + }, + Expected: image.Point{ + X: 15, + Y: 21, + }, + }, + + + + + { + Scalar: 100, + Point: image.Point{ + X: -11, + Y: 13, + }, + Expected: image.Point{ + X: -1100, + Y: 1300, + }, + }, + } + + for testNumber, test := range tests { + + actual := scalePoint(test.Scalar, test.Point) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED: %v", expected) + t.Logf("ACTUAL: %v", actual) + t.Logf("SCALAR = %d", test.Scalar) + t.Logf("POINT: %v", test.Point) + continue + } + + } +} diff --git a/scalerectangle.go b/scalerectangle.go new file mode 100644 index 0000000..86066d6 --- /dev/null +++ b/scalerectangle.go @@ -0,0 +1,12 @@ +package imgrow + +import ( + "image" +) + +func scaleRectangle(scalar int, rectangle image.Rectangle) image.Rectangle { + return image.Rectangle{ + Min: scalePoint(scalar, rectangle.Min), + Max: scalePoint(scalar, rectangle.Max), + } +} diff --git a/scalerectangle_test.go b/scalerectangle_test.go new file mode 100644 index 0000000..13369b7 --- /dev/null +++ b/scalerectangle_test.go @@ -0,0 +1,190 @@ +package imgrow + +import ( + "testing" + + "image" +) + +func TestScaleRectangle(t *testing.T) { + + tests := []struct{ + Scalar int + Rectangle image.Rectangle + Expected image.Rectangle + }{ + { + Scalar: -2, + Rectangle: image.Rectangle{ + Min: image.Point{ + X: 2, + Y: 3, + }, + Max: image.Point{ + X: 5, + Y: 7, + }, + }, + Expected: image.Rectangle{ + Min: image.Point{ + X: -4, + Y: -6, + }, + Max: image.Point{ + X: -10, + Y: -14, + }, + }, + }, + { + Scalar: -1, + Rectangle: image.Rectangle{ + Min: image.Point{ + X: 0, + Y: 0, + }, + Max: image.Point{ + X: 5, + Y: 7, + }, + }, + Expected: image.Rectangle{ + Min: image.Point{ + X: 0, + Y: 0, + }, + Max: image.Point{ + X: -5, + Y: -7, + }, + }, + }, + { + Scalar: 0, + Rectangle: image.Rectangle{ + Max: image.Point{ + X: 5, + Y: 7, + }, + }, + Expected: image.Rectangle{ + Max: image.Point{ + X: 0, + Y: 0, + }, + }, + }, + { + Scalar: 1, + Rectangle: image.Rectangle{ + Min: image.Point{ + X: 0, + Y: 0, + }, + Max: image.Point{ + X: 5, + Y: 7, + }, + }, + Expected: image.Rectangle{ + Min: image.Point{ + X: 0, + Y: 0, + }, + Max: image.Point{ + X: 5, + Y: 7, + }, + }, + }, + { + Scalar: 2, + Rectangle: image.Rectangle{ + Min: image.Point{ + X: 2, + Y: 3, + }, + Max: image.Point{ + X: 5, + Y: 7, + }, + }, + Expected: image.Rectangle{ + Min: image.Point{ + X: 4, + Y: 6, + }, + Max: image.Point{ + X: 10, + Y: 14, + }, + }, + }, + { + Scalar: 3, + Rectangle: image.Rectangle{ + Min: image.Point{ + X: 1, + Y: 2, + }, + Max: image.Point{ + X: 5, + Y: 7, + }, + }, + Expected: image.Rectangle{ + Min: image.Point{ + X: 3, + Y: 6, + }, + Max: image.Point{ + X: 15, + Y: 21, + }, + }, + }, + + + + + { + Scalar: 100, + Rectangle: image.Rectangle{ + Min: image.Point{ + X: -3, + Y: 0, + }, + Max: image.Point{ + X: -11, + Y: 13, + }, + }, + Expected: image.Rectangle{ + Min: image.Point{ + X: -300, + Y: 0, + }, + Max: image.Point{ + X: -1100, + Y: 1300, + }, + }, + }, + } + + for testNumber, test := range tests { + + actual := scaleRectangle(test.Scalar, test.Rectangle) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value is not what was expected.", testNumber) + t.Logf("EXPECTED: %v", expected) + t.Logf("ACTUAL: %v", actual) + t.Logf("SCALAR = %d", test.Scalar) + t.Logf("POINT: %v", test.Rectangle) + continue + } + + } +}