forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
49 lines (36 loc) · 1.03 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/// Source : https://leetcode.com/problems/generate-random-point-in-a-circle/description/
/// Author : liuyubobobo
/// Time : 2018-08-28
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
/// Rejection Sampling
/// Time Complexity: O(1)
/// Space Complexity: O(1)
class Solution {
private:
double x, y, r;
public:
Solution(double radius, double x_center, double y_center):
x(x_center), y(y_center), r(radius){}
vector<double> randPoint() {
double randx, randy;
do{
randx = x - r + randDouble(0, 2 * r);
randy = y - r + randDouble(0, 2 * r);
}while(!inCircle(randx, randy));
return {randx, randy};
}
private:
double randDouble(double minDouble, double maxDouble){
double randNum = (double)rand() / INT_MAX;
return randNum * (maxDouble - minDouble) + minDouble;
}
bool inCircle(double x0, double y0){
return (x - x0) * (x - x0) + (y - y0) * (y - y0) <= r * r;
}
};
int main() {
return 0;
}