-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat.go
109 lines (89 loc) · 1.92 KB
/
at.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ergotool
import (
"errors"
"strconv"
"github.com/mlilley/go-sexpr"
)
type At struct {
s *sexpr.Sexpr
x float64
y float64
r float64
}
func NewAtFromAt(fromAt *At, x float64, y float64, r float64) *At {
return &At{
s: fromAt.s,
x: x,
y: y,
r: r,
}
}
func NewAtFromSexpr(s *sexpr.Sexpr) (*At, error) {
if s.Name() != "at" {
return nil, errors.New("element is not an 'at'")
}
if len(s.Params()) != 2 && len(s.Params()) != 3 {
return nil, errors.New("element 'at' requires 2 or 3 params")
}
f1, err := s.Params()[0].AsFloat()
if err != nil {
return nil, errors.New("element 'at' requires param 1 to be a float")
}
f2, err := s.Params()[1].AsFloat()
if err != nil {
return nil, errors.New("element 'at' requires param 2 to be a float")
}
if len(s.Params()) == 2 {
str := sexpr.NewSexprStringQuoted("0", false)
sp, err := sexpr.NewSexprParam(str)
if err != nil {
return nil, err
}
s.AddParam(2, sp)
return &At{s: s, x: f1, y: f2, r: 0}, nil
}
f3, err := s.Params()[2].AsFloat()
if err != nil {
return nil, errors.New("element 'at' requires param 3 to be a float")
}
return &At{s: s, x: f1, y: f2, r: f3}, nil
}
func (a *At) Sexpr() *sexpr.Sexpr {
return a.s
}
func (a *At) X() float64 {
return a.x
}
func (a *At) Y() float64 {
return a.y
}
func (a *At) R() float64 {
return a.r
}
func (a *At) SetX(x float64) {
a.x = x
s := a.s
sp := s.Params()[0]
ss := sp.Value().(*sexpr.SexprString)
ss.SetValue(strconv.FormatFloat(x, 'f', -1, 64))
}
func (a *At) SetY(y float64) {
a.y = y
sp := a.s.Params()[1]
ss := sp.Value().(*sexpr.SexprString)
ss.SetValue(strconv.FormatFloat(y, 'f', -1, 64))
}
func (a *At) SetR(r float64) {
a.r = r
sp := a.s.Params()[2]
ss := sp.Value().(*sexpr.SexprString)
ss.SetValue(strconv.FormatFloat(r, 'f', -1, 64))
}
func (a *At) UpdateLocation(x, y, r float64) {
a.SetX(x)
a.SetY(y)
a.SetR(r)
}
func (a *At) String() string {
return a.s.String()
}