package point import ( "git.wtrh.nl/wouter/gopatterns/pkg/position" "git.wtrh.nl/wouter/gopatterns/pkg/vector" "github.com/tdewolff/canvas" "math" ) // ExtendPoint defines a point on the line between two other points. type ExtendPoint struct { id ID from Point to Point extend float64 name string draw bool hide bool } // NewExtendPoint returns a new ExtendPoint relative to two other points from and to. // The given offset defines where the new point is. // With offset = 0 the new point is a from, offset = 0.5 results in a point exactly in the middle. func NewExtendPoint(from, to Point, extend float64, id ID) *ExtendPoint { return &ExtendPoint{ id: id, from: from, to: to, extend: extend, name: string(id), } } // Position calculates and returns the absolute [position.Position]. func (b *ExtendPoint) Position() position.Position { return position.Position{ Vector: b.to.Vector().Add(b.extendedVector()), Rotation: b.to.Vector().AngleBetween(b.to.Vector()) - math.Pi/2, } } func (b *ExtendPoint) extendedVector() vector.Vector { return b.to.Vector().Subtract(b.from.Vector()).Unit().Multiply(b.extend) } // Vector calculates and returns the absolute [vector.Vector]. func (b *ExtendPoint) Vector() vector.Vector { return b.Position().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) } // ID returns the point ID. func (b *ExtendPoint) ID() ID { return b.id } // Name returns the name of a point. func (b *ExtendPoint) Name() string { return b.name } // Draw returns if the point should be drawn. func (b *ExtendPoint) Draw() bool { return b.draw } // SetDraw indicates that the point should be drawn. func (b *ExtendPoint) SetDraw() { b.draw = true } // UnsetDraw indicates that the point should not be drawn. func (b *ExtendPoint) UnsetDraw() { b.draw = true } // Hide returns if the point must remain hidden. func (b *ExtendPoint) Hide() bool { return b.hide } // SetHide indicates that the must be hidden. func (b *ExtendPoint) SetHide() { b.hide = true }