Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 15e3903

Browse files
aQuaaQua
authored andcommitted
478 accepted. 180ms
1 parent 4f191db commit 15e3903

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

.vscode/bookmarks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"path": "$ROOTPATH$/Algorithms/0478.generate-random-point-in-a-circle/generate-random-point-in-a-circle.go",
1414
"bookmarks": [
1515
{
16-
"line": 29,
16+
"line": 25,
1717
"column": 11,
1818
"label": ""
1919
}
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package problem0478
22

33
import (
4+
"fmt"
45
"math"
56
"math/rand"
67
)
@@ -9,28 +10,24 @@ import (
910
// obj := Constructor(radius, x_center, y_center);
1011
// param_1 := obj.RandPoint();
1112
type Solution struct {
12-
r, a, b, ratio float64
13+
r, a, b float64
1314
}
1415

1516
// Constructor 构建 Solution
1617
func Constructor(radius, xCenter, yCenter float64) Solution {
17-
ratio := 1.
18-
if radius < 1 {
19-
radius = 1.
20-
ratio = 1. / radius
21-
}
2218
return Solution{
23-
r: radius,
24-
a: xCenter,
25-
b: yCenter,
26-
ratio: ratio,
19+
r: radius,
20+
a: xCenter,
21+
b: yCenter,
2722
}
2823
}
2924

3025
// RandPoint 返回随机点
3126
func (s *Solution) RandPoint() []float64 {
32-
rjd := rand.Float64()
33-
x := s.a + s.r*math.Sin(rjd)
34-
y := s.b + s.r*math.Cos(rjd)
35-
return []float64{x / s.ratio, y / s.ratio}
27+
len := math.Sqrt(rand.Float64()) * s.r
28+
deg := rand.NormFloat64() * 2 * math.Pi
29+
x := s.a + len*math.Cos(deg)
30+
y := s.b + len*math.Sin(deg)
31+
fmt.Printf("len = %f, x = %f, y = %f\n", len, x, y)
32+
return []float64{x, y}
3633
}

Algorithms/0478.generate-random-point-in-a-circle/generate-random-point-in-a-circle_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Test_RandPoint(t *testing.T) {
1515
p := s.RandPoint()
1616
x, y := p[0], p[1]
1717
actual := math.Sqrt((x-a)*(x-a) + (y-b)*(y-b))
18-
ast.InDelta(r, actual, 0.00001)
18+
ast.True(actual < r)
1919
}
2020
}
2121

@@ -27,6 +27,6 @@ func Test_RandPoint_2(t *testing.T) {
2727
p := s.RandPoint()
2828
x, y := p[0], p[1]
2929
actual := math.Sqrt((x-a)*(x-a) + (y-b)*(y-b))
30-
ast.InDelta(r, actual, 0.00001)
30+
ast.True(actual < r)
3131
}
3232
}

0 commit comments

Comments
 (0)