forked from rougier/matplotlib-3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckered-sphere.py
51 lines (44 loc) · 2 KB
/
checkered-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
# -----------------------------------------------------------------------------
# Copyright (c) 2020 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
# This example shows how to generate and display a checkered sphere
# -----------------------------------------------------------------------------
import numpy as np
from mpl3d.mesh import Mesh
from mpl3d.camera import Camera
def sphere(radius=1.0, slices=32, stacks=32):
slices += 1
stacks += 1
n = slices*stacks
vertices = np.zeros((n,3))
theta1 = np.repeat(np.linspace(0, np.pi, stacks, endpoint=True), slices)
theta2 = np.tile (np.linspace(0, 2 * np.pi, slices, endpoint=True), stacks)
vertices[:,1] = np.sin(theta1) * np.cos(theta2) * radius
vertices[:,2] = np.cos(theta1) * radius
vertices[:,0] = np.sin(theta1) * np.sin(theta2) * radius
indices = []
colors = []
for i in range(stacks-1):
for j in range(slices-1):
indices.append(i*(slices) + j )
indices.append(i*(slices) + j+1 )
indices.append(i*(slices) + j+slices+1)
indices.append(i*(slices) + j+slices )
c = (i+j) % 2
colors.append([c,c,c,1-c*0.1])
indices = np.array(indices).reshape(-1,4)
return vertices, indices, np.array(colors).reshape(-1,4)
# --- main --------------------------------------------------------------------
if __name__ == "__main__":
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4,4))
ax = fig.add_axes([0,0,1,1], xlim=[-1,+1], ylim=[-1,+1], aspect=1)
ax.axis("off")
camera = Camera("perspective", 145, 35, scale=1)
vertices, faces, facecolors = sphere(0.75)
mesh = Mesh(ax, camera.transform, vertices, faces,
facecolors=facecolors, edgecolors="white", mode="all")
camera.connect(ax, mesh.update)
plt.savefig("checkered-sphere.png", dpi=600)
plt.show()