.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\piecewise_geodesic.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_piecewise_geodesic.py: Piecewise Geodesic Example ========================== Compute and visualize a piecewise geodesic curve on the unit sphere. .. GENERATED FROM PYTHON SOURCE LINES 7-116 .. image-sg:: /auto_examples/images/sphx_glr_piecewise_geodesic_001.png :alt: Piecewise Geodesic on Sphere :srcset: /auto_examples/images/sphx_glr_piecewise_geodesic_001.png :class: sphx-glr-single-img .. code-block:: Python import numpy as np import plotly.graph_objects as go import plotly.io as pio from spheresmooth import piecewise_geodesic # ------------------------------------------------------------- # Example setup # ------------------------------------------------------------- c = 1/np.sqrt(3) control_points = np.array([ [ c, c, c], [ c, c, -c], [-c, c, c], [-c, c, -c] ]) knots = np.array([1, 2, 3, 3.5]) t = np.linspace(0, 4, 300) curve = piecewise_geodesic(t, control_points, knots) # ------------------------------------------------------------- # Sphere mesh # ------------------------------------------------------------- u = np.linspace(0, 2*np.pi, 60) v = np.linspace(0, np.pi, 30) x = np.outer(np.cos(u), np.sin(v)) y = np.outer(np.sin(u), np.sin(v)) z = np.outer(np.ones_like(u), np.cos(v)) # ------------------------------------------------------------- # Plot # ------------------------------------------------------------- fig = go.Figure() # Sphere surface fig.add_surface( x=x, y=y, z=z, opacity=0.1, colorscale="Greys", showscale=False ) # Control points fig.add_trace(go.Scatter3d( x=control_points[:, 0], y=control_points[:, 1], z=control_points[:, 2], mode='markers', marker=dict(size=5, color='blue', opacity=1), name='Control Points' )) # Geodesic path fig.add_trace(go.Scatter3d( x=curve[:, 0], y=curve[:, 1], z=curve[:, 2], mode='lines', line=dict(color='red', width=6), name='Piecewise Geodesic' )) # Optional: lat/long grid lat_values = np.linspace(-60, 60, 8) for lat in lat_values: phi = np.radians(lat) ugrid = np.linspace(0, 2*np.pi, 200) fig.add_trace(go.Scatter3d( x=np.cos(ugrid) * np.cos(phi), y=np.sin(ugrid) * np.cos(phi), z=np.full_like(ugrid, np.sin(phi)), mode='lines', line=dict(color='gray', width=1), showlegend=False )) lon_values = np.linspace(0, 330, 12) for lon in lon_values: lam = np.radians(lon) vgrid = np.linspace(-np.pi/2, np.pi/2, 200) fig.add_trace(go.Scatter3d( x=np.cos(vgrid) * np.cos(lam), y=np.cos(vgrid) * np.sin(lam), z=np.sin(vgrid), mode='lines', line=dict(color='gray', width=1), showlegend=False )) fig.update_layout( title="Piecewise Geodesic on the Sphere", scene=dict( xaxis=dict(visible=False), yaxis=dict(visible=False), zaxis=dict(visible=False), aspectmode="data" ) ) import matplotlib.pyplot as plt import matplotlib.image as mpimg img = mpimg.imread("piecewise_geodesic_example.png") plt.figure(figsize=(6, 6)) plt.imshow(img) plt.axis("off") plt.title("Piecewise Geodesic on Sphere") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.260 seconds) .. _sphx_glr_download_auto_examples_piecewise_geodesic.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: piecewise_geodesic.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: piecewise_geodesic.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: piecewise_geodesic.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_