2019-07-12 13:46:26 -07:00
|
|
|
package mapping
|
|
|
|
|
|
|
|
// Rect is a helper type to represent a rectangular area.
|
|
|
|
type Rect struct {
|
|
|
|
X1, Y1, X2, Y2 int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRect returns a new Rect type with its properties calculated.
|
2019-07-19 21:08:46 -07:00
|
|
|
func NewRect(x int, y int, w int, h int) Rect {
|
|
|
|
return Rect{
|
2019-07-12 13:46:26 -07:00
|
|
|
X1: x,
|
|
|
|
Y1: y,
|
|
|
|
X2: x + w,
|
|
|
|
Y2: y + h,
|
|
|
|
}
|
|
|
|
}
|
2019-07-19 21:08:46 -07:00
|
|
|
|
2019-07-12 13:46:26 -07:00
|
|
|
// Center returns the center of the Rect.
|
|
|
|
func (r *Rect) Center() (x, y int) {
|
|
|
|
return (r.X1 + r.X2) / 2, (r.Y1 + r.Y2) / 2
|
|
|
|
}
|
2019-07-19 21:08:46 -07:00
|
|
|
|
2019-07-12 13:46:26 -07:00
|
|
|
// Intersect returns a bool representing if the Rect intersects with another.
|
2019-07-19 21:08:46 -07:00
|
|
|
func (r *Rect) Intersect(other Rect) bool {
|
2019-07-12 13:46:26 -07:00
|
|
|
if r.X1 <= other.X2 && r.X2 >= other.X1 && r.Y1 <= other.Y2 && r.Y2 >= other.Y1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2019-07-19 21:08:46 -07:00
|
|
|
}
|