Distribute circles around a center circle

2019-09-03 21:10发布

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: 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条回答
倾城 Initia
2楼-- · 2019-09-03 21:32

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

查看更多
登录 后发表回答