478. 在圆内随机生成点

423 阅读1分钟

478. 在圆内随机生成点

给定圆的半径和圆心的位置,实现函数 randPoint ,在圆中产生均匀随机点。

实现 Solution 类:

Solution(double radius, double x_center, double y_center) 用圆的半径 radius 和圆心的位置 (x_center, y_center) 初始化对象
randPoint() 返回圆内的一个随机点。圆周上的一点被认为在圆内。答案作为数组返回 [x, y] 。  

示例 1:

输入: ["Solution","randPoint","randPoint","randPoint"]
[[1.0, 0.0, 0.0], [], [], []]
输出: [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]]
解释:
Solution solution = new Solution(1.0, 0.0, 0.0);
solution.randPoint ();//返回[-0.02493,-0.38077]
solution.randPoint ();//返回[0.82314,0.38945]
solution.randPoint ();//返回[0.36572,0.17248]  

提示:

0 < radius <= 108
-107 <= x_center, y_center <= 107
randPoint 最多被调用 3 * 104 次

代码实现

/**
 * @param {number} radius
 * @param {number} x_center
 * @param {number} y_center
 */
var Solution = function(radius, x_center, y_center) {
   this.radius = radius;
   this.x_center = x_center;
   this.y_center = y_center;
};

/**
 * @return {number[]}
 */
Solution.prototype.randPoint = function() {
    while (true) {
     const xFlag = Math.floor(Math.random() * 10);
     const yFlag = Math.floor(Math.random() * 10);
     let xOffet = Math.random() * this.radius;
     let yOffet = Math.random() * this.radius;
     const x = xFlag > 4 ? this.x_center + xOffet : this.x_center - xOffet;
     const y = yFlag > 4 ? this.y_center + yOffet : this.y_center - yOffet;
     const distance = Math.sqrt(Math.pow(this.x_center-x,2) + Math.pow(this.y_center-y,2));
     if (distance <= this.radius) return [x, y];
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * var obj = new Solution(radius, x_center, y_center)
 * var param_1 = obj.randPoint()
 */