go - 检查点是否在矩形中

标签 go

我有一个 image.Point 和一个 image.Rectangle。我想知道如何检查点是否在矩形中。我知道我可以手动检查:

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!
}

但这很痛苦!有一个更好的方法吗?我在 documentation 中找不到 Contains() .

最佳答案

It’s on Point, not Rectangle:

if p.In(r) {
    …
}

关于go - 检查点是否在矩形中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32019777/

相关文章:

json - 想要发送空 json 字符串 "{}"而不是 "null"

xml - 无法读取请求 xml 正文元素值

go - 命名成员方法时如何在 "updateSomething"和 "setSomething"之间进行选择?

go - 使用整数作为小数系数而不是 float 对于货币应用来说是个好主意吗?

api - Golang - API Server 和 Socket 同时存在

ssh - 如何去除golang ssh客户端中的颜色代码

json - 将 JSON 解码为结构

parallel-processing - goroutine 的最小工作量

go - 是否可以在golang中创建进程池

go - cgo(golang): error: underfined reference to 'hello'