How to generate a set of points that are equidistant from each other and lie on a circle

I am trying to create an array of n points that are equidistant from each other and lie on a circle in C. Basically, I need to be able to pass a function the number of points that I would like to generate and get back an array of points.

+4
source share
5 answers

A lot of time has passed since I did C / C ++, so I had a hit on this subject to see how I dealt with it, but here is some code that will calculate the points for you. (This is a console application VS2010)

// CirclePoints.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdio.h" #include "math.h" int _tmain() { int points = 8; double radius = 100; double step = ((3.14159265 * 2) / points); double x, y, current = 0; for (int i = 0; i < points; i++) { x = sin(current) * radius; y = cos(current) * radius; printf("point: %dx:%lf y:%lf\n", i, x, y); current += step; } return 0; } 
+5
source

Try something like this:

 void make_circle(float *output, size_t num, float radius) { size_t i; for(i = 0; i < num; i++) { const float angle = 2 * M_PI * i / num; *output++ = radius * cos(angle); *output++ = radius * sin(angle); } } 

This is untested, it may be hidden hidden in the calculation of the step of the angle, but it should be close.

This suggests that I understood the question correctly, of course.

UPDATE Carry out an angle calculation so as not to increase, to reduce the loss of accuracy of the float due to repeated addition.

+5
source

Here the solution, somewhat optimized, has not been tested. The error may accumulate, but using double rather than float probably more than compensates for it, except for extremely large n values.

 void make_circle(double *dest, size_t n, double r) { double x0 = cos(2*M_PI/n), y0 = sin(2*M_PI/n), x=x0, y=y0, tmp; for (;;) { *dest++ = r*x; *dest++ = r*y; if (!--n) break; tmp = x*x0 - y*y0; y = x*y0 + y*x0; x = tmp; } } 
+2
source

You need to solve this in c:

In the Cartesian coordinate system xy, a circle with central coordinates (a, b) and radius r is the set of all points (x, y) such that

(x - a) ^ 2 + (y - b) ^ 2 = r ^ 2

0
source

This is where the javascript implementation is executed, which also takes an optional center point.

 function circlePoints (radius, numPoints, centerX, centerY) { centerX = centerX || 0; centerY = centerY || 0; var step = (Math.PI * 2) / numPoints, current = 0, i = 0, results = [], x, y; for (; i < numPoints; i += 1) { x = centerX + Math.sin(current) * radius; y = centerY + Math.cos(current) * radius; results.push([x,y]); console.log('point %d @ x:%d, y:%d', i, x, y); current += step; } return results; } 
0
source

All Articles