malik727
malik727

Reputation: 179

Finding Coordinates of Points on a Circle's Circumference

What I want to achieve is to store coordinates of all the points which make up the circumference of a circle. I know that this is virtually impossible as there are infinite points on the circumference of a circle but lets say I want to store coordinate found after every 1cm on the circumference of a circle. and the circles circumference is 50cm so in actual i will be storing 25 values in my array.

This is what I have tried so far:

for(var degree=0;degree<360;degree++){
    var radians = degree * Math.PI/180;
    var x = center + radius * Math.cos(radians);
    var y = center + radius * Math.sin(radians);
    //x & y are the coordinates of points on the circumference
} 

But my above code is not working as desired. Help will be highly appreciated!

Upvotes: 2

Views: 1213

Answers (1)

Spektre
Spektre

Reputation: 51845

for that you need to step the angle by appropriate step size (instead of incrementing it). So lets define our circle by:
x0,y0 - circle center
r - radius
d - approximate distance of the points

now we can exploit circle circumference so:

n = ceil (2.0*M_PI*r / d); // integer number of points (rounded up)
da = 2.0*M_PI/n;           // floating angular step between points
for (a=0.0,i=0;i<n;i++,a+=da)
  {
  x = x0 + r*cos(a);
  y = y0 + r*sin(a);
  // here x,y is your point
  }

Sorry I do not code in javascript so the code is in C++ instead so just change the syntax to match yours ...

However if you want all rasterized pixels instead there are other methods that can do that (Bresenham, midpoint , etc ...) some do not even need other operation than +,- see:

Upvotes: 5

Related Questions