478.generate-random-point-in-a-circle.go 600 B

1234567891011121314151617181920212223242526
  1. type Solution struct {
  2. r float64
  3. x float64
  4. y float64
  5. }
  6. func Constructor(radius float64, x_center float64, y_center float64) Solution {
  7. return Solution{
  8. r: radius,
  9. x: x_center,
  10. y: y_center,
  11. }
  12. }
  13. func (this *Solution) RandPoint() []float64 {
  14. phi := rand.Float64() * 2.0 * math.Pi
  15. radius := math.Sqrt(rand.Float64()) * this.r // !!!
  16. dx, dy := radius*math.Cos(phi), radius*math.Sin(phi)
  17. return []float64{this.x + dx, this.y + dy}
  18. }
  19. /**
  20. * Your Solution object will be instantiated and called as such:
  21. * obj := Constructor(radius, x_center, y_center);
  22. * param_1 := obj.RandPoint();
  23. */