draw_tangle#
Drawing utilities for algebraic tangles.
This module provides a lightweight way to render “algebraic” tangles built from the formal sum/product of elementary pieces (-1, 0, 1). It converts symbolic tangle expressions into geometric zig-zag polylines and offers two renderers:
draw(expr): quick polyline rendering
draw_smooth(expr): smooth rendering using B-splines
Notes: - Heavy dependencies (matplotlib, numpy, scipy) are imported locally inside
the functions that require them to keep import knotpy fast.
Functions
Add bounding-box corners so the zig-zag starts/ends on the rectangle. |
|
|
Return signed angle (degrees) at z2 determined by points z1–z2–z3. |
|
Heuristic test if polyline b is a continuation of polyline a. |
|
Return a short polyline connecting z (left) to w (right) by H/V/diag segments. |
|
Return the number of crossings contributed by an expression. |
|
Quick polyline drawing of an algebraic tangle. |
|
Smooth B-spline rendering of an algebraic tangle. |
|
Build an integral tangle as a nested sum of ±1 and 0. |
|
Convert an algebraic tangle expression to a geometric zig-zag. |
Classes
|
General expression for a tangle; a binary tree of sums/products. |
|
Formal product of tangles. |
|
Formal sum of tangles. |
|
A zig-zag polyline representation of a tangle. |
- class TangleExpr(term1, term2)#
Bases:
object
General expression for a tangle; a binary tree of sums/products.
The expression nodes hold two terms. Leaf values are integers in {-1, 0, 1}.
- Parameters:
term1 – Left term.
term2 – Right term.
- class TangleSum(term1, term2)#
Bases:
TangleExpr
Formal sum of tangles.
- class TangleProduct(term1, term2)#
Bases:
TangleExpr
Formal product of tangles.
- integral(n)#
Build an integral tangle as a nested sum of ±1 and 0.
For |n| ≤ 1 returns the integer directly. Otherwise returns TangleSum(±1, integral(n∓1)) recursively.
- Parameters:
n (int) – Integer tangle value.
- Returns:
int | TangleExpr – The expression representing the integral tangle.
- angle_between_points(z1, z2, z3)#
Return signed angle (degrees) at z2 determined by points z1–z2–z3.
Positive means left turn, negative right turn; 0 means collinear.
- Parameters:
z1 (complex) – First point.
z2 (complex) – Vertex point.
z3 (complex) – Third point.
- Returns:
float – Angle in degrees in range (-180, 180].
- Return type:
float
- class ZigZag#
Bases:
object
A zig-zag polyline representation of a tangle.
The geometry is a list of polyline segments, each a list of complex points. We also store a “compass” rectangle (NW, SW, SE, NE) that indicates the bounding endpoints for the left/right/top/bottom boundary of the tangle.
- property N: complex#
- property S: complex#
- property W: float#
- property E: float#
- property height: float#
- property width: float#
- bounding_box(compass)#
Return tight bounding coordinate for a given compass letter.
- Parameters:
compass (str)
- Return type:
complex | float
- add_line(line)#
Append a non-degenerate polyline to the zig-zag.
- Parameters:
line (list[complex])
- set_compass(NW, SW, SE, NE)#
Set the compass rectangle of the zig-zag.
- Parameters:
NW (complex)
SW (complex)
SE (complex)
NE (complex)
- mirror()#
Mirror through the y-axis.
- rotate()#
Rotate once CCW (multiply coordinates by 1j).
- reflect()#
Reflect (mirror then rotate).
- move(dz)#
Translate the entire zig-zag by complex offset dz.
- Parameters:
dz (complex)
- join()#
Join polyline segments that share endpoints into longer polylines.
- smoothen()#
Placeholder for point simplification (kept for compatibility).
- split()#
Split long nearly straight segments (used for smoothing heuristics).
- connect(z, w, pos)#
Return a short polyline connecting z (left) to w (right) by H/V/diag segments.
Heuristic that prefers diagonal+horizontal or diagonal+vertical depending on geometry and whether we connect along the “north” or “south” side.
- Parameters:
z (complex) – Left endpoint (complex).
w (complex) – Right endpoint (complex).
pos (str) – “N” or “S” — the side we are connecting across.
- Returns:
list[complex] – 2 or 3 points forming the connection polyline.
- Return type:
list[complex]
- crossings(expr)#
Return the number of crossings contributed by an expression.
- Parameters:
expr – int or TangleExpr.
- Returns:
int – Sum of absolute values at integer leaves.
- Return type:
int
- to_zigzag(expr)#
Convert an algebraic tangle expression to a geometric zig-zag.
Supports leaf tangles −1, 0, 1 and compositions via TangleSum/TangleProduct.
- Parameters:
expr – int in {-1, 0, 1} or a TangleExpr.
- Returns:
ZigZag – Polyline representation with compass endpoints.
- Return type:
- add_corners_and_smoothen(zz)#
Add bounding-box corners so the zig-zag starts/ends on the rectangle.
If FIT_INTO_SQUARE is True, the compass rectangle is padded to become a square.
- Parameters:
zz (ZigZag) – A ZigZag to modify in place.
- draw(expr, translate=0j)#
Quick polyline drawing of an algebraic tangle.
Heavy imports are local to keep module import time low.
- Parameters:
expr – Tangle expression (int | TangleExpr).
translate (complex) – Complex offset applied to all points before plotting.
- are_continuation(a, b, radius)#
Heuristic test if polyline b is a continuation of polyline a.
- Parameters:
a (list[complex]) – First polyline points.
b (list[complex]) – Second polyline points.
radius (float) – Unused but kept for compatibility.
- Returns:
bool – True if endpoints are close in one of four adjacency checks.
- Return type:
bool
- draw_smooth(expr)#
Smooth B-spline rendering of an algebraic tangle.
Uses SciPy BSplines and NumPy for sampling. Imports are done locally to keep the library import fast when drawing is not used.
- Parameters:
expr – Tangle expression (int | TangleExpr).