# Edge routing

> How orthogonal, straight and curved edges are drawn, how they attach to nodes and ports, how to tune routing, and how to use the orthogonal router on its own.

Every edge has a **router** that turns its two ends into a path. Set it per edge with `router`, or for every edge with `defaultEdge.router`.

```ts
createDiagram(container, {
  defaultEdge: { router: 'orthogonal' },
  nodes,
  edges: [
    { id: 'order-pick', source: { node: 'order' }, target: { node: 'pick' } },
    { id: 'pick-stock', source: { node: 'pick' }, target: { node: 'stock' }, router: 'straight', style: { dash: '4 3' } },
    { id: 'pick-pack', source: { node: 'pick' }, target: { node: 'pack' }, router: 'curved' },
  ],
});
```

| Router | Path | Good for |
| --- | --- | --- |
| `'orthogonal'` (default) | Horizontal and vertical segments with rounded corners, routed around other nodes. | Flowcharts, workflows, architecture diagrams. |
| `'straight'` | One straight line between the two ends. | Network topologies, dependency or "reads from" links. |
| `'curved'` | A smooth curve leaving and entering perpendicular to the node sides. | Mind maps and sketches where crossings matter less. |

## Where edges attach

- **With a port** (`source: { node: 'router', port: 'lan2' }`), the edge starts or ends exactly at that port and leaves in the direction of its side.
- **Without a port**, orthogonal and curved edges attach to the middle of the side that faces the other node, and straight edges end on the node's outline (rectangle, ellipse or diamond) on the line towards the other node.
- **Inside a collapsed group**, an edge to a hidden child is drawn to the group instead. Edges between two children of the same collapsed group are hidden until it is expanded.

An edge from a node to itself, which users cannot create by dragging but you can add through the API, is drawn as a loop beside the node.

## The orthogonal router

The orthogonal router finds a path with as few bends as possible that keeps clear of other nodes:

- It searches a sparse grid built from the outlines of nearby nodes (inflated by `routing.padding`), the port stubs and the midpoints between them, using A* with an extra cost per bend.
- The result is a straight line for facing ports, one bend for perpendicular ports, and a two-bend Z or bracket shape for offset or same-side ports.
- The search is limited to a box around both nodes and widens when no route fits. If none is found at all, the router draws a simple route that ignores obstacles, so an edge is never lost.
- Expanded groups and swimlanes are not obstacles, so edges can run into them to reach their children. Collapsed groups are obstacles.

Tune it with `routing`:

| Option | Default | Effect |
| --- | --- | --- |
| `routing.padding` | `16` | Clearance kept around nodes, in diagram units. |
| `routing.bendPenalty` | `24` | Extra cost of each bend. Raise it for fewer bends and longer detours; lower it for shorter paths with more corners. |
| `routing.cornerRadius` | `6` | Radius of the rounded corners. `0` draws sharp corners. |

```ts
diagram.update({ routing: { padding: 24, bendPenalty: 40, cornerRadius: 0 } });
```

Changing `routing` or `defaultEdge` re-routes every edge.

## Labels and markers

An edge's `label` sits at the middle of its path on a background pill, which keeps it readable over grid lines and other edges. Double-click an edge, or select it and press <kbd>Enter</kbd>, to edit the label in place.

Markers are set per end with `markerStart` and `markerEnd`: `'arrow'`, `'arrowOpen'`, `'diamond'`, `'circle'` or `'none'`. A UML-style composition with a filled diamond at the source:

```ts
diagram.addEdges({
  id: 'order-lines',
  source: { node: 'order' },
  target: { node: 'order-line' },
  markerStart: 'diamond',
  markerEnd: 'none',
  label: '1..*',
});
```

## Performance

Routes are cached. When something moves, only edges whose ends moved or whose corridor the moved node crossed are recomputed. Visible edges are routed within a budget of about 12 ms per frame; an edge that has not been routed yet is drawn as a straight line and refined over the next frames, so dragging a node in a dense diagram stays smooth. See [Performance](https://docs.birchui.com/diagram/performance.md).

## Reading routes

`getEdgeRoute(id)` returns the current path points in diagram units, for example to place your own badges or animations along an edge:

```ts
const points = diagram.getEdgeRoute('order-pick');
const middle = points[Math.floor(points.length / 2)];
const screen = diagram.diagramToScreen(middle);
badge.style.transform = `translate(${screen.x}px, ${screen.y}px)`;
```

For curved edges the points are the curve's control points.

## The router on its own

`routeOrthogonal` is exported for servers, tests and your own canvases. Give it the two endpoints (each with a point, the side it leaves from and the node's rectangle) and the obstacles; it returns the bend points.

```ts
import { routeOrthogonal } from '@birchui/diagram';

const points = routeOrthogonal({
  source: { point: { x: 160, y: 32 }, side: 'right', rect: { x: 0, y: 0, width: 160, height: 64 } },
  target: { point: { x: 400, y: 232 }, side: 'left', rect: { x: 400, y: 200, width: 160, height: 64 } },
  obstacles: [{ x: 220, y: 100, width: 120, height: 200 }],
});
```

The result is `[{ x: 160, y: 32 }, { x: 356, y: 32 }, { x: 356, y: 232 }, { x: 400, y: 232 }]`: right along the top, down past the obstacle, then into the target. `padding` and `bendPenalty` are optional and behave like the diagram options; `bounds` limits the search area.

## Next steps

- [Layout](https://docs.birchui.com/diagram/layout.md) to place nodes so routes stay short.
- [Nodes and edges](https://docs.birchui.com/diagram/nodes-and-edges.md#ports) for ports.
