Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circle coordinates to array in Javascript

What's the best way to add the coordinates of a circle to an array in JavaScript? So far I've only been able to do a half circle, but I need a formula that returns the whole circle to two different arrays: xValues and yValues. (I'm trying to get the coordinates so I can animate an object along a path.)

Here's what I have so far:

circle: function(radius, steps, centerX, centerY){
    var xValues = [centerX];
    var yValues = [centerY];
    for (var i = 1; i < steps; i++) {
        xValues[i] = (centerX + radius * Math.cos(Math.PI * i / steps-Math.PI/2));
        yValues[i] = (centerY + radius * Math.sin(Math.PI * i / steps-Math.PI/2));
   }
}
like image 901
VirtuosiMedia Avatar asked Sep 30 '08 23:09

VirtuosiMedia


5 Answers

Your loop should be set up like this instead:

for (var i = 0; i < steps; i++) {
    xValues[i] = (centerX + radius * Math.cos(2 * Math.PI * i / steps));
    yValues[i] = (centerY + radius * Math.sin(2 * Math.PI * i / steps));
}
  • Start your loop at 0
  • Step through the entire 2 * PI range, not just PI.
  • You shouldn't have the var xValues = [centerX]; var yValues = [centerY]; -- the center of the circle is not a part of it.
like image 76
Ates Goral Avatar answered Oct 17 '22 09:10

Ates Goral


Bresenham's algorithm is way faster. You hear of it in relation to drawing straight lines, but there's a form of the algorithm for circles.

Whether you use that or continue with the trig calculations (which are blazingly fast these days) - you only need to draw 1/8th of the circle. By swapping x,y you can get another 1/8th, and then the negative of x, of y, and of both - swapped and unswapped - gives you points for all the rest of the circle. A speedup of 8x!

like image 41
DarenW Avatar answered Oct 17 '22 08:10

DarenW


Change:

Math.PI * i / steps

to:

2*Math.PI * i / steps

A full circle is 2pi radians, and you are only going to pi radians.

like image 44
Greg Hewgill Avatar answered Oct 17 '22 09:10

Greg Hewgill


If you already have half a circle, just mirror the points to get the other half
make sure you do this in the right order.

more speficically, for the other half you simply replace the "+ sin(...)" with a "- sin(...)"

like image 1
shoosh Avatar answered Oct 17 '22 10:10

shoosh


You need to use a partial function to input the radians into cos and sin; therefore take the values you're getting for a quarter or half of the circle, and reflect them over the center points' axis to get your full circle.

That said JavaScript's sin and cos aren't quite as picky, so you must have halved your radian or something; I'd write it as:

function circle(radius, steps, centerX, centerY){
    var xValues = [centerX];
    var yValues = [centerY];
    var table="<tr><th>Step</th><th>X</th><th>Y</th></tr>";
    var ctx = document.getElementById("canvas").getContext("2d");
    ctx.fillStyle = "red"
    ctx.beginPath();
    for (var i = 0; i <= steps; i++) {
        var radian = (2*Math.PI) * (i/steps);
        xValues[i+1] = centerX + radius * Math.cos(radian);
        yValues[i+1] = centerY + radius * Math.sin(radian);
        if(0==i){ctx.moveTo(xValues[i+1],yValues[i+1]);}else{ctx.lineTo(xValues[i+1],yValues[i+1]);}
        table += "<tr><td>" + i + "</td><td>" + xValues[i+1] + "</td><td>" + yValues[i+1] + "</td></tr>";
    }
    ctx.fill();
    return table;
}
document.body.innerHTML="<canvas id=\"canvas\" width=\"300\" height=\"300\"></canvas><table id=\"table\"/>";
document.getElementById("table").innerHTML+=circle(150,15,150,150);

I assumed that for whatever reason you wanted xValues[0] and yValues[0] to be centerX and centerY. I can't figure out why you'd want that, as they're values passed into the function already.

like image 1
dlamblin Avatar answered Oct 17 '22 09:10

dlamblin