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]; //... } 
1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't that be cmath instead of math.h?
@JorenHeit: all functions/constants are defined in math.h. And cmath doesn't define constants automatically.
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.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.