-
Notifications
You must be signed in to change notification settings - Fork 9
/
box2d.go
91 lines (76 loc) · 2.08 KB
/
box2d.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package geos
import (
"fmt"
"math"
)
// A Box2D is a two-dimensional bounds.
type Box2D struct {
MinX float64
MinY float64
MaxX float64
MaxY float64
}
// NewBox2D returns a new bounds.
func NewBox2D(minX, minY, maxX, maxY float64) *Box2D {
return &Box2D{
MinX: minX,
MinY: minY,
MaxX: maxX,
MaxY: maxY,
}
}
// NewBox2DEmpty returns a new empty bounds.
func NewBox2DEmpty() *Box2D {
return &Box2D{
MinX: math.Inf(1),
MinY: math.Inf(1),
MaxX: math.Inf(-1),
MaxY: math.Inf(-1),
}
}
// Contains returns true if b contains other.
func (b *Box2D) Contains(other *Box2D) bool {
if b.IsEmpty() || other.IsEmpty() {
return false
}
return other.MinX >= b.MinX && other.MinY >= b.MinY && other.MaxX <= b.MaxX && other.MaxY <= b.MaxY
}
// ContainsPoint returns true if b contains the point at x, y.
func (b *Box2D) ContainsPoint(x, y float64) bool {
return b.MinX <= x && x <= b.MaxX && b.MinY <= y && y <= b.MaxY
}
// ContextGeom returns b as a Geom.
func (b *Box2D) ContextGeom(context *Context) *Geom {
return context.NewGeomFromBounds(b.MinX, b.MinY, b.MaxX, b.MaxY)
}
// Equals returns true if b equals other.
func (b *Box2D) Equals(other *Box2D) bool {
return b.MinX == other.MinX && b.MinY == other.MinY && b.MaxX == other.MaxX && b.MaxY == other.MaxY
}
// Geom returns b as a Geom.
func (b *Box2D) Geom() *Geom {
return b.ContextGeom(DefaultContext)
}
// IsEmpty returns true if b is empty.
func (b *Box2D) IsEmpty() bool {
return b.MinX > b.MaxX || b.MinY > b.MaxY
}
// Height returns the height of b.
func (b *Box2D) Height() float64 {
return b.MaxY - b.MinY
}
// Intersects returns true if b intersects other.
func (b *Box2D) Intersects(other *Box2D) bool {
return !(other.MinX > b.MaxX || other.MinY > b.MaxY || other.MaxX < b.MinX || other.MaxY < b.MinY)
}
// IsPoint returns true if b is a point.
func (b *Box2D) IsPoint() bool {
return b.MinX == b.MaxX && b.MinY == b.MaxY
}
func (b *Box2D) String() string {
return fmt.Sprintf("[%f %f %f %f]", b.MinX, b.MinY, b.MaxX, b.MaxY)
}
// Width returns the width of b.
func (b *Box2D) Width() float64 {
return b.MaxX - b.MinX
}