{"spec_id":"feynman-basic","library":"makie","language":"julia","code":"# anyplot.ai\n# feynman-basic: Feynman Diagram for Particle Interactions\n# Library: makie 0.24.16 | Julia 1.13.4\n# Quality: 86/100 | Updated: 2026-12-01\n\nusing CairoMakie\nusing Colors\n\n\nconst THEME     = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG   = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst INK       = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT  = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nconst INK_MUTED = THEME == \"light\" ? colorant\"#6B6A63\" : colorant\"#A8A79F\"\n\nconst FERMION_COLOR = colorant\"#009E73\"  # brand green  (e-, e+, μ-, μ+, quarks)\nconst GLUON_COLOR   = colorant\"#C475FD\"  # lavender     (gluons / QCD)\nconst PHOTON_COLOR  = colorant\"#4467A3\"  # blue         (photons / EM bosons)\nconst SCALAR_COLOR  = colorant\"#BD8233\"  # ochre        (scalar bosons / Higgs)\n\n# ─── Makie @recipe: composable Feynman propagator plot types ─────────────────\n# Each recipe becomes a reusable, composable CairoMakie primitive — a Makie-\n# exclusive feature that makes the diagram grammar extensible and domain-specific.\n\n# Filled triangle centred on (x, y) pointing along the unit vector (ux, uy)\nfunction arrowhead(x, y, ux, uy, size)\n    nx, ny = -uy, ux\n    [Point2f(x + ux * size * 0.6, y + uy * size * 0.6),\n     Point2f(x - ux * size * 0.4 + nx * size * 0.4, y - uy * size * 0.4 + ny * size * 0.4),\n     Point2f(x - ux * size * 0.4 - nx * size * 0.4, y - uy * size * 0.4 - ny * size * 0.4)]\nend\n\n# Fermion propagator: solid line with a midpoint direction arrow\n@recipe(FermionProp, xs, ys, direction) do scene\n    Attributes(\n        color     = FERMION_COLOR,\n        linewidth = 2.5,\n        arrowsize = 0.32,\n    )\nend\n\nfunction Makie.plot!(fp::FermionProp)\n    xs        = fp[1][]\n    ys        = fp[2][]\n    direction = fp[3][]   # 1 = particle (forward), -1 = antiparticle (backward)\n    clr       = fp.color[]\n    lw        = fp.linewidth[]\n\n    lines!(fp, xs, ys; color = clr, linewidth = lw)\n\n    n   = length(xs)\n    mid = n ÷ 2\n    dx  = xs[min(mid + 1, n)] - xs[max(mid - 1, 1)]\n    dy  = ys[min(mid + 1, n)] - ys[max(mid - 1, 1)]\n    len = sqrt(dx^2 + dy^2)\n    if len > 1e-9\n        poly!(fp, arrowhead(xs[mid], ys[mid], direction * dx / len, direction * dy / len, fp.arrowsize[]);\n              color = clr)\n    end\n    fp\nend\n\n# Photon propagator: transverse sinusoidal wave\n@recipe(PhotonProp, x1, y1, x2, y2) do scene\n    Attributes(\n        color     = PHOTON_COLOR,\n        linewidth = 2.5,\n        n_waves   = 9,\n        amplitude = 0.038,\n    )\nend\n\nfunction Makie.plot!(pp::PhotonProp)\n    x1, y1 = pp[1][], pp[2][]\n    x2, y2 = pp[3][], pp[4][]\n    clr     = pp.color[]\n    lw      = pp.linewidth[]\n    nw      = pp.n_waves[]\n    amp     = pp.amplitude[]\n\n    dx  = x2 - x1; dy = y2 - y1\n    len = sqrt(dx^2 + dy^2)\n    nx  = -dy / len; ny = dx / len   # unit normal (transverse direction)\n\n    t    = LinRange(0.0, 1.0, 600)\n    xs_w = @. x1 + t * dx + amp * sin(nw * 2π * t) * nx\n    ys_w = @. y1 + t * dy + amp * sin(nw * 2π * t) * ny\n    lines!(pp, xs_w, ys_w; color = clr, linewidth = lw)\n    pp\nend\n\n# Gluon propagator: one-sided curly bumps via |sin| — visually distinct from photon\n@recipe(GluonProp, x1, y1, x2, y2) do scene\n    Attributes(\n        color     = GLUON_COLOR,\n        linewidth = 2.5,\n        n_loops   = 7,\n        amplitude = 0.048,\n    )\nend\n\nfunction Makie.plot!(gp::GluonProp)\n    x1, y1 = gp[1][], gp[2][]\n    x2, y2 = gp[3][], gp[4][]\n    clr     = gp.color[]\n    lw      = gp.linewidth[]\n    nl      = gp.n_loops[]\n    amp     = gp.amplitude[]\n\n    dx  = x2 - x1; dy = y2 - y1\n    len = sqrt(dx^2 + dy^2)\n    nx  = -dy / len; ny = dx / len\n\n    # Trochoid along the line: the forward position swings back each turn, so the curve\n    # crosses itself and draws a looped coil; both ends land exactly on the line.\n    pitch = len / nl\n    t     = LinRange(0.0, 1.0, 800)\n    phase = @. nl * 2π * t\n    along = @. t * len - 0.26 * pitch * sin(phase)\n    side  = @. amp * (1 - cos(phase)) / 2\n    xs_c  = @. x1 + along * dx / len + side * nx\n    ys_c  = @. y1 + along * dy / len + side * ny\n    lines!(gp, xs_c, ys_c; color = clr, linewidth = lw)\n    gp\nend\n\n# Scalar boson propagator: dashed line (Higgs, etc.)\n@recipe(ScalarProp, x1, y1, x2, y2) do scene\n    Attributes(\n        color     = SCALAR_COLOR,\n        linewidth = 2.5,\n    )\nend\n\nfunction Makie.plot!(sp::ScalarProp)\n    x1, y1 = sp[1][], sp[2][]\n    x2, y2 = sp[3][], sp[4][]\n    lines!(sp, [x1, x2], [y1, y2]; color = sp.color[], linewidth = sp.linewidth[], linestyle = :dash)\n    sp\nend\n\n# ─── Data: gg → H → γγ (all four line types in one diagram) ─────────────────\n# Schematic layout in a 16 x 9 box; only which lines meet at which vertex matters.\nvertices = Dict(\n    \"A\" => (4.2, 6.0), \"B\" => (4.2, 3.0), \"C\" => (6.4, 4.5),    # top-quark triangle 1\n    \"D\" => (9.6, 4.5), \"E\" => (11.8, 6.0), \"F\" => (11.8, 3.0),  # top-quark triangle 2\n)\ng1_in, g2_in   = (0.8, 7.4), (0.8, 1.6)    # incoming gluons\np1_out, p2_out = (15.2, 7.4), (15.2, 1.6)  # outgoing photons\n\n# Top-quark loop: arrows run around each triangle in one direction (clockwise)\nloops = [(\"A\", \"C\"), (\"C\", \"B\"), (\"B\", \"A\"), (\"D\", \"E\"), (\"E\", \"F\"), (\"F\", \"D\")]\n\n# ─── Plot ────────────────────────────────────────────────────────────────────\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nLabel(fig[0, 1], \"feynman-basic · julia · makie · anyplot.ai\";\n      fontsize = 20, color = INK, halign = :center)\n\nax = Axis(fig[1, 1];\n    backgroundcolor    = PAGE_BG,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinevisible   = false,\n    bottomspinevisible = false,\n    xgridvisible       = false,\n    ygridvisible       = false,\n    xticksvisible      = false,\n    yticksvisible      = false,\n    xticklabelsvisible = false,\n    yticklabelsvisible = false,\n)\nxlims!(ax, 0.0, 16.0)\nylims!(ax, 0.0, 9.0)\n\nn_pts = 100\nfor (a, b) in loops\n    (xa, ya), (xb, yb) = vertices[a], vertices[b]\n    fermionprop!(ax, LinRange(xa, xb, n_pts), LinRange(ya, yb, n_pts), 1)\nend\n\ngluonprop!(ax, g1_in..., vertices[\"A\"]...; n_loops = 7, amplitude = 0.4)\ngluonprop!(ax, g2_in..., vertices[\"B\"]...; n_loops = 7, amplitude = 0.4)\nscalarprop!(ax, vertices[\"C\"]..., vertices[\"D\"]...)\nphotonprop!(ax, vertices[\"E\"]..., p1_out...; n_waves = 5, amplitude = 0.2)\nphotonprop!(ax, vertices[\"F\"]..., p2_out...; n_waves = 5, amplitude = 0.2)\n\nvx = [v[1] for v in values(vertices)]\nvy = [v[2] for v in values(vertices)]\nscatter!(ax, vx, vy; color = INK, markersize = 14, strokewidth = 0)\n\ntext!(ax, 2.3, 7.35; text = \"g\", fontsize = 30, color = GLUON_COLOR, align = (:center, :bottom))\ntext!(ax, 2.3, 1.75; text = \"g\", fontsize = 30, color = GLUON_COLOR, align = (:center, :top))\ntext!(ax, 8.0, 4.85; text = \"H\", fontsize = 30, color = SCALAR_COLOR, align = (:center, :bottom))\ntext!(ax, 13.4, 7.4; text = \"γ\", fontsize = 30, color = PHOTON_COLOR, align = (:center, :bottom))\ntext!(ax, 13.4, 1.7; text = \"γ\", fontsize = 30, color = PHOTON_COLOR, align = (:center, :top))\nfor (x, y, al) in [(5.65, 5.6, (:left, :bottom)), (5.65, 3.4, (:left, :top)), (3.95, 4.5, (:right, :center)),\n                   (10.35, 5.6, (:right, :bottom)), (10.35, 3.4, (:right, :top)), (12.05, 4.5, (:left, :center))]\n    text!(ax, x, y; text = \"t\", fontsize = 28, color = FERMION_COLOR, align = al)\nend\n\n# Time axis\nlines!(ax, [0.5, 2.0], [0.6, 0.6]; color = INK_MUTED, linewidth = 1.5)\npoly!(ax, arrowhead(2.0, 0.6, 1.0, 0.0, 0.3); color = INK_MUTED)\ntext!(ax, 2.35, 0.6; text = \"time\", fontsize = 20, color = INK_MUTED, align = (:left, :center))\n\n# Line-style key\nkey_x = [4.6, 7.4, 10.2, 13.0]\nkey_names = [\"Fermion\", \"Photon\", \"Gluon\", \"Scalar\"]\nkey_colors = [FERMION_COLOR, PHOTON_COLOR, GLUON_COLOR, SCALAR_COLOR]\nky = 0.6\nfermionprop!(ax, LinRange(key_x[1], key_x[1] + 1.0, n_pts), fill(ky, n_pts), 1; arrowsize = 0.32)\nphotonprop!(ax, key_x[2], ky, key_x[2] + 1.0, ky; n_waves = 3, amplitude = 0.12)\ngluonprop!(ax, key_x[3], ky, key_x[3] + 1.0, ky; n_loops = 4, amplitude = 0.2)\nscalarprop!(ax, key_x[4], ky, key_x[4] + 1.0, ky)\nfor (x, name, clr) in zip(key_x, key_names, key_colors)\n    text!(ax, x + 1.2, ky; text = name, fontsize = 20, color = clr, align = (:left, :center))\nend\n\ncolsize!(fig.layout, 1, Relative(1))\n\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}