-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.go
55 lines (46 loc) · 834 Bytes
/
box.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
package pak
import (
"fmt"
"math"
"strconv"
)
type Box struct {
W, H, X, Y float64
Packed, CanRotate, Rotated bool
}
func NewBox(w, h float64) *Box {
return &Box{W: w, H: h}
}
func (b *Box) Area() float64 {
return b.W * b.H
}
func (b *Box) Rotate() {
b.W, b.H = b.H, b.W
}
func (b *Box) Label() string {
return fmt.Sprintf("%.2fx%.2f at [%.2f, %.2f]", b.W, b.H, b.X, b.Y)
}
func BoxCode(bb []*Box) (code string) {
cc := map[string]int{}
min, max := 0.0, 0.0
lbl := ""
for _, b := range bb {
min = math.Min(b.W, b.H)
max = math.Max(b.W, b.H)
lbl = fmt.Sprintf("%.2fx%.2f", min, max)
k, ok := cc[lbl]
if ok {
cc[lbl] = k + 1
} else {
cc[lbl] = 1
}
}
for c, k := range cc {
if k > 1 {
code += " " + c + "x" + strconv.Itoa(k)
} else {
code += " " + c
}
}
return
}