2021-09-02 20:17:45 +10:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
|
|
|
|
// Box describes an axis-aligned rectangular prism.
|
|
|
|
type Box struct {
|
2021-09-07 13:28:44 +10:00
|
|
|
Min, Max Int3
|
2021-09-02 20:17:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string representation of b like "(3,4,5)-(6,5,8)".
|
|
|
|
func (b Box) String() string {
|
|
|
|
return b.Min.String() + "-" + b.Max.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty reports whether the box contains no points.
|
|
|
|
func (b Box) Empty() bool {
|
|
|
|
return b.Min.X >= b.Max.X || b.Min.Y >= b.Max.Y || b.Min.Z >= b.Max.Z
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eq reports whether b and c contain the same set of points. All empty boxes
|
|
|
|
// are considered equal.
|
|
|
|
func (b Box) Eq(c Box) bool {
|
|
|
|
return b == c || b.Empty() && c.Empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overlaps reports whether b and c have non-empty intersection.
|
|
|
|
func (b Box) Overlaps(c Box) bool {
|
|
|
|
return !b.Empty() && !c.Empty() &&
|
|
|
|
b.Min.X < c.Max.X && c.Min.X < b.Max.X &&
|
|
|
|
b.Min.Y < c.Max.Y && c.Min.Y < b.Max.Y &&
|
|
|
|
b.Min.Z < c.Max.Z && c.Min.Z < b.Max.Z
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns b's width, height, and depth.
|
2021-09-07 13:28:44 +10:00
|
|
|
func (b Box) Size() Int3 {
|
2021-09-02 20:17:45 +10:00
|
|
|
return b.Max.Sub(b.Min)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Back returns an image.Rectangle representing the back of the box, using
|
|
|
|
// the given projection π.
|
2021-09-07 13:14:42 +10:00
|
|
|
func (b Box) Back(π IntProjection) image.Rectangle {
|
2021-09-02 20:17:45 +10:00
|
|
|
b.Max.Z = b.Min.Z
|
|
|
|
return image.Rectangle{
|
2021-09-07 13:14:42 +10:00
|
|
|
Min: π.Project(b.Min),
|
|
|
|
Max: π.Project(b.Max),
|
2021-09-02 20:17:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Front returns an image.Rectangle representing the front of the box, using
|
|
|
|
// the given projection π.
|
2021-09-07 13:14:42 +10:00
|
|
|
func (b Box) Front(π IntProjection) image.Rectangle {
|
2021-09-02 20:17:45 +10:00
|
|
|
b.Min.Z = b.Max.Z
|
|
|
|
return image.Rectangle{
|
2021-09-07 13:14:42 +10:00
|
|
|
Min: π.Project(b.Min),
|
|
|
|
Max: π.Project(b.Max),
|
2021-09-02 20:17:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XY returns the image.Rectangle representing the box if we forgot about Z.
|
|
|
|
func (b Box) XY() image.Rectangle {
|
|
|
|
return image.Rectangle{
|
|
|
|
Min: b.Min.XY(),
|
|
|
|
Max: b.Max.XY(),
|
|
|
|
}
|
|
|
|
}
|