-
Notifications
You must be signed in to change notification settings - Fork 83
/
attach.go
53 lines (47 loc) · 1.39 KB
/
attach.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
package physics
// Attachable represents things that can be attached to Vectors
type Attachable interface {
Detach() Vector
Attach(Attachable, ...float64) Vector
AttachX(Attachable, float64) Vector
AttachY(Attachable, float64) Vector
Vec() Vector
}
// Vec returns a vector itself
func (v Vector) Vec() Vector {
return v
}
// Attach takes in something for this vector to attach to and a set of
// offsets. The resulting combined vector with the offsets is then returned,
// and needs to be assigned to the calling vector.
func (v Vector) Attach(a Attachable, offsets ...float64) Vector {
xOff := 0.0
yOff := 0.0
if len(offsets) > 0 {
xOff = offsets[0]
}
if len(offsets) > 1 {
yOff = offsets[1]
}
v2 := a.Vec()
v3 := Vector{v2.x, v2.y, xOff, yOff}
return v3
}
// AttachX performs an attachment that only attaches on the X axis.
func (v Vector) AttachX(a Attachable, offX float64) Vector {
v2 := a.Vec()
v3 := Vector{v2.x, v.y, offX, 0}
return v3
}
// AttachY performs an attachment that only attaches on the Y axis.
func (v Vector) AttachY(a Attachable, offY float64) Vector {
v2 := a.Vec()
v3 := Vector{v.x, v2.y, 0, offY}
return v3
}
// Detach returns a vector no longer attached to anything. The returned vector
// needs to be assigned to the caller for the caller to be replaced (vectors
// do not use pointer receivers)
func (v Vector) Detach() Vector {
return NewVector(v.X(), v.Y())
}