Trying to plot coordinates around the edge of a circle

user1503606 picture user1503606 · Nov 28, 2012 · Viewed 17.9k times · Source

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.

Answer

John Ledbetter picture John Ledbetter · Nov 28, 2012

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>");    

}