const { position: { x, y }, cell_radius, food_size, food } = this;
const isIntersects = item =>
(item.x - x) ** 2 + (item.y - y) ** 2 <= (cell_radius + food_size) ** 2;
food.splice(0, food.length, ...food.filter(n => !isIntersects(n)));
// или
for (let i = food.length; i--;) {
if (isIntersects(food[i])) {
food.splice(i, 1);
}
}
// или
let countEaten = 0;
for (let i = 0; i < food.length; i++) {
food[i - countEaten] = food[i];
countEaten += isIntersects(food[i]);
}
food.length -= countEaten;
for (var i = 0; i < this.food.length; i++) {
const {x: x1, y: y1} = this.position
const r1 = this.cell_radius
const {x: x2, y: y2} = this.food[i]
const r2 = this.food_size
const distance = Math.sqrt( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) )
const intersects = distance <= r1 + r2
if(intersects) {
console.log('I EAT', this.food[i])
}
}