# Path.op(): Combining Shapes Without Clip Gymnastics

> Union, difference, intersect, and xor on Path let you combine or cut shapes directly, instead of layering clips or drawing extra geometry.

**Date:** 2026-10-19T00:00:00.000Z
**Tags:** Canvas, Path, Math

## The intuition

Need to cut a notch out of a card, or merge two overlapping blobs into one
outline? `clip()` feels like the tool for the job, but it isn't built for
this:

* `clip()` only ever restricts what's already drawn to a
  stencil (A shape that decides where paint is allowed to land, without itself being painted: a binary, pre-filtering test applied before each paint operation runs, as opposed to a mask, which multiplies by alpha after painting.)
  shape. It can't merge two shapes into one new outline, and it can't
  produce "just the overlap" or "A minus B" as a real, reusable path.
* The usual workaround is faking it: draw the card, then paint a
  background-colored circle on top to simulate a bite taken out of the
  edge. It looks right until you add a `border()`, which still traces the
  full rounded rect, ignoring the circle entirely, because nothing about
  the underlying path ever changed. Hit-testing and `PathMeasure` are
  wrong the same way, for the same reason.
* `Path.op()` computes an actual new `Path`, geometrically, from two
  source paths and a boolean rule. The notch (or the merge, or the
  overlap) becomes real outline data, not a visual approximation.

- [Clipping Fundamentals: Canvas Clip Stack vs. Layer + Outline Mask](/canvas-clip-vs-layer-mask) — What clip() actually is, a stencil in front of paint operations, which is exactly the tool this post shows the limits of.

- [PathMeasure: Position and Direction at Any Distance Along a Path](/path-measure-position-along-path) — Path.op() hands back a real Path, and a real Path is exactly what PathMeasure needs to walk, stroke-reveal, or sample by distance.

## Cutting a notch: clip() can't subtract, Path.op() can

```kotlin
val card = Path().apply {
    addRoundRect(RoundRect(0f, 0f, 300f, 160f, cornerRadius = CornerRadius(16f)))
}
val notch = Path().apply {
    addOval(Rect(center = Offset(150f, 0f), radius = 24f))
}

// Faking it: paint a background-colored circle over the edge.
// Looks right with no border. Add one, and it still traces the
// untouched rounded rect, because `card` itself never changed.
drawPath(card, color = Color.White)
drawCircle(color = pageBackground, radius = 24f, center = Offset(150f, 0f))
drawPath(card, color = Color.Black, style = Stroke(width = 2f)) // ignores the notch

// Path.op(): the notch is cut from the actual outline.
val cut = Path()
cut.op(card, notch, PathOperation.Difference)
drawPath(cut, color = Color.White)
drawPath(cut, color = Color.Black, style = Stroke(width = 2f)) // follows the notch
```

![Two rounded-rectangle cards compared: the faked notch on the left shows a border that ignores a background-colored circle painted over its edge, while the Path.op() Difference result on the right shows the border actually following the real cut.](/posts/path-op-boolean-combination/faked-vs-real-notch.webp)

*Faked notch (left): border ignores it. Path.op() Difference (right): border follows the real cut.*

`cut` is a real path: stroke it, clip to it, hit-test against it, or feed
it to `PathMeasure`, and every one of those sees the notch, because it's
sitting in the path's own point data, not painted over it.

## Four operations, one pair of shapes

The same mechanism answers more than "cut a notch." Two overlapping
circles, four different results, depending only on which `PathOperation`
you pick:

```kotlin
val a = Path().apply { addOval(Rect(0f, 0f, 200f, 200f)) }
val b = Path().apply { addOval(Rect(100f, 0f, 300f, 200f)) }
val result = Path().apply { op(a, b, operation) }
```

Same two circles, same call shape, only `operation` changes:

| `operation` | `result` keeps |
|---|---|
| `PathOperation.Union` | everything covered by either circle |
| `PathOperation.Intersect` | only the lens-shaped overlap |
| `PathOperation.Difference` | circle `a`, with the overlap bitten out |
| `PathOperation.Xor` | everything covered by exactly one circle, not both |

*\[Interactive demo: PathOpDemo — view this post in a browser for the live version.]*

`Union` and `Intersect` are symmetric: swapping `a` and `b` gives the same
result either way. `Difference` isn't: it always means "first argument
minus second." `PathOperation.ReverseDifference` exists specifically so
you can flip which shape gets subtracted without reordering the
arguments, useful when `a` and `b` are named variables you don't want to
swap around the codebase.

> **The fundamental**
>
> This is **boolean path combination**: treating two shapes as sets of
> covered points and combining them with set operations. Union keeps points
> in *either* shape, intersect keeps points in *both*, difference keeps
> points in the first shape but *not* the second, and xor keeps points in
> *exactly one*. The result is computed as real geometry, a new sequence of
> points and curves, not a visual effect layered on top of the originals.

## Elsewhere: same fundamental, different framework

* **Figma / Illustrator Pathfinder**: the same four operations, under the
  names Union, Subtract, Intersect, and Exclude. Subtract is `Difference`,
  Exclude is `Xor`, and designers reach for exactly this panel to do the
  same notch-cutting and shape-merging this post is about.
* **SwiftUI / Core Graphics**: conspicuously missing. `CGPath`,
  `UIBezierPath`, and SwiftUI's `Path` have no built-in union, subtract,
  intersect, or xor — iOS developers reach for a third-party library like
  VectorBoolean, or fall back to exactly the "paint a shape on top" fake
  this post opens by debunking, because the platform never shipped the
  real thing.
* **Blender's Boolean modifier, OpenSCAD's `union()`/`difference()`/
  `intersection()`**: the identical math promoted from 2D paths to 3D
  volumes, under its formal name, **constructive solid geometry (CSG)**.
  A notch cut into a card and a hole drilled through a 3D-printed bracket
  are the same set operation, one dimension apart.
* **Shapely** (Python): `.union()`, `.difference()`, `.intersection()`,
  and `.symmetric_difference()` on polygon objects, used for GIS and
  spatial-overlap queries rather than UI, but it's the same four
  operations under their literal set-theory names.

## Edge cases

**The `Boolean` return value signals a failed computation, not an empty
result.** `op()` returns `false` when it can't compute at all, typically
because one of the input paths contains a non-finite point, not because
the resulting shape happens to be empty. Intersecting two circles that
don't overlap is a perfectly valid call that returns `true` and leaves
you with an empty path, same as intersecting any other disjoint shapes.

**The receiver's prior contents are replaced, not merged in.**
`result.op(a, b, operation)` sets `result` *to* the outcome of combining
`a` and `b`; whatever `result` held before the call is discarded, not
folded into the computation. Reusing a long-lived `Path` as the
destination needs no `reset()` first, but it also means you can't rely on
old contents sticking around. To build up a result across several
operations, feed the previous call's output back in as one of the next
call's two input paths, rather than calling `op()` repeatedly on the same
object expecting it to accumulate.

**Fill rule still governs each source shape first.** Before the boolean
math runs, "is this point inside path `a`" has to be answered for a and b
individually, the same self-intersecting-path question any fill rule
answers. A self-intersecting source path still gets resolved by its fill
rule before `op()` ever combines it with the other shape.

## Mental model to actually use

**`Path.op()` doesn't fake a cut or a merge with layered drawing, it
computes a new, real outline.** Reach for it any time the shape you want
isn't one you can draw directly, but is one you can describe as "A and B,
combined by a set operation": stroke, clip, hit-test, and `PathMeasure`
all end up seeing the same shape you do.


---

Canonical page: https://recomposed.blog/path-op-boolean-combination