-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounds.go
70 lines (61 loc) · 1.73 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
package flatsphere
// Represents a rectangular region in arbitrary units, where spherical positions are mapped to the plane.
type Bounds struct {
XMin float64
XMax float64
YMin float64
YMax float64
}
// The width of the bounds (XMax - XMin)
func (b Bounds) Width() float64 {
return b.XMax - b.XMin
}
// The height of the bounds (YMax - YMin)
func (b Bounds) Height() float64 {
return b.YMax - b.YMin
}
// The aspect ratio of the bounds (Width / Height)
func (b Bounds) AspectRatio() float64 {
return b.Width() / b.Height()
}
// Determines whether the given point is inside the bounding rectangle.
func (b Bounds) Within(x float64, y float64) bool {
return x >= b.XMin &&
x <= b.XMax &&
y >= b.YMin &&
y <= b.YMax
}
// Construct a new bounding area from the minimum and maximum along each of the two axes.
// The center point will be a position halfway between the minimum and maximum.
func NewBounds(xmin, ymin, xmax, ymax float64) Bounds {
return Bounds{
XMin: xmin,
YMin: ymin,
XMax: xmax,
YMax: ymax,
}
}
// Construct a bounding area containing the circle described by the given
// radius, centered on the origin.
func NewCircleBounds(radius float64) Bounds {
return NewEllipseBounds(radius, radius)
}
// Construct a bounding area containing the ellipse described by the given
// semiaxes, centered on the origin.
func NewEllipseBounds(semiaxisX float64, semiaxisY float64) Bounds {
return Bounds{
XMin: -semiaxisX,
XMax: semiaxisX,
YMin: -semiaxisY,
YMax: semiaxisY,
}
}
// Construct a bounding area of the given width and height centered on the origin.
func NewRectangleBounds(width float64, height float64) Bounds {
return Bounds{
XMin: -width / 2,
XMax: width / 2,
YMin: -height / 2,
YMax: height / 2,
}
}