19

I am trying to plot coordinates around a circle programmatically. Here it is hard-coded to show what I am after:

http://jsfiddle.net/jE26S/1/

var iteration = 4;
var left = [94,200,104,-6];
var top = [-6,94,200,94];    

for(var i=0; i<iteration; i++){

    $("#center").append("<div class='point' style='left:"+left[i]+"px;top:"+top[i]+"px'></div>");    

}

Math is definitely not my strong point.

I need to represent people as small Circles standing around a large circle. However, there will be random numbers of people and they all need to be equidistant. I'm not sure whether I should be working from a central point.

d-cubed
  • 1,034
  • 5
  • 30
  • 58
user1503606
  • 3,872
  • 13
  • 44
  • 78
  • 2
    Working from a central point is a good idea. For a circle whose center is `(a,b)` and has radius `r`, the position of a point on the circle's edge is `(a+r*cos(angle), b+r*sin(angle))`. [Here](http://stackoverflow.com/questions/13383112/python-arrange-images-on-canvas-in-a-circle/13384396#13384396) is a SO problem which is very similar to yours. – Kevin Nov 28 '12 at 15:18

1 Answers1

38

Assuming that (x0, y0) is the center of your circle, and r is the radius:

var items = 4;
for(var i = 0; i < items; i++) {

    var x = x0 + r * Math.cos(2 * Math.PI * i / items);
    var y = y0 + r * Math.sin(2 * Math.PI * i / items);   
    $("#center").append("<div class='point' style='left:"+ x +"px;top:"+ y +"px'></div>");    

}
John Ledbetter
  • 13,557
  • 1
  • 61
  • 80
  • 1
    Yup, you'll probably want to subtract the width/height of your point depending on which quadrant the result ends up in – John Ledbetter Nov 28 '12 at 15:29
  • Hi John could you please tell me how i can adjust the above code so that all the small circles are flush (justing touching) each other i.e removing the gap between little circles? thanks – user1503606 Dec 07 '12 at 08:54
  • 2
    Set their diameter to the value of the circumference of the large circle divided by the number of small circles. – Lee Goddard Jan 22 '15 at 08:55