geom2d.ellipse

Two dimensional ellipse and elliptical arc.

class geom2d.ellipse.Ellipse(center: Sequence[float], rx: float, ry: float, phi: float = 0.0)

Two dimensional ellipse.

For the parametric function the parameter t is the parametric angle (aka eccentric anomaly) from the semi-major axis of the ellipse before stretch and rotation (i.e. as if this ellipse were a circle.)

The ellipse will be normalized so that the semi-major axis is aligned with the X axis (i.e. rx >= ry.) The ellipse rotation (phi) will be adjusted 90deg to compensate if necessary.

See:
all_points_inside(points: Iterable[TPoint]) bool

Return True if all the given points are inside this ellipse.

angle_to_theta(angle: float) float

Compute parametric angle from geometric angle.

Parameters:

angle – The geometrical angle from the semi-major axis and a point on the ellipse.

Returns:

t - the parametric angle - 0 < t < 2*PI.

area() float

The area of this ellipse.

curvature(p: Sequence[float]) float

The curvature at a given point.

derivative(t: float, d: int = 1) P

First and second derivatives of the parametric ellipse function.

Parameters:
  • t – Parametric angle - 0 < t < 2*PI.

  • d – 1 => First derivative, 2 => Second derivative. Default is 1.

Returns:

(dx, dy)

Return type:

A 2-tuple

eccentricity() float

The eccentricity e of this ellipse.

focus() float

The focus of this ellipse.

Returns:

Distance from center to focus points.

focus_points() tuple[P, P]

Return the two focus points.

Returns:

A tuple of two focus points along major axis.

is_circle() bool

True if this ellipse is a circle.

point_at(t: float) P

Return the point on the ellipse at t.

This is the parametric function for this ellipse.

Parameters:

t – Parametric angle - 0 < t < 2*PI.

Returns:

A point at t

point_inside(p: Sequence[float]) bool

Test if point is inside ellipse or not.

Parameters:

p – Point (x, y) to test.

Returns:

True if the point is inside the ellipse, otherwise False.

point_to_theta(p: Sequence[float]) float

Compute t given a point on the ellipse.

Parameters:

p – A point on the ellipse.

Returns:

t - the parametric angle - 0 < t < 2*PI.

transform(_matrix: TMatrix) EllipticalArc

Transform this using the specified affine transform matrix.

class geom2d.ellipse.EllipticalArc(p1: Sequence[float], p2: Sequence[float], rx: float, ry: float, phi: float, large_arc: int, sweep_flag: int, start_angle: float, sweep_angle: float, center: Sequence[float])

Two dimensional elliptical arc. A section of an ellipse.

See:

http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes

static from_center(center: Sequence[float], rx: float, ry: float, phi: float, start_angle: float, sweep_angle: float) EllipticalArc

Create an elliptical arc from center parameters.

Parameters:
  • center – The center point of the arc.

  • rx – Semi-major axis length.

  • ry – Semi-minor axis length.

  • start_angle – Start angle of the arc.

  • sweep_angle – Sweep angle of the arc.

  • phi – The angle from the X axis to the semi-major axis of the ellipse.

Returns:

An EllipticalArc

static from_endpoints(p1: Sequence[float], p2: Sequence[float], rx: float, ry: float, phi: float, large_arc: int, sweep_flag: int) EllipticalArc | None

Create an elliptical arc from SVG-style endpoint parameters.

This will correct out of range parameters as per SVG spec. The center, start angle, and sweep angle will also be calculated.

See:

https://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes

Parameters:
  • p1 – The start point of the arc.

  • p2 – The end point of the arc.

  • rx – Semi-major axis length.

  • ry – Semi-minor axis length.

  • phi – The angle in radians from the X axis to the semi-major axis of the ellipse.

  • large_arc – The large arc flag (0 or 1).

  • sweep_flag – The sweep flag (0 or 1).

Returns:

An EllipticalArc or None if the parameters do not describe a valid arc.

to_svg_path(scale: float = 1, add_prefix: bool = True, add_move: bool = False) str

EllipticalArc to SVG path string.

See:

https://www.w3.org/TR/SVG11/paths.html#PathDataEllipticalArcCommands

Parameters:
  • scale – Scale factor. Default is 1.

  • add_prefix – Prefix with the command prefix if True. Default is True.

  • add_move – Prefix with M command if True. Default is False.

A string with the SVG path ‘d’ attribute value that corresponds to this arc.

transform(_matrix: TMatrix) EllipticalArc

Transform this using the specified affine transform matrix.

geom2d.ellipse.center_to_endpoints(center: P, rx: float, ry: float, phi: float, start_angle: float, sweep_angle: float) tuple[P, P, float, float]

Convert center parameterization to endpoint parameterization.

Returns:

(p1, p2, large_arc_flag, sweep_flag)

Return type:

A tuple containing

geom2d.ellipse.draw_ellipse(ellipse: Ellipse, color: str = '#cccc99', width: str | float = '1px', opacity: float = 1, verbose: bool = False, svg_context: SVGContext | None = None) None

Draw an SVG arc for debugging/testing.

geom2d.ellipse.ellipse_in_parallelogram(vertices: Sequence[TPoint], eccentricity: float = 1.0) Ellipse

Inscribe a parallelogram with an ellipse.

See: Horwitz 2008, http://arxiv.org/abs/0808.0297

Vertices:

The four vertices of a parallelogram as a list of 2-tuples.

Eccentricity:

The eccentricity of the ellipse. Where 0.0 >= eccentricity <= 1.0. If eccentricity == 1.0 then a special eccentricity value will be calculated to produce an ellipse of maximal area. The minimum eccentricity of 0.0 will produce a circle.

Returns:

A tuple containing the semi-major and semi-minor axes respectively.

geom2d.ellipse.intersect_circle(c1_center: Sequence[float], c1_radius: float, c2_center: Sequence[float], c2_radius: float) tuple

The intersection (if any) of two circles.

See:

<http://mathworld.wolfram.com/Circle-CircleIntersection.html>

Parameters:
  • c1_center – Center of first circle.

  • c1_radius – Radius of first circle.

  • c2_center – Center of second circle.

  • c2_radius – Radius of second circle.

Returns:

A tuple containing two intersection points if the circles intersect. A tuple containing a single point if the circles are only tangentially connected. An empty tuple if the circles do not intersect or if they are coincident (infinite intersections).

geom2d.ellipse.intersect_circle_line(center: Sequence[float], radius: float, line: Line, on_line: bool = False) list[P]

Find the intersection (if any) of a circle and a Line.

See:

http://mathworld.wolfram.com/Circle-LineIntersection.html

Parameters:
  • center – Center of circle.

  • radius – Radius of circle.

  • line – A line defined by two points (as a 2-tuple of 2-tuples).

  • on_line – If True the intersection(s) must lie on the line segment between its two end points. Default is False.

Returns:

A list containing zero, one, or two intersections as point (x, y) tuples.