adding tests, removing pointers

This commit is contained in:
Jakub Dóka 2020-11-05 19:08:11 +01:00
parent 49c306fcbe
commit 8bf0b74a9d
3 changed files with 58 additions and 7 deletions

View File

@ -1130,19 +1130,19 @@ var ZB = Constant(V(1, 0))
// relative to start and end point so: // relative to start and end point so:
// //
// pixel.B(ZV, ZV, ZV, V(1, 0)) == Bezier{ZV, ZV, V(1, 0), V(1, 0)} // pixel.B(ZV, ZV, ZV, V(1, 0)) == Bezier{ZV, ZV, V(1, 0), V(1, 0)}
func B(start, startHandle, endHandle, end Vec) *Bezier { func B(start, startHandle, endHandle, end Vec) Bezier {
return &Bezier{start, startHandle.Add(start), endHandle.Add(end), end, false} return Bezier{start, startHandle.Add(start), endHandle.Add(end), end, false}
} }
// Linear returns linear Bezier curve // Linear returns linear Bezier curve
func Linear(start, end Vec) *Bezier { func Linear(start, end Vec) Bezier {
return B(start, ZV, ZV, end) return B(start, ZV, ZV, end)
} }
// Constant returns Bezier curve that always return same point, // Constant returns Bezier curve that always return same point,
// This is usefull as placeholder, because it skips calculation // This is usefull as placeholder, because it skips calculation
func Constant(constant Vec) *Bezier { func Constant(constant Vec) Bezier {
return &Bezier{ return Bezier{
Start: constant, Start: constant,
redundant: true, redundant: true,
} }
@ -1151,7 +1151,7 @@ func Constant(constant Vec) *Bezier {
// Point returns point along the curve determinate by t (0 - 1) // Point returns point along the curve determinate by t (0 - 1)
// You can of course pass any value though its really hard to // You can of course pass any value though its really hard to
// predict what value will it return // predict what value will it return
func (b *Bezier) Point(t float64) Vec { func (b Bezier) Point(t float64) Vec {
if b.redundant || b.Start == b.End { if b.redundant || b.Start == b.End {
b.redundant = true b.redundant = true
return b.Start return b.Start

View File

@ -1599,3 +1599,54 @@ func BenchmarkRect_IsIntersect(b *testing.B) {
// do a thing // do a thing
} }
} }
type sub struct {
result pixel.Vec
t float64
}
func TestBezier(t *testing.T) {
tests := []struct {
curve pixel.Bezier
subTest []sub
name string
}{
{
pixel.Constant(pixel.V(1, 0)),
[]sub{
{pixel.V(1, 0), 0.0},
{pixel.V(1, 0), 100.0},
},
"constant",
},
{
pixel.Linear(pixel.V(1, 0), pixel.ZV),
[]sub{
{pixel.V(1, 0), 0.0},
{pixel.ZV, 1.0},
},
"lenear",
},
{
pixel.B(pixel.V(0, 1), pixel.V(1, 0), pixel.V(-1, 0), pixel.V(1, 0)),
[]sub{
{pixel.V(0, 1), 0.0},
{pixel.V(1, 0), 1.0},
{pixel.V(.5, .5), 0.5},
},
"curved",
},
}
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
for _, st := range c.subTest {
val := c.curve.Point(st.t)
if val != st.result {
t.Errorf("inputted: %v expected: %v got: %v", st.t, st.result, val)
}
}
})
}
}

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.12
require ( require (
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 // indirect github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72
github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7 github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0