"""Representation of an ellipseMichel BierlaireSat Dec 21 16:45:00 2024"""fromdataclassesimportdataclassfrommatplotlibimportpyplotaspltfrommatplotlib.patchesimportEllipseasMatplotEllipseimportnumpyasnp
[docs]@dataclassclassEllipse:"""Contains the information needed to draw an ellipse"""center_x:floatcenter_y:floatsin_phi:floatcos_phi:floataxis_one:floataxis_two:float
[docs]defdraw_ellipse(ellipse:Ellipse,ax=None,**kwargs):""" Draws an ellipse using matplotlib. :param ellipse: An Ellipse object containing the parameters of the ellipse. :param ax: A matplotlib Axes object. If None, a new figure and axes will be created. :param kwargs: Additional keyword arguments passed to matplotlib.patches.Ellipse. """# Compute the rotation angle of the ellipse in degreesphi=np.arctan2(ellipse.sin_phi,ellipse.cos_phi)angle=np.degrees(phi)# Create the matplotlib ellipsepatch=MatplotEllipse((ellipse.center_x,ellipse.center_y),# Center of the ellipsewidth=2*ellipse.axis_one,# Major axis (diameter)height=2*ellipse.axis_two,# Minor axis (diameter)angle=angle,# Rotation angle in degrees**kwargs# Additional styling options)# Add the ellipse to the axesifaxisNone:fig,ax=plt.subplots(figsize=(6,6))ax.add_patch(patch)# Set limits for better visualizationax.set_xlim(ellipse.center_x-1.5*ellipse.axis_one,ellipse.center_x+1.5*ellipse.axis_one,)ax.set_ylim(ellipse.center_y-1.5*ellipse.axis_two,ellipse.center_y+1.5*ellipse.axis_two,)ax.set_aspect('equal',adjustable='datalim')# Add labels and gridax.axhline(0,color='gray',linestyle='--',linewidth=0.5)ax.axvline(0,color='gray',linestyle='--',linewidth=0.5)ax.set_xlabel('X-axis')ax.set_ylabel('Y-axis')ax.grid(True)# Show plot if ax was NoneifaxisNone:plt.show()