|
| 1 | +# 478. Generate Random Point in a Circle |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Math, Geometry, Rejection Sampling, Randomized. |
| 5 | +- Similar Questions: Random Point in Non-overlapping Rectangles. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Given the radius and the position of the center of a circle, implement the function `randPoint` which generates a uniform random point inside the circle. |
| 10 | + |
| 11 | +Implement the `Solution` class: |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- `Solution(double radius, double x_center, double y_center)` initializes the object with the radius of the circle `radius` and the position of the center `(x_center, y_center)`. |
| 16 | + |
| 17 | +- `randPoint()` returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array `[x, y]`. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Example 1: |
| 22 | + |
| 23 | +``` |
| 24 | +Input |
| 25 | +["Solution", "randPoint", "randPoint", "randPoint"] |
| 26 | +[[1.0, 0.0, 0.0], [], [], []] |
| 27 | +Output |
| 28 | +[null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] |
| 29 | +
|
| 30 | +Explanation |
| 31 | +Solution solution = new Solution(1.0, 0.0, 0.0); |
| 32 | +solution.randPoint(); // return [-0.02493, -0.38077] |
| 33 | +solution.randPoint(); // return [0.82314, 0.38945] |
| 34 | +solution.randPoint(); // return [0.36572, 0.17248] |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +**Constraints:** |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +- `0 < radius <= 108` |
| 43 | + |
| 44 | +- `-107 <= x_center, y_center <= 107` |
| 45 | + |
| 46 | +- At most `3 * 104` calls will be made to `randPoint`. |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +## Solution |
| 51 | + |
| 52 | +```javascript |
| 53 | +/** |
| 54 | + * @param {number} radius |
| 55 | + * @param {number} x_center |
| 56 | + * @param {number} y_center |
| 57 | + */ |
| 58 | +var Solution = function(radius, x_center, y_center) { |
| 59 | + this.radius = radius; |
| 60 | + this.centerX = x_center; |
| 61 | + this.centerY = y_center; |
| 62 | +}; |
| 63 | + |
| 64 | +/** |
| 65 | + * @return {number[]} |
| 66 | + */ |
| 67 | +Solution.prototype.randPoint = function() { |
| 68 | + var radius = Math.sqrt(Math.random()) * this.radius; |
| 69 | + var rand = Math.random(); |
| 70 | + var degree = Math.PI / 2 * (rand === 1 ? 0 : rand); |
| 71 | + var x = Math.cos(degree) * radius; |
| 72 | + var y = Math.sin(degree) * radius; |
| 73 | + return [ |
| 74 | + this.centerX + (Math.random() > 0.5 ? 1 : -1) * x, |
| 75 | + this.centerY + (Math.random() > 0.5 ? 1 : -1) * y, |
| 76 | + ]; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Your Solution object will be instantiated and called as such: |
| 81 | + * var obj = new Solution(radius, x_center, y_center) |
| 82 | + * var param_1 = obj.randPoint() |
| 83 | + */ |
| 84 | +``` |
| 85 | + |
| 86 | +**Explain:** |
| 87 | + |
| 88 | +nope. |
| 89 | + |
| 90 | +**Complexity:** |
| 91 | + |
| 92 | +* Time complexity : O(n). |
| 93 | +* Space complexity : O(1). |
0 commit comments