rect.go 756 B

1234567891011121314151617181920212223242526272829303132
  1. package winman
  2. import "fmt"
  3. // Rect represents rectangular coordinates
  4. type Rect struct {
  5. X int // x coordinate
  6. Y int // y coordinate
  7. W int // width
  8. H int // height
  9. }
  10. // NewRect instantiates a new Rect with the given coordinates
  11. func NewRect(x, y, w, h int) Rect {
  12. return Rect{x, y, w, h}
  13. }
  14. // String implements Stringer
  15. func (r Rect) String() string {
  16. return fmt.Sprintf("{(%d, %d) %dx%d}", r.X, r.Y, r.W, r.H)
  17. }
  18. // Contains returns true if the given coordinates are within this rectangle
  19. func (r Rect) Contains(x, y int) bool {
  20. return x >= r.X && x < r.X+r.W && y >= r.Y && y < r.Y+r.H
  21. }
  22. // Rect returns the four coordinates of the rectangle: x, y, width and height
  23. func (r *Rect) Rect() (int, int, int, int) {
  24. return r.X, r.Y, r.W, r.H
  25. }