github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/framework/rect/rect.go (about)

     1  package rect
     2  
     3  import "image"
     4  
     5  type Rect struct {
     6  	X int
     7  	Y int
     8  	W int
     9  	H int
    10  }
    11  
    12  func between(x int, a int, b int) bool {
    13  	return a <= x && x <= b
    14  }
    15  
    16  func (rect Rect) Contains(p image.Point) bool {
    17  	return between(p.X, rect.X, rect.X+rect.W) && between(p.Y, rect.Y, rect.Y+rect.H)
    18  }
    19  
    20  func (r0 *Rect) Intersect(r1 *Rect) bool {
    21  	return r1.X <= (r0.X+r0.W) && (r1.X+r1.W) >= r0.X && r1.Y <= (r0.Y+r0.H) && (r1.Y+r1.H) >= r0.Y
    22  }
    23  
    24  func (rect *Rect) Min() image.Point {
    25  	return image.Point{rect.X, rect.Y}
    26  }
    27  
    28  func (rect *Rect) Max() image.Point {
    29  	return image.Point{rect.X + rect.W, rect.Y + rect.H}
    30  }
    31  
    32  func (rect Rect) Scaled(scaling float64) Rect {
    33  	if scaling == 1.0 {
    34  		return rect
    35  	}
    36  	return Rect{int(float64(rect.X) * scaling), int(float64(rect.Y) * scaling), int(float64(rect.W) * scaling), int(float64(rect.H) * scaling)}
    37  }
    38  
    39  func FromRectangle(r image.Rectangle) Rect {
    40  	return Rect{r.Min.X, r.Min.Y, r.Dx(), r.Dy()}
    41  }
    42  
    43  func (r *Rect) Rectangle() image.Rectangle {
    44  	return image.Rect(r.X, r.Y, r.X+r.W, r.Y+r.H)
    45  }