我正在尝试编写一个脚本,将 100 个不同大小的圆圈放置到舞台上。我在下面概述了简明的要求。
鉴于以下情况:
var stage; // contains a "width" and "height" property.
var circle; // the circle class. contains x, y, radius & a unique id property.
var circleArray; // contains 100 circle instances
要求:
- 编写一个函数,将 100 个不同半径的圆放置到舞台上。
- 展示位置必须是随机但均匀分布的(无聚集)。
- 展示位置必须具有高性能 - 这将在移动网络浏览器上执行。
- 圆圈不得与其他圆圈相交/重叠。
-
circle.x >= 0
一定是真的。 -
circle.y >= 0 && circle.y <= stage.height
一定是真的。 - 圆可以具有以下任意半径大小(在创建时指定):
- 150
- 120
- 90
- 80
- 65
我目前的尝试是一种暴力方法,效率不高。如果我尝试插入超过 10 个圆圈,浏览器就会挂起。下面是我当前的实现,我完全可以放弃它,转而采用性能更高/更好的实现。
这是一个现场演示(注意:没有实际的绘图代码,只有逻辑,但它仍然会锁定浏览器,因此请注意!)http://jsbin.com/muhiziduxu/2/edit?js,console
function adjustForOverlap (circleArray) {
// a reference to the circle that is invoking this function.
var _this = this;
// remove this circle from the array we are iterating over.
var arr = circleArray.filter(function (circle){
return circle.id !== _this.id;
});
// while repeat == true, the circle may be overlapping something.
var repeat = true;
while(repeat) {
var hasOverlap = false;
for (var i=0; i<arr.length; i++) {
var other = arr[i];
var dx = _self.x - other.x;
var dy = _self.y - other.y;
var rr = _self.radius + other.radius;
if (dx * dx + dy * dy < rr * rr) {
// if here, then an overlap was detected.
hit = true;
break;
}
}
// if hit is false, the circle didn't overlap anything, so break.
if (hit === false) {
repeat = false;
break;
} else {
// an overlap was detected, so randomize position.
_self.x = Math.random() * (stage.width*2);
_self.y = Math.random() * stage.height;
}
}
}
最佳答案
有lots of efficient collision detection algorithms 。其中许多方法的工作原理是将空间划分为单元,并维护一个单独的数据结构,并有效查找单元中的其他对象。基本步骤是:
- 为您的新圈子随机指定一个地点
- 确定它位于哪些单元格
- 检查每个单元格是否发生碰撞
- 如果发生冲突,请转到 1。
- 否则,将新圆圈添加到其重叠的每个单元格中。
您可以使用简单的方形网格(即二维数组)作为单元数据结构,或其他类似 quadtree 的东西。 。在某些情况下,您还可以通过首先尝试廉价但粗略的碰撞检查(边界框是否重叠)来获得一点额外的速度,如果返回 true,则尝试稍微昂贵且精确的检查。
更新
对于四叉树,请查看 d3-quadtree ,这应该会给你一个很好的实现,并带有示例。
对于(非常快速,未经测试的)二维数组实现:
function Grid(radius, width, height) {
// I'm not sure offhand how to find the optimum grid size.
// Let's use a radius as a starting point
this.gridX = Math.ceil(width / radius);
this.gridY = Math.ceil(height / radius);
// Determine cell size
this.cellWidth = width / this.gridX;
this.cellHeight = height / this.gridY;
// Create the grid structure
this.grid = [];
for (var i = 0; i < gridY; i++) {
// grid row
this.grid[i] = [];
for (var j = 0; j < gridX; j++) {
// Grid cell, holds refs to all circles
this.grid[i][j] = [];
}
}
}
Grid.prototype = {
// Return all cells the circle intersects. Each cell is an array
getCells: function(circle) {
var cells = [];
var grid = this.grid;
// For simplicity, just intersect the bounding boxes
var gridX1Index = Math.floor(
(circle.x - circle.radius) / this.cellWidth
);
var gridX2Index = Math.ceil(
(circle.x + circle.radius) / this.cellWidth
);
var gridY1Index = Math.floor(
(circle.y - circle.radius) / this.cellHeight
);
var gridY2Index = Math.ceil(
(circle.y + circle.radius) / this.cellHeight
);
for (var i = gridY1Index; i < gridY2Index; i++) {
for (var j = gridX1Index; j < gridX2Index; j++) {
// Add cell to list
cells.push(grid[i][j]);
}
}
return cells;
},
add: function(circle) {
this.getCells(circle).forEach(function(cell) {
cell.push(circle);
});
},
hasCollisions: function(circle) {
return this.getCells(circle).some(function(cell) {
return cell.some(function(other) {
return this.collides(circle, other);
}, this);
}, this);
},
collides: function (circle, other) {
if (circle === other) {
return false;
}
var dx = circle.x - other.x;
var dy = circle.y - other.y;
var rr = circle.radius + other.radius;
return (dx * dx + dy * dy < rr * rr);
}
};
var g = new Grid(150, 1000, 800);
g.add({x: 100, y: 100, radius: 50});
g.hasCollisions({x: 100, y:80, radius: 100});
这是一个功能齐全的示例:http://jsbin.com/cojoxoxufu/1/edit?js,output
请注意,这仅显示 30 个圆圈。看起来这个问题通常无法用您当前的半径、宽度和高度解决。设置为在放弃和接受碰撞之前为每个圆圈查找最多 500 个位置。
关于javascript - 随机有效地放置 100 个圆而没有任何重叠的算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38151767/