# `Macaw.Draw.Geo.Delaunay`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/geo/delaunay.ex#L1)

Planar Delaunay triangulation (spec/09 §9), copied from visualize's
`Visualize.Geo.Delaunay` and the `delaunay` step of its `Visualize.Chart.Transform.Geo`.

    iex> d = Macaw.Draw.Geo.Delaunay.new([{0, 0}, {10, 0}, {0, 10}, {10, 10}])
    iex> {length(Macaw.Draw.Geo.Delaunay.triangles(d)), Macaw.Draw.Geo.Delaunay.hull(d)}
    {2, [0, 1, 3, 2]}

The struct holds `points` (the input), `triangles` (three point indexes per triangle,
each counter-clockwise), `halfedges` (d3-delaunay's form: half-edge `e = 3t + k` runs
from `triangles[e]` to `triangles[3t + (k + 1) rem 3]`, and `halfedges[e]` is the
opposite half-edge, or −1 on the hull) and `hull` (counter-clockwise from the leftmost
point, lowest `x` then lowest `y`).

**The construction is a sweep-hull** (§9.1, §9.2, decision 12), Delaunator's, the
construction under d3-delaunay: a seed triangle, then the other points in order of
distance from its circumcenter, each joined to the hull edges it sees, the hull kept as
a linked cycle with a hash of its points by angle, and each new triangle legalized by
Lawson flips over an explicit stack. It is O(n log n) and pure Elixir: point
coordinates in a tuple, the working arrays in maps. It replaced visualize's incremental
insertion (macaw_frame#38), whose triangles it matches up to the choice among
cocircular points; the triangles' order is its own.

# `error`

```elixir
@type error() :: {:error, [Macaw.Base.Fault.t()]}
```

A refusal: faults with their paths.

# `point`

```elixir
@type point() :: {number(), number()}
```

# `t`

```elixir
@type t() :: %Macaw.Draw.Geo.Delaunay{
  halfedges: [integer()],
  hull: [non_neg_integer()],
  points: [point()],
  triangles: [non_neg_integer()]
}
```

# `columns`

```elixir
@spec columns(t(), :triangles | :hull, [term()]) ::
  %{required(String.t()) =&gt; [term()]} | error()
```

The triangulating step's columns (spec/09 §9.3), `rows` mapping a point's position to its
input row: for `:triangles` one row per triangle in construction order, `"index"`,
`"vertices"` (the three input rows) and `"path"`; for `:hull` one row, `"path"`, the
closed hull, and no row for fewer than three hull points, which enclose nothing
(visualize #1111, D-303). The output is the atom or its spelling.

# `find`

```elixir
@spec find(t(), point()) :: non_neg_integer() | nil | error()
```

The index into `triangles/1` of the first triangle containing `{x, y}` by an inclusive
barycentric test, or `nil` when none does (decision 6; visualize answered −1).

# `hull`

```elixir
@spec hull(t()) :: [non_neg_integer()] | error()
```

The convex hull's point indexes, counter-clockwise from the leftmost point.

# `hull_path`

```elixir
@spec hull_path(t()) :: Macaw.Draw.IR.Path.t() | error()
```

The hull as a closed IR path; empty for an empty hull.

# `neighbors`

```elixir
@spec neighbors(t(), non_neg_integer()) :: [non_neg_integer()] | error()
```

Every point sharing a triangle with point `i`, unique, in triangle order.

# `new`

```elixir
@spec new([point()]) :: t() | error()
```

The triangulation of a list of `{x, y}` points (spec/09 §9.1). Fewer than three points
give no triangles and a hull of every index; points all on one line no triangles and a
hull of the two extreme distinct points; a point coincident with an earlier one is left
out. A list holding anything but `{x, y}` pairs of numbers is `{[i], {:type, :point}}` at
each such position: the Frame drops a `nil` reading before triangulating (§1).

# `points`

```elixir
@spec points(t()) :: [point()] | error()
```

The input points.

# `triangles`

```elixir
@spec triangles(t()) :: [[non_neg_integer()]] | error()
```

The triangles as `[i, j, k]` point indexes, in construction order, each counter-clockwise.

# `triangles_path`

```elixir
@spec triangles_path(t()) :: Macaw.Draw.IR.Path.t() | error()
```

Every triangle as an IR path, `M`, two `L`s and `Z` each.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
