If you think about it when the circle is centered on the edge of another circle, the triangle between two centers and one intersection point has all the known edges: R, R and r (where R is a large radius and r is small). We can use some math to find the angle in the center of the big circle for this:
https://www.mathsisfun.com/algebra/trig-solving-sss-triangles.html
Then we can double it so that the angle takes into account the entire width of the small circle on the edge, and the formula as a result looks like this: 2*Math.acos(1 - (r**2)/(2*R**2))
Now, to make sure that this math is correct, we can draw on the canvas so that the circles located at this angle do not overlap:
function testShape(middleX, middleY, R, r, n) { var i, angle = 0; var inc = 2*Math.acos(1 - (r**2)/(2*R**2)); circle(middleX, middleY, "blue", R); for (i = 0; i < n; i++) { circle(middleX+Math.cos(angle)*R, middleY+Math.sin(angle)*R, "red", r); angle += inc; } }
https://jsfiddle.net/snydergd/rh6jcf30/7/
source share