{
 "id": "voronoi-lloyd-relaxation",
 "title": "Voronoi Diagrams and Lloyd Relaxation",
 "aliases": [
  "centroidal Voronoi tessellation",
  "CVT",
  "Lloyd's algorithm"
 ],
 "domain": "procgen",
 "subdomain": "point distributions",
 "stage": "procedural",
 "realtime": "interactive",
 "platforms": [
  "cpu",
  "python",
  "webgl2",
  "houdini",
  "blender",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 1982,
 "inputs": [
  "point-cloud"
 ],
 "outputs": [
  "mesh",
  "graph",
  "layout"
 ],
 "style": "both",
 "dynamic": "static",
 "summary": "Partition space by nearest seed (Voronoi), then repeatedly move each seed to the centroid of its cell. The cells become evenly sized and well-shaped, ideal for regions, provinces and tessellations.",
 "how_it_works": "Given seeds, the Voronoi diagram gives each seed the region of points closer to it than to any other; its dual is the Delaunay triangulation, computable in O(n log n) with Fortune's sweep or incremental Delaunay. Lloyd's iteration computes each cell's centroid (area-weighted, optionally with a density function) and moves the seed there, then recomputes the diagram. Each step reduces a quantisation energy and the process converges to a centroidal Voronoi tessellation (CVT) with hexagon-like cells in 2D. A few iterations (2 to 5) suffice for visually even cells without losing all randomness. Clipping the diagram to a boundary needs care at the border.",
 "when_to_use": "Choose it for map regions, cracks and fragment shapes, cell-based terrain and mesh point distributions where regular but irregular-looking cells are wanted. Prefer `poisson-disk-sampling` when you only need points, `worley-noise` for a per-pixel Voronoi texture with no explicit mesh, and `wang-tiles` for aperiodic tilings with stored tiles.",
 "cost": {
  "class": "cheap",
  "notes": "One diagram construction per iteration; n log n each."
 },
 "failure_modes": [
  "Too many iterations converge to a uniform hex grid that looks artificial; stop early",
  "Cells at domain boundaries are unbounded or clipped asymmetrically, so seeds drift outward",
  "Degenerate configurations (co-circular points) create zero-length edges and numerical instabilities",
  "Density-weighted CVT needs numeric integration, which is slow and noisy for sharp density changes"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1109/tit.1982.1056489",
   "type": "paper",
   "title": "Least squares quantization in PCM",
   "authors": [
    "S. Lloyd"
   ],
   "year": 1982,
   "venue": "IEEE Transactions on Information Theory",
   "url": "https://doi.org/10.1109/tit.1982.1056489",
   "ids": {
    "doi": "10.1109/tit.1982.1056489"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1109/tit.1982.1056489",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1109/tit.1982.1056489",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1109/tit.1982.1056489",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1109/tit.1982.1056489",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1109/tit.1982.1056489",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "lloyd1982least"
  },
  {
   "role": "primary",
   "key": "doi:10.1137/s0036144599352836",
   "type": "paper",
   "title": "Centroidal Voronoi Tessellations: Applications and Algorithms",
   "authors": [
    "Qiang Du",
    "Vance Faber",
    "Max Gunzburger"
   ],
   "year": 1999,
   "venue": "SIAM Review",
   "url": "https://doi.org/10.1137/s0036144599352836",
   "ids": {
    "doi": "10.1137/s0036144599352836"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1137/s0036144599352836",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1137/s0036144599352836",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1137/s0036144599352836",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1137/s0036144599352836",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1137/s0036144599352836",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "du1999centroidal"
  },
  {
   "role": "implementation",
   "key": "gh:d3/d3-delaunay",
   "type": "code",
   "title": "d3/d3-delaunay",
   "description": "Compute the Voronoi diagram of a set of two-dimensional points.",
   "authors": [
    "d3"
   ],
   "year": 2018,
   "venue": "GitHub",
   "url": "https://github.com/d3/d3-delaunay",
   "ids": {
    "repo": "d3/d3-delaunay"
   },
   "license": "ISC",
   "extra": {
    "stars": 668,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-04T19:21:54Z",
    "language": "JavaScript",
    "homepage": "https://d3js.org/d3-delaunay",
    "license_name": "ISC License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/d3/d3-delaunay",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/d3/d3-delaunay",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/d3/d3-delaunay",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/d3/d3-delaunay",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/d3/d3-delaunay",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/d3/d3-delaunay",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "d2018delaunay"
  }
 ],
 "presets": [
  {
   "id": "voronoi-lloyd-relaxation-specimen",
   "title": "Voronoi cells of random sites (left) vs one Lloyd relaxation step (right)",
   "runtime": "glsl-fragment",
   "params": {
    "CELLS": 7,
    "SAMP": 6
   },
   "code": "// Voronoi of jittered sites: left = raw random sites, right = after one Lloyd step (sites moved to their cell centroids, sampled).\nconst float CELLS = 7.0;\nconst int SAMP = 6;\nvec2 hash2(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453); }\nvec2 site0(vec2 c) { return c + 0.08 + 0.84 * hash2(c); }\nvec2 relaxed(vec2 c) {\n    vec2 s = site0(c), sum = vec2(0.0); float n = 0.0;\n    for (int j = 0; j < SAMP; j++) for (int i = 0; i < SAMP; i++) {\n        vec2 q = s + (vec2(float(i), float(j)) + 0.5) / float(SAMP) * 2.4 - 1.2;\n        vec2 qc = floor(q);\n        float bd = 1e9; vec2 bs = vec2(0);\n        for (int b = -1; b <= 1; b++) for (int a = -1; a <= 1; a++) {\n            vec2 sc = site0(qc + vec2(a, b));\n            float d = distance(q, sc);\n            if (d < bd) { bd = d; bs = sc; }\n        }\n        if (distance(bs, s) < 1e-4) { sum += q; n += 1.0; }\n    }\n    return n > 0.0 ? sum / n : s;\n}\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n    vec2 uv = fragCoord / iResolution.xy;\n    bool R = uv.x > 0.5;\n    vec2 p = uv * CELLS, ip = floor(p);\n    float d1 = 1e9, d2 = 1e9; vec2 best = vec2(0); vec2 bc = vec2(0);\n    for (int j = -1; j <= 1; j++) for (int i = -1; i <= 1; i++) {\n        vec2 c = ip + vec2(i, j);\n        vec2 s = R ? relaxed(c) : site0(c);\n        float d = distance(p, s);\n        if (d < d1) { d2 = d1; d1 = d; best = s; bc = c; } else if (d < d2) d2 = d;\n    }\n    vec3 col = 0.45 + 0.4 * cos(6.2831 * (hash2(bc).x + vec3(0.0, 0.33, 0.67)));\n    col *= 0.8 + 0.2 * smoothstep(0.0, 0.1, d2 - d1);\n    col = mix(col, vec3(0.05), smoothstep(0.05, 0.0, d2 - d1) * 0.8);\n    col = mix(col, vec3(0.05), smoothstep(0.1, 0.07, d1));\n    col = mix(col, vec3(1.0), smoothstep(0.004, 0.0, abs(uv.x - 0.5)));\n    fragColor = vec4(pow(col, vec3(0.8)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "voronoi-lloyd-relaxation",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.4697,
      "std": 0.1864,
      "unique_colors": 867
     },
     "image": "/specimens/voronoi-lloyd-relaxation-specimen.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "poisson-disk-sampling",
  "worley-noise",
  "voronoi-island-map-generation",
  "wang-tiles",
  "procedural-city-roads"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/procgen/voronoi-lloyd-relaxation.json"
 },
 "solves": [
  "voronoi cells are wildly uneven in size",
  "need evenly shaped map regions or provinces",
  "crack or fragment patterns look too irregular",
  "relax random seeds into regular looking cells"
 ],
 "authority": "Lloyd, 1982",
 "url": "https://texelatlas.vercel.app/t/voronoi-lloyd-relaxation/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 702.95,
  "tier": "extreme",
  "p50_ms": 23.1271,
  "p95_ms": 23.4629,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 20,
  "preset_id": "voronoi-lloyd-relaxation-specimen",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 738.38,
   "tier": "extreme",
   "p50_ms": 11.7402,
   "p95_ms": 11.9788,
   "speedup_x": 1.97,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 503.2,
   "tier": "extreme",
   "p50_ms": 14.4922,
   "p95_ms": 14.7159,
   "speedup_x": 1.6,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}