0

I need a function that let me find n random points. These points must have the same distance from a given point. Can you help me?

void createCpoints()
{
    int xcenter=3;
    int ycenter=3;
    int radius=3;
    double x[N];
    double y[N];
    //...
}
As As
  • 2,049
  • 4
  • 17
  • 33

2 Answers2

2

Just generate N angles, and calculate the (x,y) coordinates from there using

x1 = xCenter + r * cos(theta1)
y1 = yCenter + r * sin(theta1)

(Note: this is not supposed to be ready-to-use C++ code. If you need help with the language, you need to be more specific.)

JorenHeit
  • 3,877
  • 2
  • 22
  • 28
2

As @5gon12eder says, you could use polar coordinates with the initial point as virtual midpoint:

#include <math.h>

//...

for(int i = 0; i < n; i++) {
    double alpha = 2.0d*M_PI*((double) rand() / RAND_MAX);
    x[i] = radius*cos(alpha)+x0;
    y[i] = radius*sin(alpha)+y0;
}

With (x0,y0) the coordinates of the original point.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Shouldn't that be cmath instead of math.h? – JorenHeit Jan 10 '15 at 19:53
  • @JorenHeit: [all functions/constants](http://www.cs.cf.ac.uk/Dave/C/node17.html) are defined in `math.h`. And `cmath` doesn't define [constants automatically](http://stackoverflow.com/questions/6563810/m-pi-works-with-math-h-but-not-with-cmath-in-visual-studio). – Willem Van Onsem Jan 10 '15 at 22:52