Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if point is in rectangle

Tags:

go

I have an image.Point and an image.Rectangle. I'd like to know how to check if the point is in the rectangle. I know that I can manually check with:

p := image.Point{}
r := image.Rect{}

if r.Min.X <= p.X && p.X < r.Max.X && r.Min.Y <= p.Y && p.Y < r.Max.Y {
    // Point is in the rectangle!
}

But that is a pain! Is there a better way to do this? I can't find a Contains() in the documentation.

like image 715
Rick Smith Avatar asked Sep 28 '22 00:09

Rick Smith


1 Answers

It’s on Point, not Rectangle:

if p.In(r) {
    …
}
like image 77
Ry- Avatar answered Dec 31 '22 20:12

Ry-