[Swift]LeetCode478. 在圆内随机生成点 | Generate Random Point in a Circle

Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random point in the circle.

Note:

  1. input and output values are in floating-point.
  2. radius and x-y position of the center of the circle is passed into the class constructor.
  3. a point on the circumference of the circle is considered to be in the circle.
  4. randPoint returns a size 2 array containing x-position and y-position of the random point, in that order.

Example 1:

Input:
["Solution","randPoint","randPoint","randPoint"]
[[1,0,0],[],[],[]]
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]

Example 2:

Input:
["Solution","randPoint","randPoint","randPoint"]
[[10,5,-7.5],[],[],[]]
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution‘s constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. randPoint has no arguments. Arguments are always wrapped with a list, even if there aren‘t any.



给定圆的半径和圆心的 x、y 坐标,写一个在圆中产生均匀随机点的函数 randPoint 。

说明:

  1. 输入值和输出值都将是浮点数
  2. 圆的半径和圆心的 x、y 坐标将作为参数传递给类的构造函数。
  3. 圆周上的点也认为是在圆中。
  4. randPoint 返回一个包含随机点的x坐标和y坐标的大小为2的数组。

示例 1:

输入:
["Solution","randPoint","randPoint","randPoint"]
[[1,0,0],[],[],[]]
输出: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]

示例 2:

输入:
["Solution","randPoint","randPoint","randPoint"]
[[10,5,-7.5],[],[],[]]
输出: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

输入语法说明:

输入是两个列表:调用成员函数名和调用的参数。Solution 的构造函数有三个参数,圆的半径、圆心的 x 坐标、圆心的 y 坐标。randPoint 没有参数。输入参数是一个列表,即使参数为空,也会输入一个 [] 空列表。



拒绝采样的经典应用: [Swift]LeetCode470. 用 Rand7() 实现 Rand10() | Implement Rand10() Using Rand7()

Runtime: 736 ms

Memory Usage: 17.5 MB

 1 class Solution {
 2     var r:Double
 3     var centerX:Double
 4     var centerY:Double
 5
 6     init(_ radius: Double, _ x_center: Double, _ y_center: Double) {
 7         self.r = radius
 8         self.centerX = x_center
 9         self.centerY = y_center
10     }
11
12     func randPoint() -> [Double] {
13         while(true)
14         {
15             var x:Double = (2 * Double.random(in: 0..<1) - 1.0) * r
16             var y:Double = (2 * Double.random(in: 0..<1) - 1.0) * r
17
18             if x * x + y * y <= r * r
19             {
20                 return [centerX + x, centerY + y]
21             }
22         }
23     }
24 }
25
26 /**
27  * Your Solution object will be instantiated and called as such:
28  * let obj = Solution(radius, x_center, y_center)
29  * let ret_1: [Double] = obj.randPoint()
30  */
31  


Runtime: 748 ms

Memory Usage: 17.5 MB

 1 class Solution {
 2     var r:Double
 3     var centerX:Double
 4     var centerY:Double
 5
 6     init(_ radius: Double, _ x_center: Double, _ y_center: Double) {
 7         self.r = radius
 8         self.centerX = x_center
 9         self.centerY = y_center
10     }
11
12     func randPoint() -> [Double] {
13         var theta:Double = 2 * M_PI * (Double.random(in: 0..<1))
14         var len:Double = sqrt(Double.random(in: 0..<1)) * r
15         return [centerX + len * cos(theta), centerY + len * sin(theta)]
16     }
17 }
18
19 /**
20  * Your Solution object will be instantiated and called as such:
21  * let obj = Solution(radius, x_center, y_center)
22  * let ret_1: [Double] = obj.randPoint()
23  */
24  

原文地址:https://www.cnblogs.com/strengthen/p/10348304.html

时间: 2024-08-11 15:24:05

[Swift]LeetCode478. 在圆内随机生成点 | Generate Random Point in a Circle的相关文章

随机模块(import random)

随机的概念: 在某个范围内取到的每一个值的概率是相同的 随机小数: 1.random.randoom() #0-1之内的随机小数 2.random.unifom(1,5) #范围之内的随机小数 随机整数 random.randint(1,2) #[1,2] 包括2在内的范围内随机取整数 random.randrange(1,2) #[1,2) 不包括2在内的范围内随机取整数 random.randrange(1,10,2) [1,10) 不包含10在内的范围内随机取奇数 随机抽取 random.

如何在半径为1的圆中随机选取一个点

拿到这个题目大部分人的第一个思路是 在x轴[-1,1],y轴[-1,1]的正方形内随机选取一点.然后判断此点是否在圆内(通过计算此点到圆心的距离).如果在圆内,则此点即为所求:如果不在,则重新选取直到找到为止. 正方形的面积为4,圆的面积为pi,所以正方形内的随机点在圆内的概率是pi/4. 如果对机器学习的算法熟悉的话,这种方法叫做拒绝性采样.就是用一种容易生成的概率分布(本题是正方形的均匀分布),去模拟不容易生成的概率. 在单位圆上模拟均匀分布,要模拟满足概率分布 f 的随机变量,其中 f 满

谷歌面试题:在半径为1的圆中随机选取一点

方法1.在x轴[-1, 1],y轴[-1, 1]的正方形内随机选取一点.然后判断此点是否在圆内(通过计算此点到圆心的距离).如果在圆内,则此点即为所求:如果不在,则重新选取直到找到为止.正方形的面积为4,圆的面积为pi,所以正方形内的随机点在圆内的概率是 pi / 4. import java.util.HashMap; import java.util.Map; // 谷歌面试题:在半径为1的圆中随机选取一点 public class FindPointInCircle { // 方法1. /

如何检测一个圆在多个圆内?

问题定义: 存在多个半径相同的圆,和一个半径不同的圆,如何判断半径不同的圆完全在一群圆内.下图演示了几种情况,左边是完全在圆内,右边不是. 解决方法之一: 对于红圆在某个黑圆之内或者在所有黑圆之外等的特例情形,可以用简单的圆圆之间的几何判断算法得到结果,对于其余部分相交的一般情形,如果同时满足以下两个条件则红圆在黑圆内: 1. 红圆与所有黑圆的交点都在黑圆内: 2. 黑圆之间的交点如果在红圆内,则其也必然在黑圆内. 否则,红圆不在黑圆内.

圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point

1 // 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point 2 // 思路:直接暴力绝对T 3 // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再比较y 4 // O(n) 5 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define LL long long 9 const double inf = 123456789012345.0; 10 const LL MO

区域内随机布点的方法

本文研究如何在区域内随机均匀布点, 来源于 <算法可视化 Visualizing Algorithms>, 这在CAE, 图形学中都有重要意义. http://www.bilibili.com/video/av2182749/ 比如在 3*1 的区域内随机布点 1000 个, 如果每个点都是坐标都是随机给的, 结果可能并不均匀. 如 randomPoints.m 所演示的. 1 function randomPoints() 2 xmax = 3; 3 ymax = 1; 4 N = 500;

判断一个点是否在圆内

/* * 判断一个点是不是在圆内 *  *  */public class Demo { public static void main(String[] args) {  //提示用户定义圆形和半径  Scanner sc = new Scanner(System.in);  //请输入圆形坐标  System.out.println("请输入圆心坐标:");  double a1 = sc.nextDouble();  double a2 = sc.nextDouble();   

Problem E: 点在圆内吗?

Description 定义一个Point类和Circle类,用于判断给定的一系列的点是否在给定的圆内. 其中,Point类: 1.有2个成员x和y,分别为其横坐标和纵坐标:1个静态成员numOfPoints,用于计算生成的点的个数. 2.具有构造函数.析构函数和拷贝构造函数,具体格式输出根据样例自行判断. 3. 具有静态方法int getNumOfPoints(),用于返回numOfPoints的值. 4. 具有int getX()和int getY()方法,用于获取横坐标和纵坐标. Circ

圆内三角形统计

[题目描述] 在一个圆的圆周上有N(N <= 100)个点,用线段将它们彼此相连,任意三条线段在圆内都没有公共交点,询问这些线段能构成多少个顶点在圆内的三角形. [输入描述] 输入一个正整数N. [输出描述] 输出一个数表示答案. [样例输入] 6 [样例输出] 1