作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在获取圆周上的所有点(整数)时遇到问题。
这是我当前的代码:
function getPixelsInRect(x, y, width, height) {
var result = [];
for (var i = y; i < y + height; i++) {
for (var j = x; j < x + width; j++) {
result.push([ j, i ]);
}
}
return result;
}
function getPixelsOnCircle(x, y, radius) {
return getPixelsInRect(x - radius, y - radius, radius * 2, radius * 2).filter(function(pixel) {
return Math.pow(pixel[0] - x, 2) + Math.pow(pixel[1] - y, 2) == (radius * radius);
});
}
它过滤掉不在圆周上的所有点。这显然没有发挥应有的作用,而且我确信这不是正确的方法。有什么建议吗?
(jsfiddle链接:https://jsfiddle.net/852Loubf/)
最佳答案
嗯,我认为这是因为你将距离与半径
比较得太严格
我做了一些更改以考虑阈值,您可以使用新参数threshold
,它是一种厚度。低于 1 它不会是一条连续的线。 (fiddle updated)
function getPixelsOnCircle(x, y, radius, threshold) {
threshold = threshold === undefined ? 1 : threshold;
return getPixelsInRect(x - radius, y - radius, radius * 2, radius * 2).filter(function(pixel) {
var rectDist = Math.pow(pixel[0] - x, 2) + Math.pow(pixel[1] - y, 2);
var rectDistSmoothed = Math.round(rectDist / radius);
return rectDistSmoothed <= radius && rectDistSmoothed >= radius - threshold;
});
}
您的 drawPoints
函数也需要修复:
points.forEach(function(point) {
ctx.fillRect(point[0] - 0.5, point[1] - 0.5, 1, 1);
});
和getPixelsInRect
:
for (var i = y; i <= y + height; i++) {
for (var j = x; j <= x + width; j++) {
result.push([ j, i ]);
}
}
关于javascript - 圆周上的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33826192/
我确定一定有一种方法可以执行以下操作,但我不知道它叫什么,所以我无法用谷歌搜索它。 我需要一个从 A 到 B 的算法。有人知道它叫什么或有链接吗? 编辑:对不起,我不够清楚。图 A 由正方形组成,我基
我是一名优秀的程序员,十分优秀!