initial commits

master
Charles Iliya Krempeaux 2024-02-09 11:08:43 -08:00
parent 09b6eb0e7f
commit 91e9ea5115
4 changed files with 330 additions and 0 deletions

14
scalepoint.go 100644
View File

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

114
scalepoint_test.go 100644
View File

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

12
scalerectangle.go 100644
View File

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

View File

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