我怎样才能让我的圆圈与整个矩形反应?
How can i make my circle react with the whole rectangle?
我想要this.r
(圆圈)对矩形的整个区域(other
)做出反应,if di < this.r
,但是它只对左上角有反应矩形的,因为那是 x 和 y points/coordinates 所在的位置(other.x
和 other.y
)。
intersects(other){
let di = dist(this.x, this.y, other.x, other.y)
if (di < this.r) {
return true;
} else {
return false;
}
}
如何让 "dist" 函数覆盖矩形的整个区域,而不仅仅是左上角?
dist()
计算两点之间的距离。
一个矩形有 4 个角点。在你的情况下,点是 (0, 0), (other.x
, 0), (0, other.y
) 和 (other.x
, other.y
).
但是,要验证圆 "leaves" 是否为矩形区域,您根本不需要 dist()
。您必须验证圆是否在矩形 4 条边中的 1 条外:
intersects(other_x, other_y){#
let is_out = this.x - this.r < 0 || // out at the left
this.x + this.r > other_x || // out at the right
this.y - this.r < 0 || // out at the top
this.y + this.r > other_y; // out at the bottom
return is_out
我想要this.r
(圆圈)对矩形的整个区域(other
)做出反应,if di < this.r
,但是它只对左上角有反应矩形的,因为那是 x 和 y points/coordinates 所在的位置(other.x
和 other.y
)。
intersects(other){
let di = dist(this.x, this.y, other.x, other.y)
if (di < this.r) {
return true;
} else {
return false;
}
}
如何让 "dist" 函数覆盖矩形的整个区域,而不仅仅是左上角?
dist()
计算两点之间的距离。
一个矩形有 4 个角点。在你的情况下,点是 (0, 0), (other.x
, 0), (0, other.y
) 和 (other.x
, other.y
).
但是,要验证圆 "leaves" 是否为矩形区域,您根本不需要 dist()
。您必须验证圆是否在矩形 4 条边中的 1 条外:
intersects(other_x, other_y){#
let is_out = this.x - this.r < 0 || // out at the left
this.x + this.r > other_x || // out at the right
this.y - this.r < 0 || // out at the top
this.y + this.r > other_y; // out at the bottom
return is_out