Skip to content

Commit fcd39ec

Browse files
committed
add plot_cone
1 parent 34db4aa commit fcd39ec

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

‎spatialmath/base/graphics.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,79 @@ def plot_cylinder(
844844

845845
return handles
846846

847+
def plot_cone(
848+
radius,
849+
height,
850+
resolution=50,
851+
flip=False,
852+
centre=(0, 0, 0),
853+
ends=False,
854+
ax=None,
855+
filled=False,
856+
**kwargs
857+
):
858+
"""
859+
Plot a cone using matplotlib
860+
861+
:param radius: radius of cone at open end
862+
:type radius: float
863+
:param height: height of cone in the z-direction
864+
:type height: float
865+
:param resolution: number of points on circumferece, defaults to 50
866+
:type resolution: int, optional
867+
:param flip: cone faces upward, defaults to False
868+
:type flip: bool, optional
869+
870+
:param pose: pose of cone, defaults to None
871+
:type pose: SE3, optional
872+
:param ax: axes to draw into, defaults to None
873+
:type ax: Axes3D, optional
874+
:param filled: draw filled polygon, else wireframe, defaults to False
875+
:type filled: bool, optional
876+
:param kwargs: arguments passed to ``plot_wireframe`` or ``plot_surface``
877+
878+
:return: matplotlib objects
879+
:rtype: list of matplotlib object types
880+
881+
The axis of the cone is parallel to the z-axis and it is drawn pointing
882+
down. The point is at z=0 and the open end at z= ``height``. If ``flip`` is
883+
True then the cone faces upwards, the point is at z= ``height`` and the open
884+
end at z=0.
885+
886+
The cylinder can be positioned by setting ``centre``, or positioned
887+
and orientated by setting ``pose``.
888+
889+
:seealso: :func:`~matplotlib.pyplot.plot_surface`, :func:`~matplotlib.pyplot.plot_wireframe`
890+
"""
891+
ax = axes_logic(ax, 3)
892+
893+
# https://stackoverflow.com/questions/26874791/disconnected-surfaces-when-plotting-cones
894+
# Set up the grid in polar coords
895+
theta = np.linspace(0, 2 * np.pi, resolution)
896+
r = np.linspace(0, radius, resolution)
897+
T, R = np.meshgrid(theta, r)
898+
899+
# Then calculate X, Y, and Z
900+
X = R * np.cos(T) + centre[0]
901+
Y = R * np.sin(T) + centre[1]
902+
Z = np.sqrt(X**2 + Y**2) / radius * height + centre[2]
903+
if flip:
904+
Z = height - Z
905+
906+
handles = []
907+
handles.append(_render3D(ax, X, Y, Z, filled=filled, **kwargs))
908+
handles.append(_render3D(ax, X, (2 * centre[1] - Y), Z, filled=filled, **kwargs))
909+
910+
if ends and kwargs.get("filled", default=False):
911+
floor = Circle(centre[:2], radius, **kwargs)
912+
handles.append(ax.add_patch(floor))
913+
pathpatch_2d_to_3d(floor, z=height[0], zdir="z")
914+
915+
ceiling = Circle(centre[:2], radius, **kwargs)
916+
handles.append(ax.add_patch(ceiling))
917+
pathpatch_2d_to_3d(ceiling, z=height[1], zdir="z")
918+
919+
return handles
847920

848921
def plot_cuboid(
849922
sides=[1, 1, 1], centre=(0, 0, 0), pose=None, ax=None, filled=False, **kwargs

0 commit comments

Comments
 (0)