-
Notifications
You must be signed in to change notification settings - Fork 2
/
rect.go
76 lines (60 loc) · 2.23 KB
/
rect.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
package main
import (
"image"
)
func centerPt(r image.Rectangle) image.Point {
// XXX need to add 1 to get text vertically centered; maybe baseline calc is wrong?
return r.Min.Add(r.Max).Div(2).Sub(image.Point{0, 1})
}
func box(width, height int) image.Rectangle {
return image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{width, height}}
}
func topV(src, container image.Rectangle) image.Rectangle {
delta := container.Min.Y - src.Min.Y
return src.Add(image.Point{0, delta})
}
func botV(src, container image.Rectangle) image.Rectangle {
delta := container.Max.Y - src.Max.Y
return src.Add(image.Point{0, delta})
}
func centerV(src, container image.Rectangle) image.Rectangle {
mid := (container.Min.Y + container.Max.Y) / 2
dy := src.Dy()
half := dy / 2
return image.Rect(src.Min.X, mid - half, src.Max.X, mid + (dy - half))
}
func rightH(src, container image.Rectangle) image.Rectangle {
delta := container.Max.X - src.Max.X
return src.Add(image.Point{delta, 0})
}
func leftH(src, container image.Rectangle) image.Rectangle {
delta := container.Min.X - src.Min.X
return src.Add(image.Point{delta, 0})
}
func leftRect(src image.Rectangle, width int) image.Rectangle {
return image.Rect(src.Min.X - width, src.Min.Y, src.Min.X, src.Max.Y)
}
func rightRect(src image.Rectangle, width int) image.Rectangle {
return image.Rect(src.Max.X, src.Min.Y, src.Max.X + width, src.Max.Y)
}
func aboveRect(src image.Rectangle, height int) image.Rectangle {
return image.Rect(src.Min.X, src.Min.Y - height, src.Max.X, src.Min.Y)
}
func belowRect(src image.Rectangle, height int) image.Rectangle {
return image.Rect(src.Min.X, src.Max.Y, src.Max.X, src.Max.Y + height)
}
func tickRect(r image.Rectangle, bottom bool, x, size int) image.Rectangle {
if bottom {
return image.Rect(x, r.Max.Y - size, x + 1, r.Max.Y)
}
return image.Rect(x, r.Min.Y, x + 1, r.Min.Y + size)
}
func padPt(center image.Point, w, h int) image.Rectangle {
return image.Rect(center.X - w, center.Y - h, center.X + w + 1, center.Y + h + 1)
}
func padRect(r image.Rectangle, w, h int) image.Rectangle {
return image.Rect(r.Min.X - w, r.Min.Y - h, r.Max.X + w, r.Max.Y + h)
}
func vrect(r image.Rectangle, x int) image.Rectangle {
return image.Rect(x, r.Min.Y, x + 1, r.Max.Y)
}