Wednesday, March 17, 2021

[LeetCode] Generate Random Point in a Circle

Problem: Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random point in the circle.

Note:

  1. input and output values are in floating-point.
  2. radius and x-y position of the center of the circle is passed into the class constructor.
  3. a point on the circumference of the circle is considered to be in the circle.
  4. randPoint returns a size 2 array containing x-position and y-position of the random point, in that order.

Example:

["Solution","randPoint","randPoint","randPoint"]
[[1,0,0],[],[],[]]
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]
Input: 
["Solution","randPoint","randPoint","randPoint"]
[[10,5,-7.5],[],[],[]]
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution's constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. randPoint has no arguments. Arguments are always wrapped with a list, even if there aren't any.


Approach: The intuition here is it's easy to generate a random point in a square than in a circle. Now we can basically generate a random point in a square of side with size of side as "2R". It will look like below image:


Now we need to check for each point if the distance between generated point and center point is <= radius. If yes, we got our point otherwise we need to generate the random point again and do the same comparison.

You may be worried that it can take a lot of time, actually infinite time. In theory it could happen but practically its not. Let's see why:

Area of circle = 3.14 * R ^ 2

Area of square = (2 * R) ^  2 = 4 * R ^ 2

That means we will get a valid point:

((3.14 * R ^ 2) / (4 *  R ^ 2)) * 100 = (3.14 / 4) * 100 = 0.785 * 100 = 78.5% of times.

That means we will run the loop approximately (1/.785) = 1.2738 times to get the valid point. 


Implementation in C#:

public class Solution 

{

    public Solution(double radius, double x_center, double y_center) 

    {

        this.radius = radius;

        this.centerX = x_center;

        this.centerY = y_center;

    }

    

    public double[] RandPoint() 

    {

        Random random = new Random();

        while (true)

        {

            double newX = (this.centerX - this.radius) + random.NextDouble() * 2 * this.radius;

            double newY = (this.centerY - this.radius) + random.NextDouble() * 2 * this.radius;

            if (Math.Sqrt(Math.Pow((newX - this.centerX), 2) + Math.Pow((newY - this.centerY), 2)) <= this.radius)

            {

                return new double[] { newX, newY };

            }

        }

    }

    double radius, centerX, centerY;

}


Complexity: O(1) on average but O(infinite) on worst case.

No comments:

Post a Comment