- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在 Canvas 上绘制 100 个不同大小的圆圈,它们不能重叠。这些圆圈也会从右到左进行动画处理(当它们离开屏幕时会回到 Canvas 的右边缘),并且还会有一些垂直的“摆动”,它们也不能与任何其他圆圈重叠。
下面是我目前正在尝试的,它似乎正在锁定浏览器。我遍历圆集合并执行 detectOverlap()
函数,将圆集合传递给它。
然后 detectOverlap()
函数循环遍历圆圈,执行以下检查:
detectOverlap: function (bubblesArr) {
while (true) {
var hit = 0;
for (var i=0; i<bubblesArr.length; i++) {
var circle = bubblesArr[i];
var dx = this._x - circle._x;
var dy = this._y - circle._y;
var rr = this._radius + circle._radius;
if (dx * dx + dy * dy < rr * rr) {
hit++;
}
}
if (hit == 0) {
break; // didn't overlap, break out of while loop
}
// if we didn't break then there was an overlap somewhere. calc again.
this._x = Math.round(Math.random() * this.stage.getWidth());
this._y = Math.round(Math.random() * this.stage.getHeight());
}
},
如果 hit == 0
,循环中断,我们假设没有重叠。否则,我们随机计算一个新的 X/Y 位置并重新开始该过程。
这似乎效率低下。执行此操作的任何性能提示?
Canvas 类(入口点):这个类是“舞台”,它构建气泡对象,然后将它们添加到 Canvas 。
var $container;
var listData;
var bubbles = [];
function init(l, c) {
$container = c;
listData = l;
// this just draws the canvas. full-width + 500px tall.
var stage = new Konva.Stage({
container: $container.selector,
width: window.innerWidth,
height: 500
});
// this creates the drawing layer where the bubbles will live
layer = new Konva.Layer();
// create an instance of the Bubble class for each element in the list.
for (var i=0; i<listData.length; i++) {
bubbles[i] = new celebApp.Bubble.Bubble(listData[i], stage);
}
/** TODO:::: FIGURE OUT COLLISION DETECTION */
for (var i=0; i<bubbles.length; i++) {
bubbles[i].detectOverlap(bubbles);
}
// create the Konva representation for our generated objects
for (var i=0; i<bubbles.length; i++) {
var b = bubbles[i];
layer.add(new Konva.Circle({
x: b._x,
y: b._y,
radius: b._radius,
fill: b._fill,
stroke: b._stroke,
strokeWidth: b._strokeWidth,
}));
}
// add the layer to the stage
stage.add(layer);
}
气泡类:这是代表绘制到屏幕上的数据的类。我们需要确保这些对象中没有一个相互重叠。
var Bubble = function (listData, stage) {
this.stage = stage;
this._x = Math.round(Math.random() * stage.getWidth()),
this._y = Math.round(Math.random() * stage.getHeight()),
this._radius = Math.round(Math.random() * 80);
this._fill = 'red';
this._stroke = 'black';
this._strokeWidth = 4;
this._speed = 3;
};
Bubble.prototype = {
detectOverlap: function (bubblesArr) {
while (true) {
var hit = 0;
for (var i=0; i<bubblesArr.length; i++) {
var circle = bubblesArr[i];
var dx = this._x - circle._x;
var dy = this._y - circle._y;
var rr = this._radius + circle._radius;
if (dx * dx + dy * dy < rr * rr) {
hit++;
}
}
if (hit == 0) {
break; // didn't overlap
}
this._x = Math.round(Math.random() * this.stage.getWidth());
this._y = Math.round(Math.random() * this.stage.getHeight());
}
},
};
编辑:刚刚根据@MarcB 的评论进行了尝试——但是,浏览器似乎仍然锁定。是否造成了性能瓶颈,但 100 个项目都在运行它们自己的 while()
循环?
for (var i=0; i<bubblesArr.length; i++) {
var circle = bubblesArr[i];
var combinedRadius = Math.abs(circle._radius + this._radius);
var distance = Math.abs(this._x - circle._x);
if (distance <= combinedRadius) {
hit++;
}
}
最佳答案
这看起来像是一个简单的错误。您初始化一个圈子列表。然后,对于列表中的每个圆圈,您计算列表中有多少个圆圈与它重叠。如果您发现重叠,请移动圆圈并重试。
但是每个圈子都会在列表中找自己,发现和自己重叠。你移动它,同样的事情发生。这是一个永无止境的无限循环。
您需要让每个圆圈寻找与其重叠的圆圈。
从算法上讲,您可以使用四叉树等巧妙的数据结构来改进这种重叠检测。这将使您立即找到中心在您的圆圈的一个小盒子内的所有圆圈,并让您以这种方式找到重叠部分。
但是,如果性能有问题,则无需那么努力。取而代之的是按 x 坐标对圆圈进行排序,绘制垂直带,例如,相隔 5 个,然后将每个圆圈放入它相交的所有带中。现在对于每个圆圈,您可以只搜索它相交的所有带。
提高效率的下一步是按 y 坐标对每个波段进行排序,这样您就可以在该波段中进行二进制搜索,以找到与波段相交的所有圆圈,这些圆圈的距离足够近,可能与您的圆圈相交。但这些波段通常应该接近空,所以这算不上什么胜利。
关于javascript - 如何对数百个 Canvas 圆进行高性能重叠/碰撞检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37755123/
以下是我所拥有的。 我唯一需要的是让顶部填充。所以 12 点钟它应该是一个填充,广告 6 点钟它应该以渐变结束。 实现这一目标的最佳方法是什么? (这个想法是让它在下一步中旋转。) Codepen
我用 Canvas 绘制了倒计时 工作代码:http://jsfiddle.net/ajFsx/ window.onload = function() { canvas = document
我是stagexl的新手,我知道这是非常基本的问题,但是我找不到真正快速的答案,因此我认为将这个答案提供给与我处于同一职位的任何人都很好。 如何在stagexl中创建从x到y的线? 以及如何创建一个以
我想知道以编程方式为图像制作圆 Angular 的最佳方法是什么。这可以使用 PHP 或 javascript。一个算法也可以做同样的事情,我可以用 Image::Magick 或 GD 对其进行编码
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this q
我有一组二维点。我想找到: 包含所有点的最小三角形 包含所有点的最小圆。 是否有任何算法可以做到这一点?我遇到了 Convex Hull 来为一组点拟合凸多边形。但我想要一个圆形和三角形。 提前致谢
如何计算两个圆的交点。我希望在所有情况下都有两个、一个或没有交点。 我有中心点的 x 和 y 坐标,以及每个圆的半径。 Python 中的答案是首选,但任何可用的算法都是可以接受的。 最佳答案 Int
我需要用 QPainter 画一个圆。当我像这样使用 drawEllipse 函数时: void UserClass::Draw(QPainter &painter) { painter.sa
计算几何问题: 在多边形(例如BCDE)的边(例如EB)上随机选择点P0,以找到可能的点(即, P1,P2,P3,...) 基于给定距离(即 r)在其他边上。下面的演示展示了一个解决方案,它通过找到以
这个问题在这里已经有了答案: 关闭 13 年前。 重复: What is the best way to create rounded corners How to make a cross bro
我有一个 ionic4 应用程序,我需要在其中实现类似于下面卡片中的 img 效果。在边缘模糊到中心,然后在中心用另一个白色边框清除 我怎样才能做到这一点?请忽略编辑图标 最佳答案 .card
我想旋转一个 SVG 圆圈,同时保持其他元素不旋转 当我尝试使用 rotateZ(15deg) 旋转圆(白色)时,这就是我得到的: 这是我目前的进展: https://jsfiddle.net/41h
我正在尝试根据时间戳实现 LineString 挤压。正如 github 中提到的,它应该被实现,但事实并非如此。它应该类似于下面的屏幕截图。 到目前为止,我发现可以对多边形使用挤压,但随后我必须以某
我用了this question我创建了像this这样的形状但现在我不知道如何在第一次单击时为每个圆圈设置文本? (如井字棋) 最佳答案 给你! - 为了方便起见,我合并了它。只需单击圆圈即可查看其上
如何判断圆和矩形在二维欧几里得空间中是否相交? (即经典的二维几何) 最佳答案 这是我的做法: bool intersects(CircleType circle, RectType rect) {
圆 A 沿 x 轴向右移动。圆 B 沿 y 轴向上移动。我想知道他们是否会发生碰撞。 (不是何时,只是如果。) 半径相同,恒速度不同。 This answer似乎解决了这个问题,我的问题最好应该是这个
Relevant Codesandbox 我一直在我的应用程序中看到一种模式,当我创建圆形的div时,当它们的尺寸较小时,它们有时似乎具有边缘。请参见下面突出显示的代码的图像。为什么会发生这种情况,有
目前,我在 c3.js 中生成的图表图例是颜色矩形,我想将其更改为圆形。我该怎么做? var chart = c3.generate({ data: { columns: [
我需要显示带有圆 Angular 的图像。很久以前,我看到一个网站使用 javascript 库执行此操作,该库将圆 Angular 图像覆盖在普通图像上。 我们是否有任何 javascript 库(
在我的程序中,我使用 css 设计了我的按钮样式。我正在使用“-fx-background-radius”来圆 Angular ,并注意到当我将鼠标悬停在原来的 Angular 上时,它允许我单击按钮
我是一名优秀的程序员,十分优秀!