mahautils.shapes.Circle#

class mahautils.shapes.Circle(center: List[float] | Tuple[float, float] | ndarray | CartesianPoint2D, radius: float | None = None, diameter: float | None = None, default_num_coordinates: int | None = None, construction: bool = False, polygon_file_enclosed_conv: int = 1, units: str | None = None)#

Bases: ClosedShape2D

An object representing a circle in the 2D Cartesian plane

This class is intended to represent a circle in 2D Cartesian coordinates. Users are required to define the circle’s center and radius (or diameter) when creating Circle objects, and the objects can then perform actions such as returning discretized points on the circumference of the circle and determining whether a point is inside the circle.

Examples

Create a circle centered at \((0,0)\) with radius \(2\):

>>> circle = mahautils.shapes.Circle((0,0), radius=2)

Generate points around the circle’s circumference (output is rounded here for more easily understandable display):

>>> import numpy as np
>>> np.round(circle.points(num_coordinates=4, repeat_end=False))
array([[ 2.,  0.],
       [ 0.,  2.],
       [-2.,  0.],
       [-0., -2.]])

Check whether a point is inside the circle:

>>> circle.is_inside((0, 0))
True
>>> circle.is_inside((2.01, 0))
False

Attributes

area

The area of the circle

center

The center of the circle

circumference

The circumference of the circle

diameter

The diameter of the circle

radius

The radius of the circle

Methods

__init__(center[, radius, diameter, ...])

Creates an object representing a circle

intersection_area(circle)

Calculates the area of intersection with another circle

is_inside(point[, perimeter_is_inside])

Returns whether a point is inside the shape

points([repeat_end, num_coordinates])

Returns a list containing discretized points around the circumference of the circle

reflect(pntA, pntB)

Reflects the shape across a line defined by two points

rotate(center, angle[, angle_units])

Rotates the shape in the \(xy\)-plane

translate([x, y])

Translates the shape in the \(xy\)-plane

xy_coordinates([repeat_end, num_coordinates])

Generates Cartesian coordinates of the circle

Inherited Attributes

construction

Whether the shape is a "construction shape" meant for visual display but not functional geometry

default_num_coordinates

The number of coordinates to use when representing a discretized form of the shape

is_closed

Whether the shape is bounded by a closed perimeter

polygon_file_enclosed_conv

Convention for considering enclosed area when generating a polygon file from ClosedShape2D objects

units

The units in which the geometry is defined

Inherited Methods

reflect_x()

Reflects the shape over the \(x\)-axis

reflect_y()

Reflects the shape over the \(y\)-axis

__init__(center: List[float] | Tuple[float, float] | ndarray | CartesianPoint2D, radius: float | None = None, diameter: float | None = None, default_num_coordinates: int | None = None, construction: bool = False, polygon_file_enclosed_conv: int = 1, units: str | None = None) None#

Creates an object representing a circle

Defines a circle in the 2D Cartesian plane, locating it based on the center of the circle and the radius (or diameter).

Parameters:
  • center (list or tuple or CartesianPoint2D) – The center of the circle

  • radius (float, optional) – The radius of the circle (default is None)

  • diameter (float, optional) – The diameter of the circle (default is None)

  • default_num_coordinates (int, optional) – The default number of coordinates to use when representing the shape (default is None)

  • construction (bool, optional) – Whether the shape is a “construction shape” meant for visual display but not functional geometry (default is False)

  • polygon_file_enclosed_conv (int, optional) – Convention for considering enclosed area when generating a polygon file from ClosedShape2D objects (default is 1)

  • units (str, optional) – The units in which the geometry is defined, or None to indicate dimensionless geometry or that units are to be ignored (default is None)

Notes

It is required that either radius or diameter is provided, but not both. If neither or both are provided, a TypeError will be thrown.

property area: float#

The area of the circle

property center: CartesianPoint2D#

The center of the circle

property circumference: float#

The circumference of the circle

property diameter: float#

The diameter of the circle

property radius: float#

The radius of the circle

intersection_area(circle: Circle)#

Calculates the area of intersection with another circle

Calculates the overlapping area of intersection between this circle and another Circle instance. Mathematical formulas based on https://mathworld.wolfram.com/Circle-CircleIntersection.html.

Parameters:

circle (Circle) – The circle with which area of intersection is to be calculated

Returns:

The area of intersection between this circle and circle

Return type:

float

property construction#

Whether the shape is a “construction shape” meant for visual display but not functional geometry

property default_num_coordinates#

The number of coordinates to use when representing a discretized form of the shape

property is_closed#

Whether the shape is bounded by a closed perimeter

is_inside(point: List[float] | Tuple[float, float] | ndarray | CartesianPoint2D, perimeter_is_inside: bool = True) bool#

Returns whether a point is inside the shape

Parameters:
  • point (list or tuple or CartesianPoint2D) – The point whose location is to be checked. Must be either a CartesianPoint2D instance, or a list or tuple containing two elements of type float

  • perimeter_is_inside (bool, optional) – Whether to consider points on the perimeter of the shape to be inside the shape (default is True)

Returns:

If perimeter_is_inside is True, returns True if point is inside the shape or on the perimeter, and False otherwise. If perimeter_is_inside is False, returns True if point is inside the shape but NOT on the perimeter, and False otherwise

Return type:

bool

property polygon_file_enclosed_conv: int#

Convention for considering enclosed area when generating a polygon file from ClosedShape2D objects

This property is exclusively intended for use when generating Maha Multics polygon files. As discussed on the Polygon File Format page, when generating Maha Multics polygon files, the ENCLOSED_CONV option can be set for any given closed shape to indicate whether the area inside the shape (as determined by the winding number algorithm) is considered by the polygon file to to be “enclosed” area. This property is designed to store the value of this ENCLOSED_CONV option for any ClosedShape2D objects that are to be incorporated into a polygon file.

Note

For more information about the interpretation of this option, refer to the Polygon File Format page.

reflect_x()#

Reflects the shape over the \(x\)-axis

reflect_y()#

Reflects the shape over the \(y\)-axis

property units: str | None#

The units in which the geometry is defined

points(repeat_end: bool = False, num_coordinates: int | None = None) Tuple[ndarray, ...]#

Returns a list containing discretized points around the circumference of the circle

This method returns a tuple, of which each element is a point along the circumference of the circle. Points are ordered in a counterclockwise direction around the circle.

Parameters:
  • repeat_end (bool, optional) – Whether the first and last coordinate returned should be the same point (default is False). This is useful, for instance, when plotting the circle with Matplotlib: if repeat_end is set to False, there may be a slight gap visible between the end points of the circle

  • num_coordinates (int, optional) – The number of points to use when discretizing the circle’s shape (default is None). If this argument is None or omitted, the number of points is set to default_num_coordinates, if provided (otherwise an error is thrown)

See also

points

Returns the same coordinates as points() except that points are returned with all the x-coordinates aggregated and all the y-coordinates aggregated (essentially the transpose of points())

reflect(pntA: List[float] | Tuple[float, float] | ndarray | CartesianPoint2D, pntB: List[float] | Tuple[float, float] | ndarray | CartesianPoint2D) None#

Reflects the shape across a line defined by two points

Parameters:
  • pntA (list or tuple or CartesianPoint2D) – One point on the line across which the shape is to be reflected

  • pntB (list or tuple or CartesianPoint2D) – Another point on the line across which the shape is to be reflected

rotate(center: List[float] | Tuple[float, float] | ndarray | CartesianPoint2D, angle: float, angle_units: str = 'rad') None#

Rotates the shape in the \(xy\)-plane

Rotates the shape the shape a given angle in the \(xy\)-plane about a user-specified point.

Parameters:
  • center (list or tuple or CartesianPoint2D) – The center of rotation about which to rotate the shape

  • angle (float) – The angle by which to rotate the shape about center

  • angle_units (str, optional) – The units (radians or degrees) of the angle argument. Must be either 'rad' or 'deg' (default is 'rad')

translate(x: float = 0, y: float = 0) None#

Translates the shape in the \(xy\)-plane

Translates the shape a user-specified distance in the \(x\)- and/or \(y\)-directions.

Parameters:
  • x (float, optional) – The distance to translate the shape in the \(x\)-direction (default is 0)

  • y (float, optional) – The distance to translate the shape in the \(y\)-direction (default is 0)

xy_coordinates(repeat_end: bool = False, num_coordinates: int | None = None) Tuple[ndarray, ndarray]#

Generates Cartesian coordinates of the circle

This method generates a set of discretized points around the circumference of the circle, in counterclockwise order. Points are returned as a tuple of two NumPy arrays: the first NumPy array contains the x-coordinates of the points, and the second NumPy array contains the y-coordinates. This format makes it relatively easy to plot the shape using packages like Matplotlib.

Parameters:
  • repeat_end (bool, optional) – Whether the first and last coordinate returned should be the same point (default is False). This is useful, for instance, when plotting the circle with Matplotlib: if repeat_end is set to False, there may be a slight gap visible between the end points of the circle

  • num_coordinates (int, optional) – The number of points to use when discretizing the circle’s shape (default is None). If this argument is None or omitted, the number of points is set to default_num_coordinates, if provided (otherwise an error is thrown)

See also

points

Returns the same coordinates as xy_coordinates() except that points are returned with the x- and y-coordinates grouped for each point (essentially the transpose of xy_coordinates())

Examples

Create a Circle object and plot it with Matplotlib:

>>> import matplotlib.pyplot as plt
>>> circle = mahautils.shapes.Circle((0,0), radius=2)
>>> fig = plt.plot(*circle.xy_coordinates(num_coordinates=1000))