-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathGenerateRandomPointinaCircle.java
More file actions
46 lines (33 loc) · 993 Bytes
/
GenerateRandomPointinaCircle.java
File metadata and controls
46 lines (33 loc) · 993 Bytes
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
class Solution {
double a;
double b;
double r;
Random rand = null;
public Solution(double radius, double x_center, double y_center) {
this.a= x_center;
this.b= y_center;
this.r = radius;
rand = new Random();
}
public double[] randPoint() {
double x = getRandomCoordinate(r, a);
double y = getRandomCoordinate(r, b);
while(getDistance(x,y) >= r*r){
x = getRandomCoordinate(r, a);
y = getRandomCoordinate(r, b);
}
return new double[]{x,y};
}
private double getDistance(double x, double y){
return (x-a)*(x-a) + (y-b)*(y-b);
}
private double getRandomCoordinate(double r, double c){
return c - r + Math.random()*(2*r);
// a -r + radom(2r);
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(radius, x_center, y_center);
* double[] param_1 = obj.randPoint();
*/