This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
72 lines (60 loc) · 1.56 KB
/
util.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
package rectpack
import (
"image"
)
type queuedData struct {
id int
pic *image.RGBA
}
// container for the leftover space after split
type createdSplits struct {
hasSmall, hasBig bool
count int
smaller, bigger image.Rectangle
}
// adds the given leftover spaces to this container
func splits(rects ...image.Rectangle) (s *createdSplits) {
s = &createdSplits{
count: len(rects),
hasSmall: true,
smaller: rects[0],
}
if s.count == 2 {
s.hasBig = true
s.bigger = rects[1]
}
return
}
// helper function to create rectangles
func rect(x, y, w, h int) image.Rectangle {
return image.Rect(x, y, x+w, y+h)
}
func area(r image.Rectangle) int {
return r.Dx() * r.Dy()
}
// helper to split existing space
func split(img, space image.Rectangle) (s *createdSplits, err error) {
w := space.Dx() - img.Dx()
h := space.Dy() - img.Dy()
if w < 0 || h < 0 {
return nil, ErrSplitFailed
} else if w == 0 && h == 0 {
// perfectly fit case
return &createdSplits{}, nil
} else if w > 0 && h == 0 {
r := rect(space.Min.X+img.Dx(), space.Min.Y, w, img.Dy())
return splits(r), nil
} else if w == 0 && h > 0 {
r := rect(space.Min.X, space.Min.Y+img.Dy(), img.Dx(), h)
return splits(r), nil
}
var smaller, larger image.Rectangle
if w > h {
smaller = rect(space.Min.X, space.Min.Y+img.Dy(), img.Dx(), h)
larger = rect(space.Min.X+img.Dx(), space.Min.Y, w, space.Dy())
} else {
smaller = rect(space.Min.X+img.Dx(), space.Min.Y, w, img.Dy())
larger = rect(space.Min.X, space.Min.Y+img.Dy(), space.Dx(), h)
}
return splits(smaller, larger), nil
}