1
$\begingroup$

I'm a programmer and I saw this question on stackoverflow which exactly does my job: https://stackoverflow.com/questions/13608186/trying-to-plot-coordinates-around-the-edge-of-a-circle.

In this, the answer says that:

var items = 10; // say there are 10 points to be plotted.
var x0 = 40;
var y0 = 40;

// Remember top left pixel of computer screen is (0,0) and both axis go positive from left to right and top to bottom.

for(var i = 0; i < items; i++) {
    var x = x0 + r * Math.cos(2 * Math.PI * i / items); // WHAT IS HAPPENING HERE?
    var y = y0 + r * Math.sin(2 * Math.PI * i / items); // WHAT IS HAPPENING HERE?
}

I'm just undone about the code above. How is that happening. Why are we adding the x0 and y0 (coordinates of center of circle). How does it gives us exact points exactly equidistant from each other? Please explain how this math is working.

For e.g., the above code gives us like this:

enter image description here

That's what I want to do. The points generated is the variable items.

$\endgroup$
  • $\begingroup$ Could you please explain it in a better way? Check the edit I posted @Integrator $\endgroup$ – mehulmpt Nov 20 '14 at 11:41
  • $\begingroup$ For circles centred at (0,0), (rcosθ, rsinθ), represents a point on the circle subtending an angle θ at the origin with the +ve x-axis. $\endgroup$ – Yaitzme Nov 20 '14 at 11:53
  • $\begingroup$ Since the centre of the circle in your case is off-centre, we add the +ve offset to the centres of thee circle. $\endgroup$ – Yaitzme Nov 20 '14 at 11:54
  • 1
    $\begingroup$ I am of reminded of $N^{th}$ roots of unity. That is, for a circle centered at the origin and unit radius, the $N^{th}$ roots of unity are equally spaced around the unit circle. $\endgroup$ – Jose Arnaldo Bebita-Dris Nov 20 '14 at 12:16
1
$\begingroup$
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);}

The equations translated:

$$x(n)=x(0)+r\cos\left(2\pi\frac{n}{N}\right)\\ y(n)=y(0)+r\sin\left(2\pi\frac{n}{N}\right)\\$$

Remember points on a circle with centre at $x_0,y_0$ with radius $r$ at an angle $\theta$ are: $$(x,y)\equiv(x_0+r\cos\theta,y_0+r\sin\theta)$$ The $360^\circ\equiv2\pi$ radians has been divided into $N$ parts and thus the angle between two adjacent points will be $2\pi/N$, I hope you can understand the rest.

$\endgroup$
  • $\begingroup$ Is there any kind of name for these equations? $\endgroup$ – addison Aug 30 '15 at 23:16
3
$\begingroup$

I am reminded of $N^{th}$ roots of unity. That is, for a circle centered at the origin and unit radius, the $N^{th}$ roots of unity are equally spaced around the unit circle.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.