Given four integers N, R, X, and Y such that it represents a circle of radius R with [X, Y] as coordinates of the center. The task is to find N random points inside or on the circle.
Examples:
Input: R = 12, X = 3, Y = 3, N = 5
Output: (7.05, -3.36) (5.21, -7.49) (7.53, 0.19) (-2.37, 12.05) (1.45, 11.80)Input: R = 5, X = 1, Y = 1, N = 3
Output: (4.75, 1.03) (2.57, 5.21) (-1.98, -0.76)
Approach: To find a random point in or on a circle we need two components, an angle(theta) and distance(D) from the center. After that Now, the point (xi, yi) can be expressed as:
xi = X + D * cos(theta) yi = Y + D * sin(theta)
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; #define PI 3.141592653589 // Return a random double between 0 & 1 double uniform() { return (double)rand() / RAND_MAX; } // Function to find the N random points on // the given circle vector<pair<double, double> > randPoint( int r, int x, int y, int n) { // Result vector vector<pair<double, double> > res; for (int i = 0; i < n; i++) { // Get Angle in radians double theta = 2 * PI * uniform(); // Get length from center double len = sqrt(uniform()) * r; // Add point to results. res.push_back({ x + len * cos(theta), y + len * sin(theta) }); } // Return the N points return res; } // Function to display the content of // the vector A void printVector( vector<pair<double, double> > A) { // Iterate over A for (pair<double, double> P : A) { // Print the N random points stored printf("(%.2lf, %.2lf)\n", P.first, P.second); } } // Driver Code int main() { // Given dimensions int R = 12; int X = 3; int Y = 3; int N = 5; // Function Call printVector(randPoint(R, X, Y, N)); return 0; } |
(7.05, -3.36) (5.21, -7.49) (7.53, 0.19) (-2.37, 12.05) (1.45, 11.80)
Time Complexity: O(N)
Space Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Equation of circle when three points on the circle are given
- Circle and Lattice Points
- Check whether it is possible to join two points given on circle such that distance between them is k
- Queries on count of points lie inside a circle
- Non-crossing lines to connect points in a circle
- Angular Sweep (Maximum points that can be enclosed in a circle of given radius)
- Number of quadrilateral formed with N distinct points on circumference of Circle
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Find Corners of Rectangle using mid points
- Find K Closest Points to the Origin
- Minimum number of points to be removed to get remaining points on one side of axis
- Find whether only two parallel lines contain all coordinates points or not
- Find Simple Closed Path for a given set of points
- Find the maximum possible distance from origin using given points
- Find points at a given distance on a line of given slope
- Program to find line passing through 2 Points
- Program to find Circumference of a Circle
- Find the other-end coordinates of diameter in a circle
- Program to find area of a circle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

