-
Notifications
You must be signed in to change notification settings - Fork 0
/
align.go
35 lines (31 loc) · 864 Bytes
/
align.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
package align
type Align int
const (
Center Align = 0
Left Align = 1
Right Align = 2
Top Align = 4
Bottom Align = 8
TopLeft Align = Top | Left
TopRight Align = Top | Right
BottomLeft Align = Bottom | Left
BottomRight Align = Bottom | Right
)
// RectInt returns top-left coordinates of the rectangle with
// size (w, h) aligned to the point (x, y).
func (a Align) RectInt(x, y, w, h int) (nx, ny int) {
return (a & (Left | Right)).LineInt(x, w), (a & (Top | Bottom)).LineInt(y, h)
}
// LineInt returns left coordinate of the line aligned to the point
// (both horizontal and vertical constants work).
func (a Align) LineInt(x, w int) int {
center := (a&Left != a&Right) || (a&Top != a&Bottom)
left := (a&Left == Left) || (a&Top == Top)
if left {
return x
}
if center {
return x - w/2
}
return x - w
}