diff --git a/pkg/path/path.go b/pkg/path/path.go index e1be8c3..2a96310 100644 --- a/pkg/path/path.go +++ b/pkg/path/path.go @@ -20,7 +20,7 @@ func (p *Polygon) Through() []point.Point { // NewPolygon returns a new [Polygon]. func NewPolygon(points []point.Point, style Style, id util.ID) *Polygon { - return &Polygon{points: points, style: style} + return &Polygon{points: points, style: style, id: id} } // WithStyle updates the style of the Polygon. diff --git a/pkg/path/splines.go b/pkg/path/splines.go index aa80281..6c5445b 100644 --- a/pkg/path/splines.go +++ b/pkg/path/splines.go @@ -2,10 +2,10 @@ package path import ( "fmt" - "git.wtrh.nl/patterns/gopatterns/pkg/util" "log/slog" "git.wtrh.nl/patterns/gopatterns/pkg/point" + "git.wtrh.nl/patterns/gopatterns/pkg/util" "github.com/tdewolff/canvas" splines "gitlab.com/Achilleshiel/gosplines" ) diff --git a/pkg/pattern/pattern.go b/pkg/pattern/pattern.go index b3ee4d3..d11f211 100644 --- a/pkg/pattern/pattern.go +++ b/pkg/pattern/pattern.go @@ -3,11 +3,11 @@ package pattern import ( "fmt" - "git.wtrh.nl/patterns/gopatterns/pkg/util" "git.wtrh.nl/patterns/gopatterns/pkg/dimensions" "git.wtrh.nl/patterns/gopatterns/pkg/pattern/text" "git.wtrh.nl/patterns/gopatterns/pkg/point" + "git.wtrh.nl/patterns/gopatterns/pkg/util" "github.com/tdewolff/canvas" "golang.org/x/image/font/gofont/goregular" "gopkg.in/Knetic/govaluate.v3" diff --git a/pkg/point/absolute_point.go b/pkg/point/absolute_point.go index 6b4e912..998621b 100644 --- a/pkg/point/absolute_point.go +++ b/pkg/point/absolute_point.go @@ -19,7 +19,7 @@ type AbsolutePoint struct { // Matrix calculates and returns the [canvas.Matrix] of a point. func (a *AbsolutePoint) Matrix() canvas.Matrix { - return canvas.Identity.Translate(a.position.Vector.Values()).Rotate(a.position.Rotation) + return canvas.Identity.Translate(a.position.Vector.Values()).Rotate(a.position.RotationD()) } // ID returns the point ID. @@ -69,7 +69,7 @@ func (a *AbsolutePoint) SetDraw() { // UnsetDraw indicates that the point should not be drawn. func (a *AbsolutePoint) UnsetDraw() { - a.draw = true + a.draw = false } // Hide returns if the point must remain hidden. diff --git a/pkg/point/absolute_point_test.go b/pkg/point/absolute_point_test.go new file mode 100644 index 0000000..052d631 --- /dev/null +++ b/pkg/point/absolute_point_test.go @@ -0,0 +1,71 @@ +package point_test + +import ( + "math" + "testing" + + "git.wtrh.nl/patterns/gopatterns/pkg/point" + "git.wtrh.nl/patterns/gopatterns/pkg/position" + "git.wtrh.nl/patterns/gopatterns/pkg/util" + "git.wtrh.nl/patterns/gopatterns/pkg/vector" + "github.com/stretchr/testify/require" + "github.com/tdewolff/canvas" +) + +func EqualMatrix(t *testing.T, expected, actual canvas.Matrix, delta float64) { + t.Helper() + + require.InDeltaSlice(t, expected[0][:], actual[0][:], delta) + require.InDeltaSlice(t, expected[1][:], actual[1][:], delta) +} + +func TestAbsolutePoint_Matrix(t *testing.T) { + rotation := math.Pi/2 + 2 + newPoint := point.NewAbsolutePoint(1, 2, rotation, "1") + actual := newPoint.Matrix() + + expected := canvas.Matrix{ + {math.Cos(rotation), -math.Sin(rotation), 1.0}, + {math.Sin(rotation), math.Cos(rotation), 2.0}, + } + + EqualMatrix(t, actual, expected, 1e-10) +} + +func TestAbsolutePoint_ID(t *testing.T) { + newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value") + require.Equal(t, util.ID("this is a test value"), newPoint.ID()) +} + +func TestAbsolutePoint_Name(t *testing.T) { + require.Equal(t, "this is a test value", point.NewAbsolutePoint(1, 2, 3, "this is a test value").Name()) +} + +func TestAbsolutePoint_Position(t *testing.T) { + actual := point.NewAbsolutePoint(1, 2, 3, "this is a test value").Position() + expected := position.Position{ + Vector: vector.Vector{X: 1, Y: 2}, + Rotation: 3, + } + + require.Equal(t, expected, actual) +} + +func TestAbsolutePoint_Draw(t *testing.T) { + newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value") + require.False(t, newPoint.Draw()) + + newPoint.SetDraw() + require.True(t, newPoint.Draw()) + + newPoint.UnsetDraw() + require.False(t, newPoint.Draw()) +} + +func TestAbsolutePoint_Hide(t *testing.T) { + newPoint := point.NewAbsolutePoint(1, 2, 3, "this is a test value") + require.False(t, newPoint.Hide()) + + newPoint.SetHide() + require.True(t, newPoint.Hide()) +} diff --git a/pkg/point/between_point.go b/pkg/point/between_point.go index 33168aa..f585dab 100644 --- a/pkg/point/between_point.go +++ b/pkg/point/between_point.go @@ -1,10 +1,10 @@ package point import ( - "git.wtrh.nl/patterns/gopatterns/pkg/util" "math" "git.wtrh.nl/patterns/gopatterns/pkg/position" + "git.wtrh.nl/patterns/gopatterns/pkg/util" "git.wtrh.nl/patterns/gopatterns/pkg/vector" "github.com/tdewolff/canvas" ) @@ -53,8 +53,7 @@ func (b *BetweenPoint) Vector() vector.Vector { // Matrix calculates and returns the [canvas.Matrix] of a point. func (b *BetweenPoint) Matrix() canvas.Matrix { - return b.p.Matrix().Translate(b.inBetween().Values()). - Rotate((b.p.Vector().AngleBetween(b.q.Vector()) - math.Pi/2) * 180 / math.Pi) + return canvas.Identity.Translate(b.Vector().Values()).Rotate(b.Position().RotationD()) } // ID returns the point ID. diff --git a/pkg/point/between_point_test.go b/pkg/point/between_point_test.go new file mode 100644 index 0000000..c12292d --- /dev/null +++ b/pkg/point/between_point_test.go @@ -0,0 +1,48 @@ +package point_test + +import ( + "math" + "testing" + + "git.wtrh.nl/patterns/gopatterns/pkg/point" + "git.wtrh.nl/patterns/gopatterns/pkg/position" + "git.wtrh.nl/patterns/gopatterns/pkg/position/testutil" + "git.wtrh.nl/patterns/gopatterns/pkg/vector" + "github.com/tdewolff/canvas" +) + +func TestBetweenPoint_Position(t *testing.T) { + p1 := point.NewAbsolutePoint(0, 0, 0, "1") + p2 := point.NewAbsolutePoint(1, 1, 0, "2") + p3 := point.NewBetweenPoint(p1, p2, 0.5, "3") + testutil.EqualPosition(t, position.Position{ + Vector: vector.Vector{ + X: 0.5, + Y: 0.5, + }, + Rotation: -math.Pi / 4, + }, p3.Position(), 1e-10) + + matrix := p3.Matrix() + EqualMatrix(t, canvas.Matrix{ + {math.Cos(-math.Pi / 4), -math.Sin(-math.Pi / 4), 0.5}, + {math.Sin(-math.Pi / 4), math.Cos(-math.Pi / 4), 0.5}, + }, matrix, 1e-10) + + p4 := point.NewAbsolutePoint(0, 1, 0, "4") + p5 := point.NewBetweenPoint(p3, p4, 0.5, "5") + + testutil.EqualPosition(t, position.Position{ + Vector: vector.Vector{ + X: 0.25, + Y: 0.75, + }, + Rotation: math.Pi / 4, + }, p5.Position(), 1e-10) + + matrix2 := p5.Matrix() + EqualMatrix(t, canvas.Matrix{ + {math.Cos(math.Pi / 4), -math.Sin(math.Pi / 4), 0.25}, + {math.Sin(math.Pi / 4), math.Cos(math.Pi / 4), 0.75}, + }, matrix2, 1e-10) +} diff --git a/pkg/point/extend_point.go b/pkg/point/extend_point.go index d8c1f6f..caf476d 100644 --- a/pkg/point/extend_point.go +++ b/pkg/point/extend_point.go @@ -1,10 +1,10 @@ package point import ( - "git.wtrh.nl/patterns/gopatterns/pkg/util" "math" "git.wtrh.nl/patterns/gopatterns/pkg/position" + "git.wtrh.nl/patterns/gopatterns/pkg/util" "git.wtrh.nl/patterns/gopatterns/pkg/vector" "github.com/tdewolff/canvas" ) @@ -52,8 +52,9 @@ func (b *ExtendPoint) Vector() vector.Vector { // Matrix calculates and returns the [canvas.Matrix] of a point. func (b *ExtendPoint) Matrix() canvas.Matrix { - return b.to.Matrix().Translate(b.extendedVector().Values()). - Rotate((b.from.Vector().AngleBetween(b.to.Vector()) - math.Pi/2) * 180 / math.Pi) + return canvas.Identity.Translate(b.Position().Vector.Values()).Rotate(b.Position().RotationD()) + //return b.to.Matrix().Translate(b.extendedVector().Values()). + // Rotate((b.from.Vector().AngleBetween(b.to.Vector()) - math.Pi/2) * 180 / math.Pi) } // ID returns the point ID. diff --git a/pkg/point/point.go b/pkg/point/point.go index 3cf0dee..0e03e1f 100644 --- a/pkg/point/point.go +++ b/pkg/point/point.go @@ -53,7 +53,7 @@ func Draw(c *canvas.Canvas, point Point, face *canvas.FontFace, debug bool) { c.RenderPath(path, style, m) text := canvas.NewTextLine(face, point.Name(), canvas.Bottom) - c.RenderText(text, m.Translate(2, -4)) + c.RenderText(text, m.Translate(2, -4).Rotate(-point.Position().RotationD())) if debug { yStyle := canvas.Style{ diff --git a/pkg/point/relative_point.go b/pkg/point/relative_point.go index 0dbab40..9762fa5 100644 --- a/pkg/point/relative_point.go +++ b/pkg/point/relative_point.go @@ -1,10 +1,10 @@ package point import ( - "git.wtrh.nl/patterns/gopatterns/pkg/util" "math" "git.wtrh.nl/patterns/gopatterns/pkg/position" + "git.wtrh.nl/patterns/gopatterns/pkg/util" "git.wtrh.nl/patterns/gopatterns/pkg/vector" "github.com/tdewolff/canvas" ) diff --git a/pkg/position/position.go b/pkg/position/position.go index e15dcc4..3050ed8 100644 --- a/pkg/position/position.go +++ b/pkg/position/position.go @@ -22,6 +22,11 @@ func (p Position) Add(q Position) Position { } } +// RotationD returns the rotation angle of the position in degrees. +func (p Position) RotationD() float64 { + return p.Rotation * 180 / math.Pi +} + // Distance returns the distance between two positions. func (p Position) Distance(q Position) float64 { return p.Vector.Distance(q.Vector) diff --git a/pkg/template/line.go b/pkg/template/line.go index 3058cc0..8f5d1c1 100644 --- a/pkg/template/line.go +++ b/pkg/template/line.go @@ -2,11 +2,11 @@ package template import ( "errors" - "git.wtrh.nl/patterns/gopatterns/pkg/util" "git.wtrh.nl/patterns/gopatterns/pkg/path" "git.wtrh.nl/patterns/gopatterns/pkg/pattern" "git.wtrh.nl/patterns/gopatterns/pkg/point" + "git.wtrh.nl/patterns/gopatterns/pkg/util" ) var ErrLineNotFound = errors.New("required path not found") diff --git a/pkg/template/point.go b/pkg/template/point.go index 622661d..eff169c 100644 --- a/pkg/template/point.go +++ b/pkg/template/point.go @@ -3,13 +3,13 @@ package template import ( "errors" "fmt" - "git.wtrh.nl/patterns/gopatterns/pkg/path" - "git.wtrh.nl/patterns/gopatterns/pkg/util" "maps" "math" "strconv" + "git.wtrh.nl/patterns/gopatterns/pkg/path" "git.wtrh.nl/patterns/gopatterns/pkg/point" + "git.wtrh.nl/patterns/gopatterns/pkg/util" "gopkg.in/Knetic/govaluate.v3" ) diff --git a/pkg/template/point_test.go b/pkg/template/point_test.go index 99d5943..a20a589 100644 --- a/pkg/template/point_test.go +++ b/pkg/template/point_test.go @@ -2,7 +2,6 @@ package template_test import ( _ "embed" - "git.wtrh.nl/patterns/gopatterns/pkg/util" "math" "testing" @@ -10,6 +9,7 @@ import ( "git.wtrh.nl/patterns/gopatterns/pkg/position" "git.wtrh.nl/patterns/gopatterns/pkg/position/testutil" "git.wtrh.nl/patterns/gopatterns/pkg/template" + "git.wtrh.nl/patterns/gopatterns/pkg/util" "git.wtrh.nl/patterns/gopatterns/pkg/vector" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" @@ -231,11 +231,11 @@ func TestFunctions(t *testing.T) { tests := map[string]struct { result float64 }{ - "distance": {result: 5}, - "angle": {result: math.Atan2(3, 4)}, - "yDistance": {result: 3}, - "xDistance": {result: 4}, - "lineLength": {result: 12}, + "distance": {result: 5}, + "angle": {result: math.Atan2(3, 4)}, + "yDistance": {result: 3}, + "xDistance": {result: 4}, + "lineLength": {result: 12}, "lineLength2": {result: 4}, } temp := &template.Template{} diff --git a/pkg/util/id_test.go b/pkg/util/id_test.go index dad652b..0f9dd1f 100644 --- a/pkg/util/id_test.go +++ b/pkg/util/id_test.go @@ -1,9 +1,10 @@ package util_test import ( + "testing" + "git.wtrh.nl/patterns/gopatterns/pkg/util" "github.com/stretchr/testify/require" - "testing" ) func TestID(t *testing.T) { @@ -20,7 +21,7 @@ func TestID(t *testing.T) { }, "1.test": { panel: "1", - name:"test", + name: "test", }, } for testName, tt := range tests { @@ -30,4 +31,4 @@ func TestID(t *testing.T) { require.Equal(t, tt.name, id.Name()) }) } -} \ No newline at end of file +} diff --git a/templates/tailored_shirt_block.yaml b/templates/tailored_shirt_block.yaml index 5ac9866..6276c1e 100644 --- a/templates/tailored_shirt_block.yaml +++ b/templates/tailored_shirt_block.yaml @@ -367,8 +367,8 @@ panels: 8a: between: from: 4 - to: 0 - offset: 0.25 + to: 9a + offset: 0.5 8: relativeTo: 8a position: @@ -384,9 +384,9 @@ panels: x: -12.5 10a: between: - from: 4 + from: 9a to: 0 - offset: 0.75 + offset: 0.5 10: relativeTo: 10a position: