【问题标题】:How can i make my circle react with the whole rectangle?我怎样才能让我的圈子对整个矩形做出反应?
【发布时间】:2019-09-15 02:17:15
【问题描述】:

我希望this.r(圆圈)对矩形的整个区域(other)作出反应,if di < this.r,但是,它只对矩形的左上角作出反应,因为那是x 和 y 点/坐标是(other.xother.y)。

intersects(other){
    let di =  dist(this.x, this.y, other.x, other.y)
    if (di < this.r) {
        return true;
    } else {
        return false;
    }
}

如何使“dist”功能覆盖矩形的整个区域,而不仅仅是左上角?

【问题讨论】:

    标签: javascript processing p5.js


    【解决方案1】:

    dist() 计算两点之间的距离。
    一个矩形有 4 个角点。在您的情况下,这些点是 (0, 0)、(other.x, 0)、(0, other.y) 和 (other.x, other.y)。

    但是,要验证圆是否“离开”矩形区域,您根本不需要 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2015-12-16
      • 1970-01-01
      相关资源
      最近更新 更多