-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbounds.go
181 lines (157 loc) · 4.35 KB
/
bounds.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package geom
import (
"math"
)
// Bounds holds the spatial extent of a geometry.
type Bounds struct {
Min, Max Point
}
// Extend increases the extent of b1 to include b2.
func (b *Bounds) Extend(b2 *Bounds) {
if b2 == nil {
return
}
b.extendPoint(b2.Min)
b.extendPoint(b2.Max)
}
// NewBounds initializes a new bounds object.
func NewBounds() *Bounds {
return &Bounds{Point{X: math.Inf(1), Y: math.Inf(1)}, Point{X: math.Inf(-1), Y: math.Inf(-1)}}
}
// NewBoundsPoint creates a bounds object from a point.
func NewBoundsPoint(point Point) *Bounds {
return &Bounds{Point{X: point.X, Y: point.Y}, Point{X: point.X, Y: point.Y}}
}
// Copy returns a copy of b.
func (b *Bounds) Copy() *Bounds {
return &Bounds{Point{X: b.Min.X, Y: b.Min.Y}, Point{X: b.Max.X, Y: b.Max.Y}}
}
// Empty returns true if b does not contain any points.
func (b *Bounds) Empty() bool {
return b.Max.X < b.Min.X || b.Max.Y < b.Min.Y
}
func (b *Bounds) extendPoint(point Point) *Bounds {
b.Min.X = math.Min(b.Min.X, point.X)
b.Min.Y = math.Min(b.Min.Y, point.Y)
b.Max.X = math.Max(b.Max.X, point.X)
b.Max.Y = math.Max(b.Max.Y, point.Y)
return b
}
func (b *Bounds) extendPoints(points []Point) {
for _, point := range points {
b.extendPoint(point)
}
}
func (b *Bounds) extendPointss(pointss []Path) {
for _, points := range pointss {
b.extendPoints(points)
}
}
// Overlaps returns whether b and b2 overlap.
func (b *Bounds) Overlaps(b2 *Bounds) bool {
return b.Min.X <= b2.Max.X && b.Min.Y <= b2.Max.Y && b.Max.X >= b2.Min.X && b.Max.Y >= b2.Min.Y
}
// Bounds returns b
func (b *Bounds) Bounds() *Bounds {
return b
}
// Within calculates whether b is within poly.
func (b *Bounds) Within(poly Polygonal) WithinStatus {
if bp, ok := poly.(*Bounds); ok {
if b.Min.Equals(bp.Min) && b.Max.Equals(bp.Max) {
return OnEdge
} else if b.Min.X >= bp.Min.X && b.Min.Y >= bp.Min.Y && b.Max.X <= bp.Max.X && b.Max.Y <= bp.Max.Y {
return Inside
}
return Outside
}
minIn := pointInPolygonal(b.Min, poly)
maxIn := pointInPolygonal(b.Max, poly)
if minIn == Outside || maxIn == Outside {
return Outside
}
return Inside
}
// Len returns the number of points in the receiver (always==5).
func (b *Bounds) Len() int { return 4 }
// Points returns an iterator for the corners of the receiver.
func (b *Bounds) Points() func() Point {
var i int
return func() Point {
defer func() {
i++
}()
switch i {
case 0:
return b.Min
case 1:
return Point{b.Max.X, b.Min.Y}
case 2:
return b.Max
case 3:
return Point{b.Min.X, b.Max.Y}
default:
panic("out of bounds")
}
}
}
// Polygons returns a rectangle polygon
// to fulfill the Polygonal interface.
func (b *Bounds) Polygons() []Polygon {
return []Polygon{{{b.Min, Point{b.Max.X, b.Min.Y}, b.Max, Point{b.Min.X, b.Max.Y}}}}
}
// Intersection returns the Intersection of the receiver with p.
func (b *Bounds) Intersection(p Polygonal) Polygonal {
if bp, ok := p.(*Bounds); ok {
// Special case, other polygon is *Bounds.
i := &Bounds{
Min: Point{X: math.Max(b.Min.X, bp.Min.X), Y: math.Max(b.Min.Y, bp.Min.Y)},
Max: Point{X: math.Min(b.Max.X, bp.Max.X), Y: math.Min(b.Max.Y, bp.Max.Y)},
}
if i.Min.X >= i.Max.X && i.Min.Y >= i.Max.Y {
return nil
}
return i
}
bp := p.Bounds()
if w := bp.Within(b); w == Inside || w == OnEdge {
// Polygon fully within bounds.
return p
} else if bbp, ok := p.(*Bounds); ok {
// Polygon is bounds.
if w := b.Within(bbp); w == Inside || w == OnEdge {
return b
}
}
if !b.Overlaps(bp) {
return nil
}
return b.Polygons()[0].Intersection(p)
}
// Union returns the combination of the receiver and p.
func (b *Bounds) Union(p Polygonal) Polygonal {
// TODO: optimize
return b.Polygons()[0].Union(p)
}
// XOr returns the area(s) occupied by either the receiver or p but not both.
func (b *Bounds) XOr(p Polygonal) Polygonal {
// TODO: optimize
return b.Polygons()[0].XOr(p)
}
// Difference subtracts p from b.
func (b *Bounds) Difference(p Polygonal) Polygonal {
// TODO: optimize
return b.Polygons()[0].Difference(p)
}
// Area returns the area of the reciever.
func (b *Bounds) Area() float64 {
return (b.Max.X - b.Min.X) * (b.Max.Y - b.Min.Y)
}
// Simplify returns the receiver
// to fulfill the Polygonal interface.
func (b *Bounds) Simplify(tolerance float64) Geom {
return b
}
func (b *Bounds) Centroid() Point {
return Point{(b.Min.X + b.Max.X) / 2, (b.Min.Y + b.Max.Y) / 2}
}