Distribute circles around a center circle

China☆狼群 提交于 2019-12-12 02:15:52

问题


I'm trying to place six circles around a center circle. Eeach of them has the same diameter so it should be possible to place them around the center without space between or overlaps. I thinking to be close to the solution but there are small overlaps. I can't find an answer how to get a perfect calculation.

Thats my current result:

and this the way how I've calculated it:

this.distribute = function () {

    var surfaceSize = this.surface.getAbsoluteSize(),
        i,
        x = 0,
        y = 0;

    // 7 CIRCLES 6 AFFECTED
    for (i = 0; i < this.config.length; i += 1) {
        var oBall = this.getBall(i);


        if (i > 0) {
            x = oBall.config.size.width * Math.sin(i);
            y = oBall.config.size.height * Math.cos(i);
        }

        oBall.node.setPosition(x, y, 0);
    }
};

Thanks in advance


回答1:


Just play with 60 deg === PI/3:

http://jsfiddle.net/coma/nk0ms9hb/

var circles = document.querySelectorAll('div.circles > div');

Array.prototype.forEach.call(circles, function (circle, index) {

    var angle = index * Math.PI / 3;

    circle.style.top = Math.sin(angle) + 'em';
    circle.style.left = Math.cos(angle) + 'em';
});

I'm using ems to ease the calculations like 2 * radius === 1em



来源:https://stackoverflow.com/questions/30426255/distribute-circles-around-a-center-circle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!