svg_path_editor.geometry#

Classes#

Point

2D point with decimal.Decimal coordinates.

Vec2

2D vector with SymPy coordinates.

Mat2

\(2×2\) matrix

Line

Line segment from p to q.

ParametricEllipticalArc

Elliptical arc in parametric form.

Functions#

rotation_matrix(phi)

Rotation matrix for angle \(φ\) in degrees.

dot(…)

Compute the dot product of two 2D vectors.

polygon_signed_area(poly)

Signed area of a simple polygon.

Module Contents#

svg_path_editor.geometry.rotation_matrix(phi)[source]#

Rotation matrix for angle \(φ\) in degrees.

Uses

\[\begin{split}R(φ) = \begin{pmatrix} \cos φ & -\sin φ \\ \sin φ & \cos φ \end{pmatrix},\end{split}\]

where \(\cos\) and \(\sin\) are evaluated after converting \(φ\) from degrees to radians via sympy.rad().

Parameters:

phi (Expr)

Return type:

Mat2

class svg_path_editor.geometry.Point(x, y)[source]#

2D point with decimal.Decimal coordinates.

Parameters:
x: decimal.Decimal#
y: decimal.Decimal#
__iter__()[source]#

Iterate as (x, y).

Return type:

collections.abc.Iterator[decimal.Decimal]

__eq__(other)[source]#

Compare coordinates for equality.

Parameters:

other (Any)

Return type:

bool

__ne__(value, /)[source]#

Compare coordinates for inequality.

Parameters:

value (object)

Return type:

bool

property vec2: Vec2#

Exact conversion to Vec2.

Coordinates are converted to SymPy rationals via dec_to_rat().

Return type:

Vec2

__str__()[source]#

Human-readable representation (x, y) with decimal formatting.

Return type:

str

__repr__()[source]#

Debug representation Point(x, y) with decimal formatting.

Return type:

str

property length: decimal.Decimal#

Euclidean norm \(‖v‖_2 = \sqrt{x^2 + y^2}\).

Return type:

decimal.Decimal

property normalized: Point#

Unit vector \(v / ‖v‖_2\).

The zero vector is returned unchanged.

Return type:

Point

__neg__()[source]#

Unary minus \(-v\).

Return type:

Point

__add__(other)[source]#

Vector addition \(v + w\).

Parameters:

other (Point)

Return type:

Point

__sub__(other)[source]#

Vector subtraction \(v - w\).

Parameters:

other (Point)

Return type:

Point

__mul__(other)[source]#

Scalar multiplication \(v ⋅ λ\).

Parameters:

other (Number)

Return type:

Point

__truediv__(other)[source]#

Scalar division \(v / λ\).

Parameters:

other (Number)

Return type:

Point

class svg_path_editor.geometry.Vec2[source]#

2D vector with SymPy coordinates.

Supports exact arithmetic and simple linear operations.

x: Expr#
y: Expr#
static from_point(p)[source]#

Construct a Vec2 from a Point.

Coordinates are converted to SymPy rationals via dec_to_rat().

Parameters:

p (Point)

Return type:

Vec2

property point: Point#

Convert to numeric Point.

Uses rat_to_dec() to convert SymPy expressions to Decimal.

Return type:

Point

__iter__()[source]#

Iterate as (x, y).

Return type:

collections.abc.Iterator[Expr]

subs(sub, *, n=None)[source]#

Substitute symbols in both coordinates.

Parameters:
  • sub (dict[Symbol, Expr]) – Substitution dictionary mapping symbols to expressions.

  • n (Precision | None) – Optional precision passed through to subs().

Return type:

Vec2

property swapped: Vec2#

Swap coordinates: \((x, y) ↦ (y, x)\).

Return type:

Vec2

evalf(*, n=None)[source]#

Evaluate coordinates numerically.

Parameters:

n (Precision | None) – Optional precision passed to evalf().

Return type:

Vec2

simplify()[source]#

Simplify both components using sympy.simplify().

Return type:

Vec2

__eq__(other)[source]#

Equality comparison between Vec2 instances, False when comparing to non-Vec2.

Parameters:

other (object)

Return type:

bool

__ne__(value, /)[source]#

Inequality comparison between Vec2 instances, False when comparing to non-Vec2.

Parameters:

value (object)

Return type:

bool

property length: Expr#

Euclidean norm \(‖v‖_2 = \sqrt{x^2 + y^2}\).

Return type:

Expr

property normalized: Vec2#

Unit vector \(v / ‖v‖_2\).

The zero vector is returned unchanged.

Return type:

Vec2

__neg__()[source]#

Unary minus \(-v\).

Return type:

Vec2

__add__(other)[source]#

Vector addition \(v + w\).

Parameters:

other (Vec2)

Return type:

Vec2

__sub__(other)[source]#

Vector subtraction \(v - w\).

Parameters:

other (Vec2)

Return type:

Vec2

__mul__(other)[source]#

Scalar multiplication \(v ⋅ λ\).

Parameters:

other (Expr)

Return type:

Vec2

__truediv__(other)[source]#

Scalar division \(v / λ\).

Parameters:

other (Expr)

Return type:

Vec2

svg_path_editor.geometry.dot(v1: Point, v2: Point) decimal.Decimal[source]#
svg_path_editor.geometry.dot(v1: Vec2, v2: Vec2) Expr

Compute the dot product of two 2D vectors.

class svg_path_editor.geometry.Mat2[source]#

\(2×2\) matrix

\[\begin{split}M = \begin{pmatrix} a & b \\ c & d \end{pmatrix}\end{split}\]

acting on Vec2 by standard matrix-vector multiplication.

Variables:
  • a – Entry \(a_{11}\).

  • b – Entry \(a_{12}\).

  • c – Entry \(a_{21}\).

  • d – Entry \(a_{22}\).

a: Expr#
b: Expr#
c: Expr#
d: Expr#
__matmul__(v)[source]#

Matrix-vector product M @ v.

\[(x', y') = (a x + b y, \; c x + d y).\]
Parameters:

v (Vec2)

Return type:

Vec2

svg_path_editor.geometry.polygon_signed_area(poly)[source]#

Signed area of a simple polygon.

Uses the shoelace formula

\[A = \frac12 \sum_i (x_i y_{i+1} - x_{i+1} y_i),\]

with positive area for counter-clockwise vertex order.

Parameters:

poly (collections.abc.Sequence[Vec2]) – Vertex sequence, implicitly closed.

Return type:

Expr

class svg_path_editor.geometry.Line(p, q)[source]#

Line segment from p to q.

Parametric form

\[L(t) = p + (q - p)\,t, \quad t \in \mathbb{R}.\]
Parameters:
p: Vec2#
q: Vec2#
property delta: Vec2#

Direction vector \(q - p\) of the segment.

Return type:

Vec2

property length: Expr#

Euclidean segment length \(‖q - p‖_2\).

Return type:

Expr

inward_normal(is_ccw)[source]#

Unit inward normal.

For a CCW-oriented boundary, the inward normal is obtained by rotating the edge direction \((Δx, Δy)\) 90° clockwise; for a CW boundary, by rotating 90° counter-clockwise:

\[\begin{split}n = \begin{cases} (Δy, -Δx) & \text{if CCW} \\ (-Δy, Δx) & \text{if CW} \end{cases}\end{split}\]

The resulting vector is then normalized.

Parameters:

is_ccw (bool) – True if the enclosing polygon is CCW oriented.

Return type:

Vec2

offset(*, d, is_ccw, n=None)[source]#

Offset the line along its inward normal by distance d.

Both endpoints are translated by \(d ⋅ n_{\mathit{in}}\).

Parameters:
  • d (Expr) – Signed offset distance.

  • is_ccw (bool) – Orientation of the surrounding boundary.

  • n (Precision | None) – Optional precision used in Vec2.evalf().

Return type:

Line

__call__(t)[source]#

Evaluate the parametric line \(L(t) = p + (q - p)\,t\).

No restriction is imposed on \(t\); points with \(t \notin [0, 1]\) lie on the infinite supporting line but outside the segment.

Parameters:

t (Expr)

Return type:

Vec2

__str__()[source]#

Human-readable representation (p, q).

Return type:

str

__repr__()[source]#

Debug representation Line(p, q).

Return type:

str

class svg_path_editor.geometry.ParametricEllipticalArc[source]#

Elliptical arc in parametric form.

The underlying full ellipse is

\[\begin{split}E(θ) &= R(φ) ⋅ \begin{pmatrix} r_x \cos θ \\ r_y \sin θ \end{pmatrix} + \begin{pmatrix} c_x \\ c_y \end{pmatrix}\end{split}\]

where \(θ\) and \(φ\) are in degrees and \((c_x, c_y)\) is the center.

This arc covers the interval \([θ_0, θ_0 + Δθ]\) modulo \(360°\).

Variables:
  • c – Center \((c_x, c_y)\).

  • r – Radii \((r_x, r_y)\).

  • theta0 – Start angle \(θ_0\) in degrees.

  • dtheta – Sweep \(Δθ\) in degrees (signed).

  • phi – Rotation angle \(φ\) in degrees.

c: Vec2#
r: Vec2#
theta0: Expr#
dtheta: Expr#
phi: Expr#
property theta1: Expr#

End angle of the arc.

Returns:

\(θ_1 = θ_0 + Δθ\) in degrees.

Return type:

Expr

locally_convex(*, is_ccw)[source]#

Test if the arc is locally convex with respect to the boundary orientation.

Returns:

True iff the interior lies on the convex side of the arc for a boundary with orientation is_ccw.

Parameters:

is_ccw (bool)

Return type:

bool

offset(*, d, is_ccw, n=None)[source]#

Offset the arc by changing its radii.

  • \(d > 0\): move inward

  • \(d < 0\): move outward

“Inward” is defined with respect to the polygon orientation: for a CCW boundary, the interior is to the left of the path, for a CW boundary to the right.

Parameters:
  • d (Expr) – Signed offset distance applied to both radii.

  • is_ccw (bool) – Orientation of the surrounding boundary.

  • n (Precision | None) – Unused; kept for API symmetry with Line.offset().

Return type:

ParametricEllipticalArc

angle_condition(theta: Symbol, *, n: Precision | None = None) Boolean[source]#
angle_condition(theta: Expr, *, n: Precision | None = None) Boolean

Test whether theta (in degrees) lies on this arc, modulo 360°.

Works for positive and negative \(Δθ\) and wrap-around intervals.

Parameters:
  • theta – Angle to test, interpreted in degrees.

  • n – Optional precision passed to evalf(), ge(), and le().

point_tangent(theta, n=None)[source]#

Point and tangent at parameter theta (in degrees).

Returns \((p(θ), ±p'(θ))\), where the derivative is w.r.t. \(θ\) in degrees and not normalized. The sign of the derivative is chosen so that the tangent at \(θ_0\) points along the arc and that at \(θ_1\) points away from the arc.

Parameters:
  • n (Precision | None) – Optional precision used in evalf() and sign checks.

  • theta (Expr)

Return type:

tuple[Vec2, Vec2]

transform(p, *, inverse=False)[source]#

Affine map between unit circle and this ellipse.

  • inverse=False: \((u, v) \mapsto (x, y)\) on the ellipse.

  • inverse=True: \((x, y) \mapsto (u, v)\) on the unit circle.

The forward mapping is

\[(x, y) = c + R(φ)\,\mathrm{diag}(r_x, r_y)\,(u, v),\]

where all angles are in degrees.

Parameters:
  • p (Vec2) – Point to transform, either on the unit circle (forward) or on the ellipse (inverse).

  • inverse (bool) – Select direction of the mapping.

Return type:

Vec2

implicit(x, y)[source]#

Implicit ellipse equation at (x, y).

Returns

\[F(x, y) = u^2 + v^2 - 1,\]

where \((u, v)\) is the image of \((x, y)\) under the inverse transform to the unit circle. Points on the ellipse satisfy \(F(x, y) = 0\).

Parameters:
Return type:

Expr