# 均匀的生成圆和三角形内的随机点

x = r*sin(theta)

y = r*cos(theta)

theta固定后，当r越靠近R，即点越靠近圆的边缘，因此如果r是[0，R]等概率生成的，那么圆的边缘的点会比靠近圆心处要稀疏。

x = r*sin(theta)

y = r*cos(theta)

 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 import  numpy as np import  matplotlib.pyplot as plt import  random import  math   #博客算法1 def  GeneratePointInCycle1(point_num, radius):      for  i  in  range ( 1 , point_num + 1 ):          while  True :              x  =  random.uniform( - radius, radius)              y  =  random.uniform( - radius, radius)              if  (x * * 2 )  +  (y * * 2 ) < (radius * * 2 ):                  break          plt.plot(x, y,  '*' , color  =  "black" )   #博客算法2 def  GeneratePointInCycle2(point_num, radius):      for  i  in  range ( 1 , point_num + 1 ):          x  =  random.uniform( - radius, radius)          y_max  =  math.sqrt(radius * * 2  -  x * * 2 )          y  =  random.uniform( - y_max, y_max)          plt.plot(x, y,  '*' , color  =  "black" )   #博客算法3 def  GeneratePointInCycle3(point_num, radius):      for  i  in  range ( 1 , point_num + 1 ):          theta  =  random.random() * 2 * pi;          r  =  random.uniform( 0 , radius)          x  =  r * math.sin(theta)          y  =  r * math.cos(theta)          plt.plot(x, y,  '*' , color  =  "black" )   #博客算法4 def  GeneratePointInCycle4(point_num, radius):      for  i  in  range ( 1 , point_num + 1 ):          theta  =  random.random() * 2 * pi;          r  =  random.uniform( 0 , radius)          x  =  math.sin(theta) *  (r * * 0.5 )          y  =  math.cos(theta) *  (r * * 0.5 )          plt.plot(x, y,  '*' , color  =  "black" )           pi  =  np.pi theta  =  np.linspace( 0 , pi * 2 ,  1000 ) R  =  1 x  =  np.sin(theta) * R y  =  np.cos(theta) * R   plt.figure(figsize = ( 6 , 6 )) plt.plot(x,y,label  =  "cycle" ,color = "red" ,linewidth = 2 ) plt.title( "cycyle" ) GeneratePointInCycle4( 4000 , R)  #修改此处来显示不同算法的效果 plt.legend() plt.show()

p点在三角形内部的充分必要条件是：1 >= u >= 0, 1 >= v >= 0, u+v <= 1。

 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 import  numpy as np import  matplotlib.pyplot as plt import  matplotlib.lines as line import  random import  math   #对应博客算法5 def  GeneratePointInTriangle1(point_num, pointA, pointB, pointC):      for  i  in  range ( 1 , point_num + 1 ):          u  =  random.uniform( 0.0 ,  1.0 )          v  =  random.uniform( 0.0 ,  1.0  -  u)          pointP  =  u * pointC  +  v * pointB  +  ( 1.0 - u - v) * pointA;          plt.plot(pointP[ 0 ], pointP[ 1 ],  '*' , color  =  "black" )   #根据向量叉乘计算三角形面积，参考 http://www.cnblogs.com/TenosDoIt/p/4024413.html def  ComputeTriangleArea(pointA, pointB, pointC):      return  math.fabs(np.cross(pointB  -  pointA, pointB  -  pointC))  /  2.0   #判断点P是否在三角形ABC内,参考 http://www.cnblogs.com/TenosDoIt/p/4024413.html def  IsPointInTriangle(pointA, pointB, pointC, pointP):      area_abc  =  ComputeTriangleArea(pointA, pointB, pointC)      area_pab  =  ComputeTriangleArea(pointA, pointB, pointP)      area_pbc  =  ComputeTriangleArea(pointP, pointB, pointC)      area_pac  =  ComputeTriangleArea(pointP, pointA, pointC)      return  math.fabs(area_pab  +  area_pac  +  area_pbc  -  area_abc) <  0.000001   #计算一个点关于某一点的中心对称点 def  ComputeCentralSymmetryPoint(point_src, point_center):      return  np.array([point_center[ 0 ] * 2 - point_src[ 0 ], point_center[ 1 ] * 2 - point_src[ 1 ]])   #对应博客算法6 def  GeneratePointInTriangle2(point_num, pointA, pointB, pointC):      for  i  in  range ( 1 , point_num + 1 ):          pointP  =  np.array([random.uniform(pointA[ 0 ], pointB[ 0 ]), random.uniform(pointA[ 1 ], pointC[ 1 ])])          if  not  IsPointInTriangle(pointA, pointB, pointC, pointP):              if  pointP[ 0 ] > pointC[ 0 ]:                  pointP  =  ComputeCentralSymmetryPoint(pointP, np.array([(pointC[ 0 ]  +  pointB[ 0 ]) / 2 , (pointC[ 1 ]  +  pointB[ 1 ]) / 2 ]))              else :                  pointP  =  ComputeCentralSymmetryPoint(pointP, np.array([(pointC[ 0 ]  +  pointA[ 0 ]) / 2 , (pointC[ 1 ]  +  pointA[ 1 ]) / 2 ]))          plt.plot(pointP[ 0 ], pointP[ 1 ],  '*' , color  =  "black" )             fig  =  plt.figure() #三角形三个顶点 pointA  =  np.array([ 0 , 1 ]) pointB  =  np.array([ 3 , 1 ]) pointC  =  np.array([ 1 , 2 ])   plt.plot([pointA[ 0 ],pointB[ 0 ]], [pointA[ 1 ],pointB[ 1 ]]) plt.plot([pointA[ 0 ],pointC[ 0 ]], [pointA[ 1 ],pointC[ 1 ]]) plt.plot([pointB[ 0 ],pointC[ 0 ]], [pointB[ 1 ],pointC[ 1 ]])   GeneratePointInTriangle2( 1500 , pointA, pointB, pointC)  #修改此处来显示不同算法的效果   plt.ylim( 0.5 , 2 ) plt.xlim( 0 , 3 ) plt.show()

|
1月前
|

P8218 【深进1.例1】求区间和 P1719 最大加权矩形
P8218 【深进1.例1】求区间和 P1719 最大加权矩形
11 1
|
22天前
|

17 0
|
22天前
|

24 0
|
2月前
leetcode-1725：可以形成最大正方形的矩形数目
leetcode-1725：可以形成最大正方形的矩形数目
22 0
|

66 0
|

73 0

93 0
09:与圆相关的计算
09:与圆相关的计算
89 0
|

🎈每天进行一道算法题目练习，今天的题目是“非重叠矩形中的随机点”。
155 0
1725. 可以形成最大正方形的矩形数目 : 简单模拟题
1725. 可以形成最大正方形的矩形数目 : 简单模拟题
125 0