{"spec_id":"feynman-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// feynman-basic: Feynman Diagram for Particle Interactions\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n\nimport * as React from \"react\";\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst SIZE = window.ANYPLOT_SIZE;\nconst TITLE = \"feynman-basic · javascript · muix · anyplot.ai\";\nconst TIME_AXIS_LABEL = \"time\";\n\nconst FONT =\n  '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif';\n\nconst X_PAD_SHARE = 0.095;\nconst Y_TOP_PAD_SHARE = 0.62;\nconst Y_BOTTOM_PAD_SHARE = 0.72;\nconst TIME_AXIS_DROP_SHARE = 0.25;\nconst STYLE_KEY_DROP_SHARE = 0.54;\nconst PHOTON_WAVES = 6;\nconst PHOTON_AMP_PX = 12;\nconst GLUON_LOOPS = 8;\nconst GLUON_R_PX = 9;\nconst KEY_SWATCH_W_PX = 60;\nconst KEY_WAVES = 2.5;\nconst KEY_AMP_PX = 8;\n\nconst STYLE_KEY: { kind: \"fermion\" | \"photon\" | \"gluon\" | \"boson\"; label: string }[] = [\n  { kind: \"fermion\", label: \"Fermion (e⁻, q, …)\" },\n  { kind: \"photon\", label: \"Photon (γ)\" },\n  { kind: \"gluon\", label: \"Gluon (g)\" },\n  { kind: \"boson\", label: \"Scalar boson (H)\" },\n];\n\n// Data: replace this block with your own parameters\nconst VERTICES = [ // role: vertices\n  { id: \"eIn\", x: 8, y: 24, isInteraction: false },\n  { id: \"posIn\", x: 8, y: 0, isInteraction: false },\n  { id: \"v1\", x: 34, y: 12, isInteraction: true },\n  { id: \"v2\", x: 66, y: 12, isInteraction: true },\n  { id: \"muOut\", x: 92, y: 24, isInteraction: false },\n  { id: \"antiMuOut\", x: 92, y: 0, isInteraction: false },\n];\n\nconst PARTICLES: {\n  id: string;\n  type: \"fermion\" | \"photon\" | \"gluon\" | \"boson\";\n  label: string;\n  anti: boolean;\n}[] = [ // role: particles\n  { id: \"e-\", type: \"fermion\", label: \"e⁻\", anti: false },\n  { id: \"e+\", type: \"fermion\", label: \"e⁺\", anti: true },\n  { id: \"gamma\", type: \"photon\", label: \"γ*\", anti: false },\n  { id: \"mu-\", type: \"fermion\", label: \"μ⁻\", anti: false },\n  { id: \"mu+\", type: \"fermion\", label: \"μ⁺\", anti: true },\n];\n\nconst PROPAGATORS = [ // role: propagators\n  { fromVertex: \"eIn\", toVertex: \"v1\", particleId: \"e-\" },\n  { fromVertex: \"posIn\", toVertex: \"v1\", particleId: \"e+\" },\n  { fromVertex: \"v1\", toVertex: \"v2\", particleId: \"gamma\" },\n  { fromVertex: \"v2\", toVertex: \"muOut\", particleId: \"mu-\" },\n  { fromVertex: \"v2\", toVertex: \"antiMuOut\", particleId: \"mu+\" },\n];\n\n// Plot: reads only data and the constants above\nconst vertexMap = new Map(VERTICES.map((v) => [v.id, v]));\nconst particleMap = new Map(PARTICLES.map((p) => [p.id, p]));\n\nconst typeColorMap = new Map<string, string>();\nSTYLE_KEY.forEach((entry, idx) => {\n  typeColorMap.set(entry.kind, t.palette[idx]);\n});\n\nconst vxValues = VERTICES.map((v) => v.x);\nconst vyValues = VERTICES.map((v) => v.y);\nconst minVx = Math.min(...vxValues);\nconst maxVx = Math.max(...vxValues);\nconst minVy = Math.min(...vyValues);\nconst maxVy = Math.max(...vyValues);\nconst vxSpan = maxVx - minVx;\nconst vySpan = maxVy - minVy;\nconst midVx = (minVx + maxVx) / 2;\n\nconst xDomainMin = minVx - vxSpan * X_PAD_SHARE;\nconst xDomainMax = maxVx + vxSpan * X_PAD_SHARE;\nconst yDomainMin = minVy - vySpan * Y_BOTTOM_PAD_SHARE;\nconst yDomainMax = maxVy + vySpan * Y_TOP_PAD_SHARE;\n\nconst timeAxisY = minVy - vySpan * TIME_AXIS_DROP_SHARE;\nconst styleKeyY = minVy - vySpan * STYLE_KEY_DROP_SHARE;\n\nconst interactionVertices = VERTICES.filter((v) => v.isInteraction);\n\nfunction FeynmanCanvas() {\n  const xs = useXScale() as (v: number) => number;\n  const ys = useYScale() as (v: number) => number;\n\n  const fermionColor = typeColorMap.get(\"fermion\")!;\n\n  return (\n    <g fontFamily={FONT}>\n      <defs>\n        <marker\n          id=\"arrow-fermion\"\n          markerWidth=\"22\"\n          markerHeight=\"20\"\n          refX=\"14\"\n          refY=\"10\"\n          orient=\"auto\"\n          markerUnits=\"userSpaceOnUse\"\n        >\n          <path d=\"M0,0 L22,10 L0,20 Z\" fill={fermionColor} />\n        </marker>\n        <marker\n          id=\"arrow-axis\"\n          markerWidth=\"9\"\n          markerHeight=\"9\"\n          refX=\"7\"\n          refY=\"4.5\"\n          orient=\"auto\"\n          markerUnits=\"userSpaceOnUse\"\n        >\n          <path d=\"M0,0 L9,4.5 L0,9 Z\" fill={t.inkSoft} />\n        </marker>\n      </defs>\n\n      {/* Propagator lines and particle symbol labels */}\n      {PROPAGATORS.map((prop, i) => {\n        const fromV = vertexMap.get(prop.fromVertex)!;\n        const toV = vertexMap.get(prop.toVertex)!;\n        const particle = particleMap.get(prop.particleId)!;\n        const color = typeColorMap.get(particle.type)!;\n\n        const x1 = xs(fromV.x);\n        const y1 = ys(fromV.y);\n        const x2 = xs(toV.x);\n        const y2 = ys(toV.y);\n        const mx = (x1 + x2) / 2;\n        const my = (y1 + y2) / 2;\n\n        let labelX = mx;\n        let labelY = my - 22;\n        let labelAnchor: \"start\" | \"middle\" | \"end\" = \"middle\";\n        if (!fromV.isInteraction) {\n          labelX = x1 - 14;\n          labelY = y1 + (fromV.y > toV.y ? -10 : 24);\n          labelAnchor = \"end\";\n        } else if (!toV.isInteraction) {\n          labelX = x2 + 14;\n          labelY = y2 + (toV.y > fromV.y ? -10 : 24);\n          labelAnchor = \"start\";\n        }\n\n        const dx = x2 - x1;\n        const dy = y2 - y1;\n        const len = Math.hypot(dx, dy);\n        const ux = dx / len;\n        const uy = dy / len;\n        const px = -uy;\n        const py = ux;\n\n        let lineEl: React.ReactNode;\n        if (particle.type === \"photon\") {\n          const steps = Math.round(PHOTON_WAVES * 22);\n          let d = \"\";\n          for (let s = 0; s <= steps; s++) {\n            const u = s / steps;\n            const offset = PHOTON_AMP_PX * Math.sin(u * PHOTON_WAVES * 2 * Math.PI);\n            const wx = x1 + ux * u * len + px * offset;\n            const wy = y1 + uy * u * len + py * offset;\n            d += `${s === 0 ? \"M\" : \"L\"} ${wx.toFixed(1)} ${wy.toFixed(1)} `;\n          }\n          lineEl = <path d={d} stroke={color} strokeWidth={4.5} fill=\"none\" />;\n        } else if (particle.type === \"gluon\") {\n          const steps = Math.round(GLUON_LOOPS * 24);\n          let d = \"\";\n          for (let s = 0; s <= steps; s++) {\n            const u = s / steps;\n            const theta = u * GLUON_LOOPS * 2 * Math.PI;\n            const along = u * len + GLUON_R_PX * 0.72 * Math.sin(theta);\n            const perp = -GLUON_R_PX * Math.cos(theta);\n            const gx = x1 + ux * along + px * perp;\n            const gy = y1 + uy * along + py * perp;\n            d += `${s === 0 ? \"M\" : \"L\"} ${gx.toFixed(1)} ${gy.toFixed(1)} `;\n          }\n          lineEl = <path d={d} stroke={color} strokeWidth={3.6} fill=\"none\" />;\n        } else if (particle.type === \"boson\") {\n          lineEl = (\n            <line\n              x1={x1}\n              y1={y1}\n              x2={x2}\n              y2={y2}\n              stroke={color}\n              strokeWidth={4.5}\n              strokeDasharray=\"12,7\"\n              strokeLinecap=\"round\"\n            />\n          );\n        } else {\n          const marked = particle.anti\n            ? { ax: x2, ay: y2, bx: x1, by: y1 }\n            : { ax: x1, ay: y1, bx: x2, by: y2 };\n          lineEl = (\n            <g stroke={color} strokeWidth={4.5} strokeLinecap=\"round\">\n              <line x1={marked.ax} y1={marked.ay} x2={mx} y2={my} markerEnd=\"url(#arrow-fermion)\" />\n              <line x1={marked.bx} y1={marked.by} x2={mx} y2={my} />\n            </g>\n          );\n        }\n\n        return (\n          <g key={i}>\n            {lineEl}\n            <text\n              x={labelX}\n              y={labelY}\n              textAnchor={labelAnchor}\n              fontSize={24}\n              fontWeight={700}\n              fill={color}\n            >\n              {particle.label}\n            </text>\n          </g>\n        );\n      })}\n\n      {/* Interaction vertex dots */}\n      {interactionVertices.map((v) => (\n        <circle\n          key={v.id}\n          cx={xs(v.x)}\n          cy={ys(v.y)}\n          r={9}\n          fill={t.ink}\n        />\n      ))}\n\n      {/* Time direction axis */}\n      <line\n        x1={xs(minVx)}\n        y1={ys(timeAxisY)}\n        x2={xs(maxVx)}\n        y2={ys(timeAxisY)}\n        stroke={t.inkSoft}\n        strokeWidth={1.5}\n        markerEnd=\"url(#arrow-axis)\"\n      />\n      <text x={xs(maxVx) + 14} y={ys(timeAxisY) + 5} fontSize={16} fill={t.inkSoft}>\n        {TIME_AXIS_LABEL}\n      </text>\n\n      {/* Four-type Feynman propagator style key */}\n      {STYLE_KEY.map((item, idx) => {\n        const { kind, label } = item;\n        const color = typeColorMap.get(kind)!;\n        const xFrac = minVx + vxSpan * 0.02 + (vxSpan * 0.86 * idx) / (STYLE_KEY.length - 1);\n        const px = xs(xFrac);\n        const py = ys(styleKeyY);\n        const x2 = px + KEY_SWATCH_W_PX;\n\n        let swatchEl: React.ReactNode;\n        if (kind === \"fermion\") {\n          swatchEl = (\n            <line\n              x1={px}\n              y1={py}\n              x2={x2}\n              y2={py}\n              stroke={color}\n              strokeWidth={4}\n              markerEnd=\"url(#arrow-fermion)\"\n            />\n          );\n        } else if (kind === \"photon\") {\n          const steps = Math.round(KEY_WAVES * 22);\n          let d = \"\";\n          for (let s = 0; s <= steps; s++) {\n            const u = s / steps;\n            const wx = px + u * KEY_SWATCH_W_PX;\n            const wy = py + KEY_AMP_PX * Math.sin(u * KEY_WAVES * 2 * Math.PI);\n            d += `${s === 0 ? \"M\" : \"L\"} ${wx.toFixed(1)} ${wy.toFixed(1)} `;\n          }\n          swatchEl = <path d={d} stroke={color} strokeWidth={4} fill=\"none\" />;\n        } else if (kind === \"gluon\") {\n          const loops = 4;\n          const steps = 96;\n          const rLoop = 7.5;\n          let d = \"\";\n          for (let s = 0; s <= steps; s++) {\n            const u = s / steps;\n            const theta = u * loops * 2 * Math.PI;\n            const gx = px + u * KEY_SWATCH_W_PX + rLoop * 0.72 * Math.sin(theta);\n            const gy = py - rLoop * Math.cos(theta);\n            d += `${s === 0 ? \"M\" : \"L\"} ${gx.toFixed(1)} ${gy.toFixed(1)} `;\n          }\n          swatchEl = <path d={d} stroke={color} strokeWidth={3.2} fill=\"none\" />;\n        } else {\n          swatchEl = (\n            <line\n              x1={px}\n              y1={py}\n              x2={x2}\n              y2={py}\n              stroke={color}\n              strokeWidth={4}\n              strokeDasharray=\"12,7\"\n            />\n          );\n        }\n\n        return (\n          <g key={kind}>\n            {swatchEl}\n            <text x={px} y={py + 26} fontSize={15} fill={t.inkSoft}>\n              {label}\n            </text>\n          </g>\n        );\n      })}\n\n      {/* Title */}\n      <text\n        x={xs(midVx)}\n        y={ys(maxVy + vySpan * 0.5)}\n        textAnchor=\"middle\"\n        fontSize={22}\n        fontWeight={600}\n        fill={t.ink}\n      >\n        {TITLE}\n      </text>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  return (\n    <div style={{ width: SIZE.width, height: SIZE.height, backgroundColor: t.pageBg }}>\n      <ChartsContainer\n        width={SIZE.width}\n        height={SIZE.height}\n        margin={{ top: 12, right: 12, bottom: 12, left: 12 }}\n        series={[]}\n        xAxis={[{ id: \"x\", scaleType: \"linear\", min: xDomainMin, max: xDomainMax, position: \"none\" }]}\n        yAxis={[{ id: \"y\", scaleType: \"linear\", min: yDomainMin, max: yDomainMax, position: \"none\" }]}\n        skipAnimation\n      >\n        <FeynmanCanvas />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}