views:

1654

answers:

7

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));
   }
}
+1  A: 

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(...)"

shoosh
+1  A: 

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.

dlamblin
A: 

I was able to solve it on my own by multiplying the number of steps by 2:

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*2-Math.PI/2));
     yValues[i] = (centerY + radius * Math.sin(Math.PI * i / steps*2-Math.PI/2));
   }
}
VirtuosiMedia
Note that this solution works, but for a different reason than what you have stated. You said "multiplying the number of steps by 2" but your expression really means: ((Math.PI * i) / steps)*2 due to associativity.
Greg Hewgill
+3  A: 

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.
Ates Goral
Also, I would store the phase computation in a local variable first: `var phase = 2 * Math.PI * i / steps;`
Ates Goral
+2  A: 

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!

DarenW
+2  A: 

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.

Greg Hewgill
A: 

Any one know how to check that wheter a mouse is clicked inside the circle or polygon. My problem is I want to check that if mouse has been clciked inside the circle or polygon. circle or polygon coordinates has been stored inside an array.

Dheeraj