If I understand you correctly, this is a pretty standard math question:
Just loop some angle variable in the appropriate step size and use sin() and cos() to calculate your x and y values.
For example:
Let's say you are trying to place 3 objects. There are 360 ββdegrees in a circle. Therefore, each object is at a distance of 120 degrees from the next. If your objects are 20x20 pixels in size, put them in the following places:
x1 = sin( 0 * pi()/180) * r + xc - 10; y1 = cos( 0 * pi()/180) * r + yc - 10 x2 = sin(120 * pi()/180) * r + xc - 10; y2 = cos(120 * pi()/180) * r + yc - 10 x3 = sin(240 * pi()/180) * r + xc - 10; y3 = cos(240 * pi()/180) * r + yc - 10
Here r is the radius of the circle, and (xc, yc) are the coordinates of the center point of the circle. -10's make sure the objects have their center (and not their upper left corner) on the circle. * pi()/180 converts degrees to radians, which require most implementations of sin() and cos() .
Note. This places the shapes evenly distributed in a circle. To make sure that they do not overlap, you should choose your r large enough. If the objects have simple and identical borders, just lay out 10 of them and choose the radius you need, and then, if you need to place 20, make the radius twice as large, 30 times as much as three, and so on. If the objects are irregular in shape and you want to place them in an optimal order in a circle to find the smallest circle, this problem will be extremely messy. Maybe there is a library for this, but I do not have one at the top of my head, and since I have not used D3.js, I am not sure if it will provide this function.
Markus A.
source share