-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcosine_weighted_sphere.py
71 lines (58 loc) · 1.72 KB
/
cosine_weighted_sphere.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import matplotlib.pyplot as plt
import numpy as np
import math
# Fixing random state for reproducibility
np.random.seed(19680801)
def randrange(n, vmin, vmax):
"""
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
"""
return (vmax - vmin)*np.random.rand(n) + vmin
def sphere_plot(n):
rands = np.random.rand(n, 2)
xs = []
ys = []
zs = []
pxs = []
pys = []
pzs = []
for rnd in rands:
z = 1 - 2 * rnd[0]
r = math.sqrt(1 - z * z)
phi = 2 * math.pi * rnd[1]
x = r * math.cos(phi)
y = r * math.sin(phi)
xs.append(x)
ys.append(y)
zs.append(z)
pz = z + 1
l = 0.5 * math.sqrt(x*x + y*y + pz*pz)
pxs.append(x / l)
pys.append(y / l)
pzs.append((pz / l) - 1)
#pxs.append(x / l)
#pys.append(y / l)
#pzs.append(-pz / l - 1)
return [xs, ys, zs, pxs, pys, pzs]
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
#ax.set_adjustable('box')
#ax.axis('equal')
n = 1000
plot = sphere_plot(n)
ax.scatter(plot[0], plot[1], plot[2], marker='o', s=2, c="#FFC107", alpha=0.8)
ax.scatter(plot[3], plot[4], plot[5], marker='o', s=2, c="#1E88E5", alpha=0.5)
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
#for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
# xs = randrange(n, 23, 32)
# ys = randrange(n, 0, 100)
# zs = randrange(n, zlow, zhigh)
# ax.scatter(xs, ys, zs, marker=m)
#ax.set_xlabel('X Label')
#ax.set_ylabel('Y Label')
#ax.set_zlabel('Z Label')
ax.set_aspect('equal')
plt.axis('off')
plt.show()