-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path478-GenerateRandomPointInACircle.go
More file actions
95 lines (84 loc) · 2.75 KB
/
478-GenerateRandomPointInACircle.go
File metadata and controls
95 lines (84 loc) · 2.75 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
// 478. Generate Random Point in a Circle
// 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.
// Implement the Solution class:
// 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).
// 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].
// Example 1:
// Input
// ["Solution", "randPoint", "randPoint", "randPoint"]
// [[1.0, 0.0, 0.0], [], [], []]
// Output
// [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]]
// Explanation
// Solution solution = new Solution(1.0, 0.0, 0.0);
// solution.randPoint(); // return [-0.02493, -0.38077]
// solution.randPoint(); // return [0.82314, 0.38945]
// solution.randPoint(); // return [0.36572, 0.17248]
// Constraints:
// 0 < radius <= 10^8
// -10^7 <= x_center, y_center <= 10^7
// At most 3 * 10^4 calls will be made to randPoint.
import "fmt"
import "math/rand"
// type Solution struct {
// r float64
// x float64
// y float64
// }
// func Constructor(radius float64, x_center float64, y_center float64) Solution {
// return Solution {
// r: radius,
// x: x_center,
// y: y_center,
// }
// }
// func (this *Solution) RandPoint() []float64 {
// return []float64 {
// this.x + rand.Float64() * this.r,
// this.y + rand.Float64() * this.r,
// }
// }
type Solution struct {
r float64
x float64
y float64
}
func Constructor(radius, xCenter, yCenter float64) Solution {
return Solution{
r: radius,
x: xCenter,
y: yCenter,
}
}
func (this *Solution) RandPoint() []float64 {
// [-1,1) 的随机数
xFactor, yFactor := 1., 1.
for xFactor * xFactor + yFactor * yFactor > 1 {
xFactor = 2 * rand.Float64() - 1
yFactor = 2 * rand.Float64() - 1
}
return []float64{
this.x + this.r * xFactor,
this.y + this.r * yFactor,
}
}
/**
* Your Solution object will be instantiated and called as such:
* obj := Constructor(radius, x_center, y_center);
* param_1 := obj.RandPoint();
*/
func main() {
// Solution solution = new Solution(1.0, 0.0, 0.0);
obj := Constructor(1.0, 0.0, 0.0)
// solution.randPoint(); // return [-0.02493, -0.38077]
fmt.Println(obj.RandPoint())
// solution.randPoint(); // return [0.82314, 0.38945]
fmt.Println(obj.RandPoint())
// solution.randPoint(); // return [0.36572, 0.17248]
fmt.Println(obj.RandPoint())
}