-
Notifications
You must be signed in to change notification settings - Fork 2
/
item.go
91 lines (76 loc) · 1.65 KB
/
item.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
package packme
import (
"bytes"
"fmt"
)
type Item struct {
desc string
length,
width,
height float32
rot Rotation
pos Point
}
func NewItem(desc string, dims Dimensions) *Item {
return &Item{
desc: desc,
length: dims.Length(),
width: dims.Width(),
height: dims.Height(),
// default rotation
rot: RotationLWH,
// default position
pos: Point{0, 0, 0},
}
}
func (i *Item) Desc() string {
return i.desc
}
func (i *Item) Length() float32 {
return i.length
}
func (i *Item) Width() float32 {
return i.width
}
func (i *Item) Height() float32 {
return i.height
}
func (i *Item) Volume() float32 {
return i.length * i.width * i.height
}
func (i *Item) Dimensions() Dimensions {
var d Dimensions
switch i.rot {
case RotationLWH:
d = Dimensions{i.Length(), i.Width(), i.Height()}
case RotationWLH:
d = Dimensions{i.Width(), i.Length(), i.Height()}
case RotationWHL:
d = Dimensions{i.Width(), i.Height(), i.Length()}
case RotationHLW:
d = Dimensions{i.Height(), i.Length(), i.Width()}
case RotationHWL:
d = Dimensions{i.Height(), i.Width(), i.Length()}
case RotationLHW:
d = Dimensions{i.Length(), i.Height(), i.Width()}
}
return d
}
func (i *Item) Collision(i2 *Item) bool {
return collision(i, i2, AxisX, AxisY) &&
collision(i, i2, AxisY, AxisZ) &&
collision(i, i2, AxisX, AxisZ)
}
func (i *Item) String() string {
return fmt.Sprintf("%s %s vol(%v) pos(%s) %s", i.desc, i.Dimensions(),
i.Volume(), i.pos.String(), i.rot.String())
}
type Items []*Item
func (items Items) String() string {
buf := &bytes.Buffer{}
for _, item := range items {
buf.WriteString(item.String())
buf.WriteRune('\n')
}
return buf.String()
}