命中球体上的指定目标区域

时间:2017-08-01 22:22:33

标签: python arrays numpy random montecarlo

我正在尝试创建一个程序,用于计算球体表面上特定矩形区域的命中数。程序如何工作,是生成随机行,如果目标区域中的其中一行命中,则计数上升一。我的问题是我不认为我正确生成行,我真的知道如何正确设置计数参数。这是我到目前为止的代码,以及我认为应该生成这些行以及count参数可能是什么。

import numpy as np
import random as rand
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


fig = plt.figure()

ax = fig.gca(projection='3d')

ax.set_aspect("equal")  

#rough model of the earth

theta, phi =  np.mgrid[0:2*np.pi : 20j ,0:np.pi : 20j]

r = 6.3

x = r * np.cos(phi)*np.sin(theta)
y = r * np.sin(phi)*np.sin(theta)
z = r * np.cos(theta)

ax.plot_wireframe(x,y,z, color = "k")

#target area
lat1x = 46.49913179 * (2*np.pi/360)
lat2x = 46.4423682 * (2*np.pi/360)
long1y = -119.4049072 * (2*np.pi/360)
long2y = -119.5048141 * (2*np.pi/360)

lat3x = 46.3973998 * (2*np.pi/360)
lat4x = 46.4532495 * (2*np.pi/360)
long3y = -119.4495392 * (2*np.pi/360)
long4y = -119.3492884 * (2*np.pi/360)

def to_cartesian(lat,lon):
    x = r * np.cos(lon)*np.cos(lat)
    y = r * np.sin(lon)*np.cos(lat)
    z = r * np.sin(lat)
    return [x,y,z]

p1 = to_cartesian(lat1x,long1y)
p2 = to_cartesian(lat2x,long2y)
p3 = to_cartesian(lat3x,long3y)
p4 = to_cartesian(lat4x,long4y)

X = np.array([p1,p2,p3,p4])
ax.scatter(X[:,0],X[:,1],X[:,2], color = "r")

#random line path

n = 500

x0 = np.zeros(n)
y0 = np.zeros(n)
z0 = np.zeros(n)

x1 = np.zeros(n)
y1 = np.zeros(n)
z1 = np.zeros(n)

for k in range (n):
     theta = rand.uniform(0.0, np.pi)
     phi = rand.uniform(0, (2 * np.pi)) 
     x0[k] = 100 * np.sin(phi) * np.cos(theta)
     y0[k] = 100 * np.sin(phi) * np.sin(theta)
     z0[k] = 100 * np.cos(theta)

for j in range (n):
    theta = rand.uniform(0.0, np.pi)
    phi = rand.uniform(0, (2 * np.pi))
    x1[j] = 100 * np.sin(phi) * np.cos(theta)
    y1[j] = 100 * np.sin(phi) * np.sin(theta)
    z1[j] = 100 * np.cos(theta)
#ax.plot_wireframe([x0[k],x1[j]],[y0[k],y1[j]],[z0[k],z1[j]], color="g")

# count if hit target area

count = 0

for i in range (n):
    if np.any([x1[i]<=X[:0]<=x0[i]]) and np.any([y1[i]<=X[:1]<=y0[i]]) and 
    np.any([z1[i]<=X[:2]<=z0[i]]):
         count =+1
 print (count)

 plt.show()

0 个答案:

没有答案