| @@ -20,7 +20,7 @@ func (p *Polygon) Through() []point.Point { | |||||
| // NewPolygon returns a new [Polygon]. | // NewPolygon returns a new [Polygon]. | ||||
| func NewPolygon(points []point.Point, style Style, id util.ID) *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. | // WithStyle updates the style of the Polygon. | ||||
| @@ -2,10 +2,10 @@ package path | |||||
| import ( | import ( | ||||
| "fmt" | "fmt" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "log/slog" | "log/slog" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/point" | "git.wtrh.nl/patterns/gopatterns/pkg/point" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "github.com/tdewolff/canvas" | "github.com/tdewolff/canvas" | ||||
| splines "gitlab.com/Achilleshiel/gosplines" | splines "gitlab.com/Achilleshiel/gosplines" | ||||
| ) | ) | ||||
| @@ -3,11 +3,11 @@ package pattern | |||||
| import ( | import ( | ||||
| "fmt" | "fmt" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/dimensions" | "git.wtrh.nl/patterns/gopatterns/pkg/dimensions" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/pattern/text" | "git.wtrh.nl/patterns/gopatterns/pkg/pattern/text" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/point" | "git.wtrh.nl/patterns/gopatterns/pkg/point" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "github.com/tdewolff/canvas" | "github.com/tdewolff/canvas" | ||||
| "golang.org/x/image/font/gofont/goregular" | "golang.org/x/image/font/gofont/goregular" | ||||
| "gopkg.in/Knetic/govaluate.v3" | "gopkg.in/Knetic/govaluate.v3" | ||||
| @@ -19,7 +19,7 @@ type AbsolutePoint struct { | |||||
| // Matrix calculates and returns the [canvas.Matrix] of a point. | // Matrix calculates and returns the [canvas.Matrix] of a point. | ||||
| func (a *AbsolutePoint) Matrix() canvas.Matrix { | 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. | // ID returns the point ID. | ||||
| @@ -69,7 +69,7 @@ func (a *AbsolutePoint) SetDraw() { | |||||
| // UnsetDraw indicates that the point should not be drawn. | // UnsetDraw indicates that the point should not be drawn. | ||||
| func (a *AbsolutePoint) UnsetDraw() { | func (a *AbsolutePoint) UnsetDraw() { | ||||
| a.draw = true | |||||
| a.draw = false | |||||
| } | } | ||||
| // Hide returns if the point must remain hidden. | // Hide returns if the point must remain hidden. | ||||
| @@ -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()) | |||||
| } | |||||
| @@ -1,10 +1,10 @@ | |||||
| package point | package point | ||||
| import ( | import ( | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "math" | "math" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/position" | "git.wtrh.nl/patterns/gopatterns/pkg/position" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/vector" | "git.wtrh.nl/patterns/gopatterns/pkg/vector" | ||||
| "github.com/tdewolff/canvas" | "github.com/tdewolff/canvas" | ||||
| ) | ) | ||||
| @@ -53,8 +53,7 @@ func (b *BetweenPoint) Vector() vector.Vector { | |||||
| // Matrix calculates and returns the [canvas.Matrix] of a point. | // Matrix calculates and returns the [canvas.Matrix] of a point. | ||||
| func (b *BetweenPoint) Matrix() canvas.Matrix { | 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. | // ID returns the point ID. | ||||
| @@ -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) | |||||
| } | |||||
| @@ -1,10 +1,10 @@ | |||||
| package point | package point | ||||
| import ( | import ( | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "math" | "math" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/position" | "git.wtrh.nl/patterns/gopatterns/pkg/position" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/vector" | "git.wtrh.nl/patterns/gopatterns/pkg/vector" | ||||
| "github.com/tdewolff/canvas" | "github.com/tdewolff/canvas" | ||||
| ) | ) | ||||
| @@ -52,8 +52,9 @@ func (b *ExtendPoint) Vector() vector.Vector { | |||||
| // Matrix calculates and returns the [canvas.Matrix] of a point. | // Matrix calculates and returns the [canvas.Matrix] of a point. | ||||
| func (b *ExtendPoint) Matrix() canvas.Matrix { | 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. | // ID returns the point ID. | ||||
| @@ -53,7 +53,7 @@ func Draw(c *canvas.Canvas, point Point, face *canvas.FontFace, debug bool) { | |||||
| c.RenderPath(path, style, m) | c.RenderPath(path, style, m) | ||||
| text := canvas.NewTextLine(face, point.Name(), canvas.Bottom) | 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 { | if debug { | ||||
| yStyle := canvas.Style{ | yStyle := canvas.Style{ | ||||
| @@ -1,10 +1,10 @@ | |||||
| package point | package point | ||||
| import ( | import ( | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "math" | "math" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/position" | "git.wtrh.nl/patterns/gopatterns/pkg/position" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/vector" | "git.wtrh.nl/patterns/gopatterns/pkg/vector" | ||||
| "github.com/tdewolff/canvas" | "github.com/tdewolff/canvas" | ||||
| ) | ) | ||||
| @@ -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. | // Distance returns the distance between two positions. | ||||
| func (p Position) Distance(q Position) float64 { | func (p Position) Distance(q Position) float64 { | ||||
| return p.Vector.Distance(q.Vector) | return p.Vector.Distance(q.Vector) | ||||
| @@ -2,11 +2,11 @@ package template | |||||
| import ( | import ( | ||||
| "errors" | "errors" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/path" | "git.wtrh.nl/patterns/gopatterns/pkg/path" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/pattern" | "git.wtrh.nl/patterns/gopatterns/pkg/pattern" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/point" | "git.wtrh.nl/patterns/gopatterns/pkg/point" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| ) | ) | ||||
| var ErrLineNotFound = errors.New("required path not found") | var ErrLineNotFound = errors.New("required path not found") | ||||
| @@ -3,13 +3,13 @@ package template | |||||
| import ( | import ( | ||||
| "errors" | "errors" | ||||
| "fmt" | "fmt" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/path" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "maps" | "maps" | ||||
| "math" | "math" | ||||
| "strconv" | "strconv" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/path" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/point" | "git.wtrh.nl/patterns/gopatterns/pkg/point" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "gopkg.in/Knetic/govaluate.v3" | "gopkg.in/Knetic/govaluate.v3" | ||||
| ) | ) | ||||
| @@ -2,7 +2,6 @@ package template_test | |||||
| import ( | import ( | ||||
| _ "embed" | _ "embed" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "math" | "math" | ||||
| "testing" | "testing" | ||||
| @@ -10,6 +9,7 @@ import ( | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/position" | "git.wtrh.nl/patterns/gopatterns/pkg/position" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/position/testutil" | "git.wtrh.nl/patterns/gopatterns/pkg/position/testutil" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/template" | "git.wtrh.nl/patterns/gopatterns/pkg/template" | ||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/vector" | "git.wtrh.nl/patterns/gopatterns/pkg/vector" | ||||
| "github.com/stretchr/testify/require" | "github.com/stretchr/testify/require" | ||||
| "gopkg.in/yaml.v3" | "gopkg.in/yaml.v3" | ||||
| @@ -231,11 +231,11 @@ func TestFunctions(t *testing.T) { | |||||
| tests := map[string]struct { | tests := map[string]struct { | ||||
| result float64 | 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}, | "lineLength2": {result: 4}, | ||||
| } | } | ||||
| temp := &template.Template{} | temp := &template.Template{} | ||||
| @@ -1,9 +1,10 @@ | |||||
| package util_test | package util_test | ||||
| import ( | import ( | ||||
| "testing" | |||||
| "git.wtrh.nl/patterns/gopatterns/pkg/util" | "git.wtrh.nl/patterns/gopatterns/pkg/util" | ||||
| "github.com/stretchr/testify/require" | "github.com/stretchr/testify/require" | ||||
| "testing" | |||||
| ) | ) | ||||
| func TestID(t *testing.T) { | func TestID(t *testing.T) { | ||||
| @@ -20,7 +21,7 @@ func TestID(t *testing.T) { | |||||
| }, | }, | ||||
| "1.test": { | "1.test": { | ||||
| panel: "1", | panel: "1", | ||||
| name:"test", | |||||
| name: "test", | |||||
| }, | }, | ||||
| } | } | ||||
| for testName, tt := range tests { | for testName, tt := range tests { | ||||
| @@ -30,4 +31,4 @@ func TestID(t *testing.T) { | |||||
| require.Equal(t, tt.name, id.Name()) | require.Equal(t, tt.name, id.Name()) | ||||
| }) | }) | ||||
| } | } | ||||
| } | |||||
| } | |||||
| @@ -367,8 +367,8 @@ panels: | |||||
| 8a: | 8a: | ||||
| between: | between: | ||||
| from: 4 | from: 4 | ||||
| to: 0 | |||||
| offset: 0.25 | |||||
| to: 9a | |||||
| offset: 0.5 | |||||
| 8: | 8: | ||||
| relativeTo: 8a | relativeTo: 8a | ||||
| position: | position: | ||||
| @@ -384,9 +384,9 @@ panels: | |||||
| x: -12.5 | x: -12.5 | ||||
| 10a: | 10a: | ||||
| between: | between: | ||||
| from: 4 | |||||
| from: 9a | |||||
| to: 0 | to: 0 | ||||
| offset: 0.75 | |||||
| offset: 0.5 | |||||
| 10: | 10: | ||||
| relativeTo: 10a | relativeTo: 10a | ||||
| position: | position: | ||||