mathx

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package mathx provides math-rendering mobjects: LaTeX equations, number lines, 2D coordinate axes, and plotted functions.

Equations are rendered via a pure-Go LaTeX parser (tdewolff/canvas.ParseLaTeX, backed by star-tex.org/x/tex). No external LaTeX installation is required. The parser supports a useful subset of math expressions; complex documents and custom packages are out of scope.

When rendered in sketchy mode (Sloppiness > Architect), equations pick up the active style's roughness — producing a "handwritten math" effect that's unique to this library. The math stays correct (path topology preserved); only the stroke style changes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HighlightTerm

func HighlightTerm(eq *Equation, index int, duration time.Duration) animation.Animation

HighlightTerm flashes a specific submobject of an equation by index. Useful for "look at the m in E=mc^2." Index is 0-based; out-of-range indices are no-ops.

Current limitation: highlight pulses the equation's overall scale via SetReveal. True per-symbol highlighting requires per-symbol position/style state and is not yet implemented.

func TransformEquation

func TransformEquation(from, to *Equation, duration time.Duration) animation.Animation

TransformEquation morphs one equation into another. Phase-4 approximation: cross-fade between the two — `from` fades out while `to` fades in. A proper symbol-matching morph (where shared symbols reposition rather than fade) is a Phase-5 follow-up.

Both equations must be added to the scene; the caller is responsible for that.

func Write

func Write(eq *Equation, duration time.Duration) animation.Animation

Write animates an equation as if being handwritten: each symbol fades in with a small stagger, so glyphs appear left-to-right rather than the entire equation popping in at once. Conceptually like DrawOn for math.

Implementation: rather than truly drawing each glyph stroke-by-stroke (which is a Phase-5 nicety), we reveal the equation as a whole using the reveal fraction. The animation duration is split: nothing visible for first 10%, then linear reveal to 100% at end.

Types

type Axes

type Axes struct {
	*mobject.Group
	// contains filtered or unexported fields
}

Axes is a 2D coordinate plane: two perpendicular number lines with tick marks. Used as the parent of Graph mobjects.

func NewAxes

func NewAxes(xMin, xMax, yMin, yMax float64) *Axes

NewAxes constructs a 2D coordinate plane covering the given ranges. Pixel dimensions default to 800×500; override with WithSize.

func (*Axes) Bounds

func (a *Axes) Bounds() geometry.Rect

func (*Axes) MoveTo

func (a *Axes) MoveTo(x, y float64) *Axes

func (*Axes) Plot

func (a *Axes) Plot(fn func(float64) float64) *Graph

Plot constructs a Graph of f over the axes. Default range is the axes' xMin..xMax; override with Graph.WithRange.

func (*Axes) PointAt

func (a *Axes) PointAt(x, y float64) geometry.Point

PointAt converts a (math-space x, y) to a scene coordinate.

func (*Axes) Position

func (a *Axes) Position() (float64, float64)

func (*Axes) Render

func (a *Axes) Render(r render.Renderer, ctx style.Context)

func (*Axes) Reveal

func (a *Axes) Reveal() float64

func (*Axes) SetPosition

func (a *Axes) SetPosition(x, y float64)

func (*Axes) SetReveal

func (a *Axes) SetReveal(t float64)

func (*Axes) SetStyle

func (a *Axes) SetStyle(s style.Style)

func (*Axes) SetVisualScale

func (a *Axes) SetVisualScale(float64)

func (*Axes) Style

func (a *Axes) Style() *style.Style

func (*Axes) VisualBounds

func (a *Axes) VisualBounds() geometry.Rect

func (*Axes) WithGrid

func (a *Axes) WithGrid(show bool) *Axes

func (*Axes) WithLabels

func (a *Axes) WithLabels(show bool) *Axes

func (*Axes) WithSize

func (a *Axes) WithSize(w, h float64) *Axes

func (*Axes) WithSteps

func (a *Axes) WithSteps(xs, ys float64) *Axes

type Equation

type Equation struct {
	*mobject.Group
	// contains filtered or unexported fields
}

Equation is a math formula rendered from LaTeX source.

In Architect (crisp) mode, the equation is drawn as clean filled glyph paths. In Artist or Cartoonist mode, the same paths are stroked with the active sloppiness — producing the signature "handwritten math" look.

eq := mathx.NewEquation("E = mc^2").WithHeight(80)
scene.Add(eq)

Sub-symbols are addressable by index for fine-grained animation. Indices correspond to glyph order in the LaTeX source (left-to-right, no semantic awareness).

func NewEquation

func NewEquation(latexSrc string) *Equation

NewEquation constructs an equation from LaTeX source. Default height is 80px; override with WithHeight.

func (*Equation) Bounds

func (e *Equation) Bounds() geometry.Rect

Bounds returns the equation's bounding box at its current center.

func (*Equation) MoveTo

func (e *Equation) MoveTo(x, y float64) *Equation

MoveTo sets the equation's center.

func (*Equation) Position

func (e *Equation) Position() (float64, float64)

Position returns the equation's center.

func (*Equation) Render

func (e *Equation) Render(r render.Renderer, ctx style.Context)

Render draws the equation at its current position with the resolved style. In crisp mode each glyph is filled; in sketchy mode each glyph is stroked with the active roughness (handwritten math).

func (*Equation) Reveal

func (e *Equation) Reveal() float64

Reveal returns the current reveal fraction.

func (*Equation) SetPosition

func (e *Equation) SetPosition(x, y float64)

SetPosition is the imperative form of MoveTo.

func (*Equation) SetReveal

func (e *Equation) SetReveal(t float64)

SetReveal sets the reveal fraction (0..1). Used by Write and FadeIn/Out animations.

func (*Equation) SetStyle

func (e *Equation) SetStyle(s style.Style)

SetStyle replaces the per-mobject style.

func (*Equation) SetVisualScale

func (e *Equation) SetVisualScale(s float64)

SetVisualScale — equations expose VisualScale via SetReveal mapping (no separate scale support yet). Implementing the Scaler interface lets PopIn target equations.

func (*Equation) Source

func (e *Equation) Source() string

Source returns the original LaTeX string.

func (*Equation) Style

func (e *Equation) Style() *style.Style

Style returns the per-mobject style override.

func (*Equation) Submobjects

func (e *Equation) Submobjects() []*geometry.Path

Submobjects returns one path per visual glyph (approximately). Subpaths are clustered by bounding-box geometry so closed counters and accents stay merged with their letter body. Used by Write and TransformEquation animations.

func (*Equation) Symbol

func (e *Equation) Symbol(i int) *geometry.Path

Symbol returns the i-th glyph as a path. Returns nil if i is OOB.

func (*Equation) SymbolCount

func (e *Equation) SymbolCount() int

SymbolCount returns the number of addressable submobjects.

func (*Equation) WithColor

func (e *Equation) WithColor(c color.Color) *Equation

WithColor overrides the equation's stroke/fill color.

func (*Equation) WithHeight

func (e *Equation) WithHeight(h float64) *Equation

WithHeight sets the equation's pixel height and recompiles.

func (*Equation) WithStyle

func (e *Equation) WithStyle(s style.Style) *Equation

WithStyle sets the per-mobject style override.

type Graph

type Graph struct {
	*mobject.Group
	// contains filtered or unexported fields
}

Graph is a plotted function over an Axes. It samples the function at `samples` points across [xMin, xMax], smooths them into a cubic Bezier path via Catmull-Rom tangents, and renders with the active style.

Three behaviors keep the curve looking like part of the chart:

  • Boundary clipping interpolates to the actual yMin/yMax crossing instead of dropping the last in-range sample — so the curve meets the chart edge cleanly.
  • Discontinuities (Inf/NaN) start a new subpath, so the line breaks where the function is undefined rather than connecting across the jump.
  • When the active style is rough (Roughness > 0) the curve's Bezier control points are jittered perpendicular to the local segment, scaled by the style's MaxJitter. The path stays continuous (no per-segment MoveTo) so the curve reads as one drawn line, matching the rough axes around it.

func (*Graph) Bounds

func (g *Graph) Bounds() geometry.Rect

func (*Graph) Children

func (g *Graph) Children() []mobject.Mobject

func (*Graph) Render

func (g *Graph) Render(r render.Renderer, ctx style.Context)

func (*Graph) Reveal

func (g *Graph) Reveal() float64

func (*Graph) Seed

func (g *Graph) Seed() int64

func (*Graph) SetReveal

func (g *Graph) SetReveal(t float64)

func (*Graph) SetStyle

func (g *Graph) SetStyle(s style.Style)

func (*Graph) SetVisualScale

func (g *Graph) SetVisualScale(float64)

func (*Graph) Style

func (g *Graph) Style() *style.Style

func (*Graph) WithColor

func (g *Graph) WithColor(c color.Color) *Graph

func (*Graph) WithRange

func (g *Graph) WithRange(xMin, xMax float64) *Graph

func (*Graph) WithSamples

func (g *Graph) WithSamples(n int) *Graph

func (*Graph) WithSeed

func (g *Graph) WithSeed(s int64) *Graph

type NumberLine

type NumberLine struct {
	*mobject.Group
	// contains filtered or unexported fields
}

NumberLine renders a horizontal axis from min to max with tick marks at every `step` value and optional numeric labels under selected ticks.

func NewNumberLine

func NewNumberLine(min, max float64) *NumberLine

NewNumberLine constructs a number line from min to max with step=1.

func (*NumberLine) AddPoint

func (n *NumberLine) AddPoint(value float64, labelText string) geometry.Point

AddPoint marks a value with a filled dot and a label. Returns the dot's center so the caller can attach further elements.

func (*NumberLine) Bounds

func (n *NumberLine) Bounds() geometry.Rect

func (*NumberLine) MoveTo

func (n *NumberLine) MoveTo(x, y float64) *NumberLine

func (*NumberLine) PointAt

func (n *NumberLine) PointAt(value float64) geometry.Point

PointAt converts a value on the number line to a scene coordinate.

func (*NumberLine) Position

func (n *NumberLine) Position() (float64, float64)

func (*NumberLine) Render

func (n *NumberLine) Render(r render.Renderer, ctx style.Context)

func (*NumberLine) Reveal

func (n *NumberLine) Reveal() float64

func (*NumberLine) SetPosition

func (n *NumberLine) SetPosition(x, y float64)

func (*NumberLine) SetReveal

func (n *NumberLine) SetReveal(t float64)

func (*NumberLine) SetStyle

func (n *NumberLine) SetStyle(s style.Style)

func (*NumberLine) SetVisualScale

func (n *NumberLine) SetVisualScale(float64)

func (*NumberLine) Style

func (n *NumberLine) Style() *style.Style

func (*NumberLine) VisualBounds

func (n *NumberLine) VisualBounds() geometry.Rect

func (*NumberLine) WithLabels

func (n *NumberLine) WithLabels(values ...float64) *NumberLine

WithLabels picks specific values to receive numeric labels under their tick. If empty, every step gets a label.

func (*NumberLine) WithLength

func (n *NumberLine) WithLength(px float64) *NumberLine

func (*NumberLine) WithStep

func (n *NumberLine) WithStep(s float64) *NumberLine

type Shade

type Shade struct {
	*mobject.Group
	// contains filtered or unexported fields
}

Shade fills the region between a Graph curve and the axes' x-axis (or yMin, whichever is in range) across [xMin, xMax]. Used to mark areas under a probability distribution, integrals, etc.

Reveal grows the shaded region from left to right by clipping the x-range — useful for animating the tail of a bell curve filling in.

func NewShade

func NewShade(g *Graph, xMin, xMax float64) *Shade

NewShade builds a fill region under g across [xMin, xMax]. The region is bounded above by g.fn and below by y=0 (clamped to the axes' y-range).

func (*Shade) Bounds

func (s *Shade) Bounds() geometry.Rect

func (*Shade) Render

func (s *Shade) Render(r render.Renderer, ctx style.Context)

func (*Shade) Reveal

func (s *Shade) Reveal() float64

func (*Shade) Seed

func (s *Shade) Seed() int64

func (*Shade) SetReveal

func (s *Shade) SetReveal(t float64)

func (*Shade) SetStyle

func (s *Shade) SetStyle(st style.Style)

func (*Shade) SetVisualScale

func (s *Shade) SetVisualScale(float64)

func (*Shade) Style

func (s *Shade) Style() *style.Style

func (*Shade) WithSamples

func (s *Shade) WithSamples(n int) *Shade

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL