VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-n-random-points-within-a-circle/

⇱ Find N random points within a Circle - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find N random points within a Circle

Last Updated : 22 Jun, 2022

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:


Output: 
(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)
 

Comment