{"spec_id":"feynman-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' feynman-basic: Feynman Diagram for Particle Interactions\n#' Library: ggplot2 4.0.3 | R 4.6.1\n#' Quality: 86/100 | Updated: 2026-12-01\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(tibble)\nlibrary(ragg)\n\n# --- Theme tokens -----------------------------------------------------------\nTHEME       <- Sys.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG     <- if (THEME == \"light\") \"#FAF8F1\" else \"#1A1A17\"\nINK         <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT    <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\nINK_MUTED   <- if (THEME == \"light\") \"#6B6A63\" else \"#A8A79F\"\n\n# Imprint palette — semantic assignments for all 4 particle types\nCOL_FERMION <- \"#009E73\"  # pos 1 — quarks/fermions (green)\nCOL_BOSON   <- \"#C475FD\"  # pos 2 — scalar Higgs boson (lavender)\nCOL_PHOTON  <- \"#4467A3\"  # pos 3 — photons (blue, EM radiation)\nCOL_GLUON   <- \"#BD8233\"  # pos 4 — gluons (ochre, QCD)\n\n# --- Helper: wavy photon line (sinusoidal, perpendicular to path) -----------\nwavy_line <- function(x0, y0, x1, y1, n_waves = 3, amplitude = 0.35, n_pts = 300) {\n  t   <- seq(0, 1, length.out = n_pts)\n  dx  <- x1 - x0;  dy  <- y1 - y0\n  len <- sqrt(dx^2 + dy^2)\n  px  <- -dy / len; py  <-  dx / len\n  wave <- amplitude * sin(n_waves * 2 * pi * t)\n  tibble(x = x0 + t * dx + wave * px,\n         y = y0 + t * dy + wave * py)\n}\n\n# --- Helper: curly gluon line (prolate cycloid — closed looped coils) ------\ngluon_line <- function(x0, y0, x1, y1, n_loops = 5, radius = 0.3, n_pts = 600) {\n  t   <- seq(0, 1, length.out = n_pts)\n  dx  <- x1 - x0;  dy  <- y1 - y0\n  len <- sqrt(dx^2 + dy^2)\n  ux  <- dx / len; uy  <- dy / len\n  px  <- -uy;      py  <- ux\n  phi    <- n_loops * 2 * pi * t\n  along  <- t * len - radius * 1.6 * sin(phi)\n  across <- radius * 1.6 * (1 - cos(phi)) / 2 * 1.4\n  tibble(x = x0 + along * ux + across * px,\n         y = y0 + along * uy + across * py)\n}\n\n# --- Diagram: gg -> H -> gamma gamma (Higgs via gluon fusion) ---------------\n# Coordinate space: x in [0, 16], y in [0, 9]  (16:9 aspect ratio)\n#\n# Two curly gluons enter a top-quark triangle (left), a dashed Higgs line joins\n# it to a second top-quark triangle (right), and two wavy photons leave it.\n# 6 vertices and 11 lines, all four line types in one diagram.\n\nVT <- c(4.6, 6.0)    # left triangle: top gluon vertex\nVB <- c(4.6, 3.0)    # left triangle: bottom gluon vertex\nVR <- c(7.4, 4.5)    # left triangle: Higgs emission vertex\nVH <- c(10.6, 4.5)   # right triangle: Higgs absorption vertex\nPT <- c(13.4, 6.0)   # right triangle: top photon vertex\nPB <- c(13.4, 3.0)   # right triangle: bottom photon vertex\n\n# Gluon lines (curly, ochre) — incoming from left\ngluons <- bind_rows(\n  gluon_line(0.8, 7.4, VT[1], VT[2], n_loops = 5) |> mutate(group = \"g_top\"),\n  gluon_line(VB[1], VB[2], 0.8, 1.6, n_loops = 5) |> mutate(group = \"g_bot\")\n)\n\n# Fermion (top quark) loops — each side runs around its triangle in one direction\nquark_loops <- tibble(\n  x    = c(VT[1], VR[1], VB[1], PT[1], PB[1], VH[1]),\n  y    = c(VT[2], VR[2], VB[2], PT[2], PB[2], VH[2]),\n  xend = c(VR[1], VB[1], VT[1], PB[1], VH[1], PT[1]),\n  yend = c(VR[2], VB[2], VT[2], PB[2], VH[2], PT[2])\n)\n\n# Arrowheads sit mid-side so the direction reads unbroken through every vertex\nquark_arrows <- quark_loops |>\n  mutate(mx = (x + xend) / 2, my = (y + yend) / 2,\n         ax = mx - (xend - x) * 0.02, ay = my - (yend - y) * 0.02,\n         axend = mx + (xend - x) * 0.02, ayend = my + (yend - y) * 0.02)\n\n# Higgs propagator (dashed, lavender) — between the two triangles\nhiggs_seg <- tibble(x = VR[1], y = VR[2], xend = VH[1], yend = VH[2])\n\n# Photon lines (wavy, blue) — leave the right triangle\nphotons <- bind_rows(\n  wavy_line(PT[1], PT[2], 15.6, 7.6, n_waves = 3) |> mutate(group = \"gamma_top\"),\n  wavy_line(PB[1], PB[2], 15.6, 1.4, n_waves = 3) |> mutate(group = \"gamma_bot\")\n)\n\n# All six interaction vertices\nvertices <- tibble(\n  x = c(VT[1], VB[1], VR[1], VH[1], PT[1], PB[1]),\n  y = c(VT[2], VB[2], VR[2], VH[2], PT[2], PB[2])\n)\n\n# Particle labels — colored to match their line type\nlabels_df <- tibble(\n  x     = c(0.5,  0.5,  9.0,  15.75, 15.75, 6.2, 6.2, 4.15, 11.8, 11.8, 13.85),\n  y     = c(7.6,  1.4,  4.75, 7.8,   1.2,   5.7, 3.3, 4.5,  5.7,  3.3,  4.5),\n  label = c(\"g\",  \"g\",  \"H\",  \"γ\",   \"γ\",   \"t\", \"t\", \"t\",  \"t\",  \"t\",  \"t\"),\n  hjust = c(1.0,  1.0,  0.5,  0.0,   0.0,   0.5, 0.5, 0.5,  0.5,  0.5,  0.5),\n  vjust = c(0.5,  0.5,  0.0,  0.5,   0.5,   0.5, 0.5, 0.5,  0.5,  0.5,  0.5),\n  col   = c(rep(COL_GLUON, 2), COL_BOSON, rep(COL_PHOTON, 2), rep(COL_FERMION, 6))\n)\n\n# --- Title ------------------------------------------------------------------\nplot_title    <- \"feynman-basic · r · ggplot2 · anyplot.ai\"\nplot_subtitle <- \"Higgs Boson Production via Gluon Fusion:  gg → H → γγ\"\n\n# --- Build plot -------------------------------------------------------------\np <- ggplot() +\n  # Gluon curly lines (ochre) — two incoming gluons\n  geom_path(data = gluons, aes(x = x, y = y, group = group),\n            color = COL_GLUON, linewidth = 1.0) +\n  # Top-quark fermion loops — solid triangle sides (green)\n  geom_segment(data = quark_loops,\n               aes(x = x, y = y, xend = xend, yend = yend),\n               color = COL_FERMION, linewidth = 1.0) +\n  # Mid-side arrowheads giving the loop direction\n  geom_segment(data = quark_arrows,\n               aes(x = ax, y = ay, xend = axend, yend = ayend),\n               color = COL_FERMION, linewidth = 1.0,\n               arrow = arrow(length = unit(0.22, \"cm\"), type = \"closed\", ends = \"last\")) +\n  # Higgs scalar propagator (lavender dashed)\n  geom_segment(data = higgs_seg, aes(x = x, y = y, xend = xend, yend = yend),\n               color = COL_BOSON, linewidth = 1.2, linetype = \"dashed\") +\n  # Photon wavy lines (blue) — two outgoing photons from H decay\n  geom_path(data = photons, aes(x = x, y = y, group = group),\n            color = COL_PHOTON, linewidth = 1.0) +\n  # Interaction vertex dots (theme-adaptive INK color)\n  geom_point(data = vertices, aes(x = x, y = y),\n             color = INK, size = 3.5, shape = 16) +\n  # Particle labels — colored italic, using scale_color_identity()\n  geom_text(data = labels_df,\n            aes(x = x, y = y, label = label,\n                hjust = hjust, vjust = vjust, color = col),\n            size = 5.5, fontface = \"italic\") +\n  scale_color_identity() +\n  # Time direction arrow at bottom\n  annotate(\"segment\", x = 5.5, xend = 10.5, y = 0.35, yend = 0.35,\n           color = INK_MUTED, linewidth = 0.7,\n           arrow = arrow(length = unit(0.15, \"cm\"), type = \"open\")) +\n  annotate(\"text\", x = 8.0, y = 0.02, label = \"time\",\n           color = INK_MUTED, size = 4.0, hjust = 0.5) +\n  coord_cartesian(xlim = c(-0.5, 16.5), ylim = c(-0.2, 9.3)) +\n  labs(title = plot_title, subtitle = plot_subtitle) +\n  theme_void() +\n  theme(\n    plot.background = element_rect(fill = PAGE_BG, color = PAGE_BG),\n    plot.title      = element_text(color = INK,      size = 12, hjust = 0.5,\n                                   margin = margin(t = 15, b = 5)),\n    plot.subtitle   = element_text(color = INK_SOFT, size = 9,  hjust = 0.5,\n                                   margin = margin(b = 10)),\n    plot.margin     = margin(t = 10, r = 30, b = 20, l = 30)\n  )\n\n# --- Save -------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 8,\n  height   = 4.5,\n  units    = \"in\",\n  dpi      = 400\n)\n"}