-
Notifications
You must be signed in to change notification settings - Fork 2
/
math.go
57 lines (50 loc) · 903 Bytes
/
math.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
package dos
import "testing"
// Min returns the smaller of the two values.
func Min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
// Max returns the larger of the two values.
func Max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
// Clamp keeps the input value within a range of [min, max].
func Clamp(value, min, max int) int {
return Max(min, Min(value, max))
}
type Rect struct {
X, Y int
W, H int
}
func (r Rect) HasPoint(x, y int) bool {
return x >= r.X && y >= r.Y && x < r.X+r.W && y < r.Y+r.H
}
func TestRectHasPoint(t *testing.T) {
if !(Rect{0, 0, 1, 1}.HasPoint(0, 0)) {
t.Fail()
}
if (Rect{1, 1, 1, 1}.HasPoint(0, 0)) {
t.Fail()
}
if (Rect{1, 1, 0, 0}.HasPoint(1, 1)) {
t.Fail()
}
wide := Rect{3, 2, 15, 4}
if wide.HasPoint(5, 1) {
t.Fail()
}
if wide.HasPoint(8, 7) {
t.Fail()
}
if !wide.HasPoint(5, 3) {
t.Fail()
}
}