| @@ -3,6 +3,7 @@ package template | |||||
| import ( | import ( | ||||
| "errors" | "errors" | ||||
| "fmt" | "fmt" | ||||
| "maps" | |||||
| "math" | "math" | ||||
| "strconv" | "strconv" | ||||
| @@ -40,7 +41,9 @@ type Point struct { | |||||
| var ErrInvalidPointID = errors.New("type cannot be converted to a PointID") | var ErrInvalidPointID = errors.New("type cannot be converted to a PointID") | ||||
| func (p Points) Functions(pat *pattern.Pattern) map[string]govaluate.ExpressionFunction { | func (p Points) Functions(pat *pattern.Pattern) map[string]govaluate.ExpressionFunction { | ||||
| return map[string]govaluate.ExpressionFunction{ | |||||
| functions := p.evaluationFunctions() | |||||
| maps.Copy(functions, map[string]govaluate.ExpressionFunction{ | |||||
| "DistanceBetween": func(args ...interface{}) (interface{}, error) { | "DistanceBetween": func(args ...interface{}) (interface{}, error) { | ||||
| if len(args) != 2 { | if len(args) != 2 { | ||||
| return nil, fmt.Errorf("function DistanceBetween() requires 2 arguments: %w", | return nil, fmt.Errorf("function DistanceBetween() requires 2 arguments: %w", | ||||
| @@ -66,7 +69,35 @@ func (p Points) Functions(pat *pattern.Pattern) map[string]govaluate.ExpressionF | |||||
| return points[0].Vector().AngleBetween(points[1].Vector()), nil | return points[0].Vector().AngleBetween(points[1].Vector()), nil | ||||
| }, | }, | ||||
| } | |||||
| "YDistanceBetween": func(args ...interface{}) (interface{}, error) { | |||||
| if len(args) != 2 { | |||||
| return nil, fmt.Errorf("function DistanceBetween() requires 2 arguments: %w", | |||||
| ErrInvalidArguments) | |||||
| } | |||||
| points, err := p.getOrCreateFromArgs(pat, args...) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| return points[0].Vector().Y - points[1].Vector().Y, nil | |||||
| }, | |||||
| "XDistanceBetween": func(args ...interface{}) (interface{}, error) { | |||||
| if len(args) != 2 { | |||||
| return nil, fmt.Errorf("function DistanceBetween() requires 2 arguments: %w", | |||||
| ErrInvalidArguments) | |||||
| } | |||||
| points, err := p.getOrCreateFromArgs(pat, args...) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| return points[0].Vector().X - points[1].Vector().X, nil | |||||
| }, | |||||
| }) | |||||
| return functions | |||||
| } | } | ||||
| func (p Points) getOrCreateFromArgs(pat *pattern.Pattern, args ...interface{}) ([]point.Point, error) { | func (p Points) getOrCreateFromArgs(pat *pattern.Pattern, args ...interface{}) ([]point.Point, error) { | ||||