如何使我的圆与整个矩形反应?

时间:2019-09-14 06:45:52

标签: javascript processing p5.js

我希望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”功能覆盖矩形的整个区域,而不仅仅是左上角?

1 个答案:

答案 0 :(得分:1)

here计算两点之间的距离。
矩形具有4个角点。在您的情况下,点是(0,0),(other.y,0),(0,other.x)和(other.ydist())。

但是,要验证圆是否“离开”了矩形区域,则根本不需要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 。您必须验证圆是否在矩形的4个边之一中:

from collections import Counter

def count_deletions(s):
  " number of deletions to make same number of elements "
  if len(s)  == 0:
    return 0

  cnts = list(Counter(s).values())
  cnts.sort(reverse=True)
  deletes = 0
  for i in range(1, len(cnts)):
      while cnts[i] >= cnts[i-1] and cnts[i] > 0: # only delete while count > 0
        cnts[i] -= 1
        deletes += 1
  return deletes

t = ['aaabbb', 'aaabbbccc', 'aaabbbcccddd', 'abc']
print([(x, count_deletions(x)) for x in t]) # [('aaabbb', 1), ('aaabbbccc', 3), ('aaabbbcccddd', 6), ('abc', 2)]