简体   繁体   English

在球体上绘制点

[英]Plotting Points on a Sphere

I am trying to generate random, uniformly distributed points on a sphere.我正在尝试在球体上生成随机、均匀分布的点。 However, the code is creating points that seem to create a disk instead.但是,该代码正在创建似乎创建磁盘的点。 I believe that the issue resides in the "phirand" definition.我认为问题在于“phirand”的定义。 Is the math there not correct?那里的数学不正确吗? I used the same code in Matlab and it worked in that.我在 Matlab 中使用了相同的代码,它在其中起作用。

Code:代码:

import numpy as np
import pylab
from scipy.integrate import odeint
import matplotlib.pyplot as plt
#import random
import mpl_toolkits.mplot3d.axes3d as p3
import random as rand

particlecount = 10 ## of particles to generate energies for energy generation
binsize = 15 #Determines bin size for historgram of electron energies
RStart = 0.02
phi1 = 0
phi2 = 180
phi1rad = phi1*(np.pi/180)
phi2rad = phi2*(np.pi/180)

#Generate random positions for each particle between s1 and s2
ICPositions = np.array([])
for i in range(0,particlecount):
    #In Spherical: Generates random position with boundaries of: S1<r<S2
    thetarand = (2*np.pi)*rand.uniform(0,1) #Random # generation for component y between s1 and s2
    phirand = np.arcsin((np.sin(phi2rad) - np.sin(phi1rad))*rand.uniform(0,1) + np.sin(phi1rad))
    xrand = RStart*np.sin(phirand)*np.cos(thetarand)
    yrand = RStart*np.sin(phirand)*np.sin(thetarand)
    zrand = RStart*np.cos(phirand)
    randArray = np.array([xrand,yrand,zrand])
    randArray = np.array(randArray,dtype = float)
    if ICPositions.size == 0:
        ICPositions = np.array([randArray])
    else:
        ICPositions = np.append(ICPositions,[randArray],axis = 0)

print(ICPositions)

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(ICPositions[:,0],ICPositions[:,1],ICPositions[:,2],c='r',marker='o')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.show()

I figured out the solution but do not understand why it works.我想出了解决方案,但不明白为什么它有效。 Mathematically there is nothing wrong with my conversion definitions for x, y and z (After I Googled it and looked in a textbook).在数学上,我对 x、y 和 z 的转换定义没有任何问题(在我用谷歌搜索并查看教科书之后)。 However, I saw another post where someone defined these coordinates slightly differently (Replacing sines with cosines for all phi components) without explaining why.但是,我看到另一篇文章,有人对这些坐标的定义略有不同(用余弦替换所有 phi 分量的正弦),但没有解释原因。 The following worked when I replaced this:当我替换它时,以下内容起作用:

xrand = RStart*np.sin(phirand)*np.cos(thetarand)
yrand = RStart*np.sin(phirand)*np.sin(thetarand)
zrand = RStart*np.cos(phirand)

With this:有了这个:

xrand = RStart*np.cos(phirand)*np.cos(thetarand)
yrand = RStart*np.cos(phirand)*np.sin(thetarand)
zrand = RStart*np.sin(phirand)

Again I have no idea why this works but @Jenny gave a hint on another post that was asking a different question about the same code.我再次不知道为什么会这样,但@Jenny 在另一篇帖子中给出了一个提示,该帖子针对相同的代码提出了不同的问题。 Using [-90,90] instead of [0,180] is probably why this change is needed but again I am not certain why as sin[0,90,180] --> [0,1,0] while cos[-90,0,90] --> [0,1,0] and both cover the same numerical ranges.使用 [-90,90] 而不是 [0,180] 可能是需要这种改变的原因,但我也不确定为什么 as sin[0,90,180] --> [0,1,0] 而 cos[-90,0] ,90] --> [0,1,0] 并且都涵盖相同的数值范围。 If someone has a solid mathematical/coding reason for this please comment on my answer to explain further.如果有人对此有可靠的数学/编码原因,请评论我的回答以进一步解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM