2017-07-28 07:35:00 -05:00
|
|
|
package pixel_test
|
|
|
|
|
|
|
|
import (
|
2017-07-30 23:41:26 -05:00
|
|
|
"fmt"
|
2019-02-12 07:57:03 -06:00
|
|
|
"math"
|
2019-01-28 03:00:24 -06:00
|
|
|
"reflect"
|
2017-07-28 07:35:00 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/faiface/pixel"
|
2019-01-28 03:00:24 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-07-28 07:35:00 -05:00
|
|
|
)
|
|
|
|
|
2019-01-28 03:00:24 -06:00
|
|
|
func TestRect_Resize(t *testing.T) {
|
2019-01-29 03:39:44 -06:00
|
|
|
type rectTestTransform struct {
|
|
|
|
name string
|
|
|
|
f func(pixel.Rect) pixel.Rect
|
|
|
|
}
|
|
|
|
|
2017-07-30 23:29:23 -05:00
|
|
|
// rectangles
|
2017-07-30 23:41:26 -05:00
|
|
|
squareAroundOrigin := pixel.R(-10, -10, 10, 10)
|
|
|
|
squareAround2020 := pixel.R(10, 10, 30, 30)
|
|
|
|
rectangleAroundOrigin := pixel.R(-20, -10, 20, 10)
|
|
|
|
rectangleAround2020 := pixel.R(0, 10, 40, 30)
|
2017-07-30 23:29:23 -05:00
|
|
|
|
|
|
|
// resize transformations
|
|
|
|
resizeByHalfAroundCenter := rectTestTransform{"by half around center", func(rect pixel.Rect) pixel.Rect {
|
|
|
|
return rect.Resized(rect.Center(), rect.Size().Scaled(0.5))
|
|
|
|
}}
|
|
|
|
resizeByHalfAroundMin := rectTestTransform{"by half around Min", func(rect pixel.Rect) pixel.Rect {
|
|
|
|
return rect.Resized(rect.Min, rect.Size().Scaled(0.5))
|
|
|
|
}}
|
|
|
|
resizeByHalfAroundMax := rectTestTransform{"by half around Max", func(rect pixel.Rect) pixel.Rect {
|
|
|
|
return rect.Resized(rect.Max, rect.Size().Scaled(0.5))
|
|
|
|
}}
|
|
|
|
resizeByHalfAroundMiddleOfLeftSide := rectTestTransform{"by half around middle of left side", func(rect pixel.Rect) pixel.Rect {
|
|
|
|
return rect.Resized(pixel.V(rect.Min.X, rect.Center().Y), rect.Size().Scaled(0.5))
|
|
|
|
}}
|
|
|
|
resizeByHalfAroundOrigin := rectTestTransform{"by half around the origin", func(rect pixel.Rect) pixel.Rect {
|
|
|
|
return rect.Resized(pixel.ZV, rect.Size().Scaled(0.5))
|
|
|
|
}}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2017-07-30 23:41:26 -05:00
|
|
|
input pixel.Rect
|
2017-07-30 23:29:23 -05:00
|
|
|
transform rectTestTransform
|
|
|
|
answer pixel.Rect
|
|
|
|
}{
|
|
|
|
{squareAroundOrigin, resizeByHalfAroundCenter, pixel.R(-5, -5, 5, 5)},
|
|
|
|
{squareAround2020, resizeByHalfAroundCenter, pixel.R(15, 15, 25, 25)},
|
|
|
|
{rectangleAroundOrigin, resizeByHalfAroundCenter, pixel.R(-10, -5, 10, 5)},
|
|
|
|
{rectangleAround2020, resizeByHalfAroundCenter, pixel.R(10, 15, 30, 25)},
|
|
|
|
|
|
|
|
{squareAroundOrigin, resizeByHalfAroundMin, pixel.R(-10, -10, 0, 0)},
|
|
|
|
{squareAround2020, resizeByHalfAroundMin, pixel.R(10, 10, 20, 20)},
|
|
|
|
{rectangleAroundOrigin, resizeByHalfAroundMin, pixel.R(-20, -10, 0, 0)},
|
|
|
|
{rectangleAround2020, resizeByHalfAroundMin, pixel.R(0, 10, 20, 20)},
|
|
|
|
|
|
|
|
{squareAroundOrigin, resizeByHalfAroundMax, pixel.R(0, 0, 10, 10)},
|
|
|
|
{squareAround2020, resizeByHalfAroundMax, pixel.R(20, 20, 30, 30)},
|
|
|
|
{rectangleAroundOrigin, resizeByHalfAroundMax, pixel.R(0, 0, 20, 10)},
|
|
|
|
{rectangleAround2020, resizeByHalfAroundMax, pixel.R(20, 20, 40, 30)},
|
|
|
|
|
|
|
|
{squareAroundOrigin, resizeByHalfAroundMiddleOfLeftSide, pixel.R(-10, -5, 0, 5)},
|
|
|
|
{squareAround2020, resizeByHalfAroundMiddleOfLeftSide, pixel.R(10, 15, 20, 25)},
|
|
|
|
{rectangleAroundOrigin, resizeByHalfAroundMiddleOfLeftSide, pixel.R(-20, -5, 0, 5)},
|
|
|
|
{rectangleAround2020, resizeByHalfAroundMiddleOfLeftSide, pixel.R(0, 15, 20, 25)},
|
|
|
|
|
|
|
|
{squareAroundOrigin, resizeByHalfAroundOrigin, pixel.R(-5, -5, 5, 5)},
|
|
|
|
{squareAround2020, resizeByHalfAroundOrigin, pixel.R(5, 5, 15, 15)},
|
|
|
|
{rectangleAroundOrigin, resizeByHalfAroundOrigin, pixel.R(-10, -5, 10, 5)},
|
|
|
|
{rectangleAround2020, resizeByHalfAroundOrigin, pixel.R(0, 5, 20, 15)},
|
2017-07-28 08:12:07 -05:00
|
|
|
}
|
|
|
|
|
2017-07-30 23:29:23 -05:00
|
|
|
for _, testCase := range testCases {
|
2017-07-30 23:41:26 -05:00
|
|
|
t.Run(fmt.Sprintf("Resize %v %s", testCase.input, testCase.transform.name), func(t *testing.T) {
|
|
|
|
testResult := testCase.transform.f(testCase.input)
|
|
|
|
if testResult != testCase.answer {
|
|
|
|
t.Errorf("Got: %v, wanted: %v\n", testResult, testCase.answer)
|
|
|
|
}
|
|
|
|
})
|
2017-07-28 08:12:07 -05:00
|
|
|
}
|
2017-07-28 07:35:00 -05:00
|
|
|
}
|
2019-02-12 07:57:03 -06:00
|
|
|
|
|
|
|
func TestMatrix_Unproject(t *testing.T) {
|
2019-02-13 07:39:47 -06:00
|
|
|
const delta = 1e-15
|
2019-02-12 07:57:03 -06:00
|
|
|
t.Run("for rotated matrix", func(t *testing.T) {
|
2019-02-12 11:53:38 -06:00
|
|
|
matrix := pixel.IM.
|
|
|
|
Rotated(pixel.ZV, math.Pi/2)
|
2019-02-12 07:57:03 -06:00
|
|
|
unprojected := matrix.Unproject(pixel.V(0, 1))
|
2019-02-12 08:24:17 -06:00
|
|
|
assert.InDelta(t, unprojected.X, 1, delta)
|
|
|
|
assert.InDelta(t, unprojected.Y, 0, delta)
|
2019-02-12 07:57:03 -06:00
|
|
|
})
|
|
|
|
t.Run("for moved matrix", func(t *testing.T) {
|
2019-02-12 11:53:38 -06:00
|
|
|
matrix := pixel.IM.
|
2019-02-13 07:39:47 -06:00
|
|
|
Moved(pixel.V(1, 2))
|
|
|
|
unprojected := matrix.Unproject(pixel.V(2, 5))
|
|
|
|
assert.InDelta(t, unprojected.X, 1, delta)
|
|
|
|
assert.InDelta(t, unprojected.Y, 3, delta)
|
2019-02-12 07:57:03 -06:00
|
|
|
})
|
|
|
|
t.Run("for scaled matrix", func(t *testing.T) {
|
2019-02-12 11:53:38 -06:00
|
|
|
matrix := pixel.IM.
|
|
|
|
Scaled(pixel.ZV, 2)
|
2019-02-13 07:39:47 -06:00
|
|
|
unprojected := matrix.Unproject(pixel.V(2, 4))
|
|
|
|
assert.InDelta(t, unprojected.X, 1, delta)
|
2019-02-12 08:24:17 -06:00
|
|
|
assert.InDelta(t, unprojected.Y, 2, delta)
|
2019-02-12 07:57:03 -06:00
|
|
|
})
|
2019-02-12 11:40:15 -06:00
|
|
|
t.Run("for scaled, rotated and moved matrix", func(t *testing.T) {
|
2019-02-12 11:53:38 -06:00
|
|
|
matrix := pixel.IM.
|
|
|
|
Scaled(pixel.ZV, 2).
|
|
|
|
Rotated(pixel.ZV, math.Pi/2).
|
|
|
|
Moved(pixel.V(2, 2))
|
2019-02-12 11:40:15 -06:00
|
|
|
unprojected := matrix.Unproject(pixel.V(-2, 6))
|
|
|
|
assert.InDelta(t, unprojected.X, 2, delta)
|
|
|
|
assert.InDelta(t, unprojected.Y, 2, delta)
|
|
|
|
})
|
2019-02-12 09:26:19 -06:00
|
|
|
t.Run("for rotated and moved matrix", func(t *testing.T) {
|
2019-02-12 11:53:38 -06:00
|
|
|
matrix := pixel.IM.
|
|
|
|
Rotated(pixel.ZV, math.Pi/2).
|
|
|
|
Moved(pixel.V(1, 1))
|
2019-02-12 09:26:19 -06:00
|
|
|
unprojected := matrix.Unproject(pixel.V(1, 2))
|
|
|
|
assert.InDelta(t, unprojected.X, 1, delta)
|
|
|
|
assert.InDelta(t, unprojected.Y, 0, delta)
|
|
|
|
})
|
2019-02-13 07:39:47 -06:00
|
|
|
t.Run("for projected vertices using all kinds of matrices", func(t *testing.T) {
|
2019-02-13 08:25:12 -06:00
|
|
|
namedMatrices := map[string]pixel.Matrix{
|
|
|
|
"IM": pixel.IM,
|
|
|
|
"Scaled": pixel.IM.Scaled(pixel.ZV, 0.5),
|
|
|
|
"Scaled x 2": pixel.IM.Scaled(pixel.ZV, 2),
|
|
|
|
"Rotated": pixel.IM.Rotated(pixel.ZV, math.Pi/4),
|
|
|
|
"Moved": pixel.IM.Moved(pixel.V(0.5, 1)),
|
|
|
|
"Moved 2": pixel.IM.Moved(pixel.V(-1, -0.5)),
|
|
|
|
"Scaled and Rotated": pixel.IM.Scaled(pixel.ZV, 0.5).Rotated(pixel.ZV, math.Pi/4),
|
|
|
|
"Scaled, Rotated and Moved": pixel.IM.Scaled(pixel.ZV, 0.5).Rotated(pixel.ZV, math.Pi/4).Moved(pixel.V(1, 2)),
|
|
|
|
"Rotated and Moved": pixel.IM.Rotated(pixel.ZV, math.Pi/4).Moved(pixel.V(1, 2)),
|
2019-02-13 07:39:47 -06:00
|
|
|
}
|
|
|
|
vertices := [...]pixel.Vec{
|
|
|
|
pixel.V(0, 0),
|
|
|
|
pixel.V(5, 0),
|
|
|
|
pixel.V(5, 10),
|
|
|
|
pixel.V(0, 10),
|
|
|
|
pixel.V(-5, 10),
|
|
|
|
pixel.V(-5, 0),
|
|
|
|
pixel.V(-5, -10),
|
|
|
|
pixel.V(0, -10),
|
|
|
|
pixel.V(5, -10),
|
|
|
|
}
|
2019-02-13 08:25:12 -06:00
|
|
|
for matrixName, matrix := range namedMatrices {
|
2019-02-13 07:39:47 -06:00
|
|
|
for _, vertex := range vertices {
|
2019-02-13 08:25:12 -06:00
|
|
|
testCase := fmt.Sprintf("for matrix %s and vertex %v", matrixName, vertex)
|
2019-02-13 07:39:47 -06:00
|
|
|
t.Run(testCase, func(t *testing.T) {
|
|
|
|
projected := matrix.Project(vertex)
|
|
|
|
unprojected := matrix.Unproject(projected)
|
|
|
|
assert.InDelta(t, vertex.X, unprojected.X, delta)
|
|
|
|
assert.InDelta(t, vertex.Y, unprojected.Y, delta)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-02-12 07:57:03 -06:00
|
|
|
t.Run("for singular matrix", func(t *testing.T) {
|
|
|
|
matrix := pixel.Matrix{0, 0, 0, 0, 0, 0}
|
|
|
|
unprojected := matrix.Unproject(pixel.ZV)
|
|
|
|
assert.True(t, math.IsNaN(unprojected.X))
|
|
|
|
assert.True(t, math.IsNaN(unprojected.Y))
|
|
|
|
})
|
|
|
|
}
|
2019-01-28 03:00:24 -06:00
|
|
|
|
|
|
|
func TestC(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want pixel.Circle
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "C(): positive radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
args: args{radius: 10, center: pixel.ZV},
|
|
|
|
want: pixel.Circle{Radius: 10, Center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "C(): zero radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
args: args{radius: 0, center: pixel.ZV},
|
|
|
|
want: pixel.Circle{Radius: 0, Center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "C(): negative radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
args: args{radius: -5, center: pixel.ZV},
|
|
|
|
want: pixel.Circle{Radius: -5, Center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := pixel.C(tt.args.radius, tt.args.center); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("C() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCircle_String(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Circle.String(): positive radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
want: "Circle(10.00, Vec(0, 0))",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.String(): zero radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 0, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
want: "Circle(0.00, Vec(0, 0))",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.String(): negative radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: -5, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
want: "Circle(-5.00, Vec(0, 0))",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.String(): irrational radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: math.Pi, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
want: "Circle(3.14, Vec(0, 0))",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := pixel.C(tt.fields.radius, tt.fields.center)
|
|
|
|
if got := c.String(); got != tt.want {
|
|
|
|
t.Errorf("Circle.String() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCircle_Norm(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want pixel.Circle
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Circle.Norm(): positive radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
|
|
|
want: pixel.C(10, pixel.ZV),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Norm(): zero radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 0, center: pixel.ZV},
|
|
|
|
want: pixel.C(0, pixel.ZV),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Norm(): negative radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: -5, center: pixel.ZV},
|
|
|
|
want: pixel.C(5, pixel.ZV),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := pixel.C(tt.fields.radius, tt.fields.center)
|
|
|
|
if got := c.Norm(); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Circle.Norm() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCircle_Area(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want float64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Circle.Area(): positive radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
want: 20 * math.Pi,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Area(): zero radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 0, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
want: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Area(): negative radius",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: -5, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
want: -10 * math.Pi,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := pixel.C(tt.fields.radius, tt.fields.center)
|
|
|
|
if got := c.Area(); got != tt.want {
|
|
|
|
t.Errorf("Circle.Area() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCircle_Moved(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
delta pixel.Vec
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want pixel.Circle
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Circle.Moved(): positive movement",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
args: args{delta: pixel.V(10, 20)},
|
2019-01-29 06:18:35 -06:00
|
|
|
want: pixel.C(10, pixel.V(10, 20)),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Moved(): zero movement",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
args: args{delta: pixel.ZV},
|
2019-01-29 06:18:35 -06:00
|
|
|
want: pixel.C(10, pixel.V(0, 0)),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Moved(): negative movement",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
args: args{delta: pixel.V(-5, -10)},
|
2019-01-29 06:18:35 -06:00
|
|
|
want: pixel.C(10, pixel.V(-5, -10)),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := pixel.C(tt.fields.radius, tt.fields.center)
|
|
|
|
if got := c.Moved(tt.args.delta); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Circle.Moved() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCircle_Resized(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
radiusDelta float64
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want pixel.Circle
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Circle.Resized(): positive delta",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
args: args{radiusDelta: 5},
|
2019-01-29 06:18:35 -06:00
|
|
|
want: pixel.C(15, pixel.V(0, 0)),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Resized(): zero delta",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
args: args{radiusDelta: 0},
|
2019-01-29 06:18:35 -06:00
|
|
|
want: pixel.C(10, pixel.V(0, 0)),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Resized(): negative delta",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
args: args{radiusDelta: -5},
|
2019-01-29 06:18:35 -06:00
|
|
|
want: pixel.C(5, pixel.V(0, 0)),
|
2019-01-28 03:00:24 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := pixel.C(tt.fields.radius, tt.fields.center)
|
|
|
|
if got := c.Resized(tt.args.radiusDelta); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Circle.Resized() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCircle_Contains(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
u pixel.Vec
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Circle.Contains(): point on cicles' center",
|
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
|
|
|
args: args{u: pixel.ZV},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Contains(): point offcenter",
|
|
|
|
fields: fields{radius: 10, center: pixel.V(5, 0)},
|
|
|
|
args: args{u: pixel.ZV},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Contains(): point on circumference",
|
|
|
|
fields: fields{radius: 10, center: pixel.V(10, 0)},
|
|
|
|
args: args{u: pixel.ZV},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Contains(): point outside circle",
|
|
|
|
fields: fields{radius: 10, center: pixel.V(15, 0)},
|
|
|
|
args: args{u: pixel.ZV},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := pixel.C(tt.fields.radius, tt.fields.center)
|
|
|
|
if got := c.Contains(tt.args.u); got != tt.want {
|
|
|
|
t.Errorf("Circle.Contains() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCircle_Union(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
d pixel.Circle
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want pixel.Circle
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Circle.Union(): overlapping circles",
|
|
|
|
fields: fields{radius: 5, center: pixel.ZV},
|
|
|
|
args: args{d: pixel.C(5, pixel.ZV)},
|
|
|
|
want: pixel.C(5, pixel.ZV),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Union(): separate circles",
|
|
|
|
fields: fields{radius: 1, center: pixel.ZV},
|
|
|
|
args: args{d: pixel.C(1, pixel.V(0, 2))},
|
|
|
|
want: pixel.C(2, pixel.V(0, 1)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := pixel.C(tt.fields.radius, tt.fields.center)
|
|
|
|
if got := c.Union(tt.args.d); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Circle.Union() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCircle_Intersect(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
radius float64
|
|
|
|
center pixel.Vec
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
d pixel.Circle
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want pixel.Circle
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Circle.Intersect(): intersecting circles",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 1, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
args: args{d: pixel.C(1, pixel.V(1, 0))},
|
|
|
|
want: pixel.C(1, pixel.V(0.5, 0)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Intersect(): non-intersecting circles",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 1, center: pixel.ZV},
|
2019-01-28 03:00:24 -06:00
|
|
|
args: args{d: pixel.C(1, pixel.V(3, 3))},
|
|
|
|
want: pixel.C(0, pixel.V(1.5, 1.5)),
|
|
|
|
},
|
2019-01-29 03:33:20 -06:00
|
|
|
{
|
|
|
|
name: "Circle.Intersect(): first circle encompassing second",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 10, center: pixel.ZV},
|
2019-01-29 03:33:20 -06:00
|
|
|
args: args{d: pixel.C(1, pixel.V(3, 3))},
|
2019-01-29 06:18:35 -06:00
|
|
|
want: pixel.C(10, pixel.ZV),
|
2019-01-29 03:33:20 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Intersect(): second circle encompassing first",
|
|
|
|
fields: fields{radius: 1, center: pixel.V(-1, -4)},
|
2019-01-29 06:18:35 -06:00
|
|
|
args: args{d: pixel.C(10, pixel.ZV)},
|
|
|
|
want: pixel.C(10, pixel.ZV),
|
2019-01-29 03:33:20 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Circle.Intersect(): matching circles",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{radius: 1, center: pixel.ZV},
|
|
|
|
args: args{d: pixel.C(1, pixel.ZV)},
|
|
|
|
want: pixel.C(1, pixel.ZV),
|
2019-01-29 03:33:20 -06:00
|
|
|
},
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := pixel.C(
|
|
|
|
tt.fields.radius,
|
|
|
|
tt.fields.center,
|
|
|
|
)
|
|
|
|
if got := c.Intersect(tt.args.d); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Circle.Intersect() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 05:20:21 -06:00
|
|
|
|
|
|
|
func TestRect_IntersectsCircle(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
Min pixel.Vec
|
|
|
|
Max pixel.Vec
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
c pixel.Circle
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Rect.IntersectsCircle(): no overlap",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
|
2019-01-29 05:20:21 -06:00
|
|
|
args: args{c: pixel.C(1, pixel.V(50, 50))},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Rect.IntersectsCircle(): circle contains rect",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
|
2019-01-29 05:20:21 -06:00
|
|
|
args: args{c: pixel.C(10, pixel.V(5, 5))},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Rect.IntersectsCircle(): rect contains circle",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
|
2019-01-29 05:20:21 -06:00
|
|
|
args: args{c: pixel.C(1, pixel.V(5, 5))},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Rect.IntersectsCircle(): circle overlaps one corner",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
|
|
|
|
args: args{c: pixel.C(1, pixel.ZV)},
|
2019-01-29 05:20:21 -06:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2019-01-29 05:41:10 -06:00
|
|
|
name: "Rect.IntersectsCircle(): circle overlaps two corners",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
|
2019-01-29 05:41:10 -06:00
|
|
|
args: args{c: pixel.C(6, pixel.V(0, 5))},
|
2019-01-29 05:20:21 -06:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Rect.IntersectsCircle(): circle overlaps one edge",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
|
2019-01-29 05:20:21 -06:00
|
|
|
args: args{c: pixel.C(1, pixel.V(0, 5))},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Rect.IntersectsCircle(): edge is tangent",
|
2019-01-29 06:18:35 -06:00
|
|
|
fields: fields{Min: pixel.ZV, Max: pixel.V(10, 10)},
|
2019-01-29 05:20:21 -06:00
|
|
|
args: args{c: pixel.C(1, pixel.V(-1, 5))},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := pixel.Rect{
|
|
|
|
Min: tt.fields.Min,
|
|
|
|
Max: tt.fields.Max,
|
|
|
|
}
|
|
|
|
if got := r.IntersectsCircle(tt.args.c); got != tt.want {
|
|
|
|
t.Errorf("Rect.IntersectsCircle() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|