/------------- Monkeyandy original reprinted please specify the source ------------------/
Http://blog.csdn.net/MonkeyAndy
------------------------- If any error occurs, you are welcome to criticize and correct -------------------------
Problem description:
Set rand (S, T) to return a random decimal number between [S, T]. Use this function to find random N points in a circle with a radius of R, the time complexity analysis is provided.
Ideas:
Find random N points in the circle whose radius is R. Since we are looking for a point, we need to establish a coordinate system for it. If we create a plane Cartesian coordinate system with the center of the center as the origin, create a Cartesian coordinate system for a circle with a radius of R. To make a random point in the circle, the absolute value of point X and Y must be smaller than R. The problem is converted: randomly find the points in N circles so that the absolute values of the X and Y coordinates are smaller than R. Here, X is used as an example,
First, X should meet the same requirements as-r <= x <= r; Y. The points generated in this way are all centered around the center of the center, and the side length is within the positive release of 2R, and the value of the distance from R needs to be determined separately. This article introduces how to create a Polar Coordinate System of a circle in the form of Polar Coordinates. If any point in the circle satisfies P (P, θ) (p represents the op length of a line segment with P (P, θ, θ indicates the angle from ox to OP). At this time, the problem is converted to: Find a point, so that the distance from OP to the center of the circle is greater than or equal to 0 and less than R, and the polar angle is greater than or equal to 0.
If the value is less than 360, OP = rand (0, R) is searched again when OP = r, angle = rand (0,360), and angle = 360. Each time a vertex P is found, it is compared with all the previously found vertices. If it overlaps, it will continue to be searched. Otherwise, it will be added to the point set that has been found, until N points are found.
Data Structure of the storage point
Typedef struct point {
Double R;
Double angle;
} Point;
Algorithm flow:
For (I = N; I> 0; I --)
Generate P. R = rand (0, R), and P. R! = R
Generate P. rangle = rand (0,360), and p. rangle! = 360
Traverse all generated points. If P already exists, regenerate the point.
Otherwise, add it to the generated point set.
To generate the nth vertex, traverse the first n-1 vertex. the time complexity is O (n). Therefore, the time complexity of generating n vertex is O (n ^ 2)