Nodes/ComfyUI-TinyBee/Intersect Rects
ComfyUI Node

Intersect Rects

The Overlap of Two Boxes, in One Node

By TinyBeeman·Created about a year ago·Updated about a month ago· 1
Intersect Rects
  • rectA
  • rectB
  • tinyrect

If you work with bounding boxes at all - region prompts, inpaint areas, face/body detection, Florence2-style caption boxes - you will eventually need "where do these two rectangles overlap?" Intersect Rects answers exactly that: feed it two TINYRECTs, get back the shared region as another rect. It's one of the pack's Rectangle category workhorses, and its honest simplicity is both its strength and its only gotcha.

How it works

Two inputs, one output:

  • rectA and rectB - the two rectangles, both in the pack's TINYRECT format: a (x, y, width, height) tuple. Build them with Ints to Rect / Floats to Rect, or pull them from the pack's detection-parsing nodes.
  • tinyrect (output) - the intersection: the overlap region as (x, y, width, height).

The math is textbook: the output's left edge is the larger of the two x-origins, the right edge is the smaller of the two right edges, and similarly for the vertical axis. If one box fully contains the other, you get the contained box back. If they merely touch at an edge, you get a zero-width or zero-height rect.

And here's the gotcha, stated plainly: if the rectangles don't overlap at all, the width or height goes negative. There's no overlap guard. Intersect Rects won't crash - it returns a rect whose width/height are negative numbers, which is a "no intersection" signal only if you know to look for it. Downstream nodes drawing or masking with a negative-size rect will do something weird. So if your two boxes can be disjoint, check the output dimensions before wiring it into an image operation, or structure your workflow so the boxes are guaranteed to intersect.

Where you'd actually use it

  • Inpaint scoping: intersect a detected subject box with a larger region to get the exact area to re-render.
  • Overlap analysis: when a face box and a body box should overlap, the intersection tells you how much of the body box is "face territory."
  • Refinement: intersect a coarse region with a tighter one to crop to the common area.

Installing it

Part of ComfyUI-TinyBee under 🐝TinyBee/Rectangles:

  1. ComfyUI Manager → Install Custom Nodes → search "ComfyUI-TinyBee" → Install, then restart ComfyUI.
cd ComfyUI/custom_nodes
git clone https://github.com/TinyBeeman/ComfyUI-TinyBee

Restart. No models, and the math is a few max/min calls - nothing from the pack's requirements.txt (pillow, jsonata) is involved.

Gotchas

The negative-width-when-disjoint behavior is the one thing to internalize. If your workflow can present non-overlapping boxes, you want a guard before this node, or you want to read the output's w/h and treat non-positive as "empty." The pack's Rect Inside Image, Scale Rect, and the aspect nodes pair naturally with it - the Rectangle category is designed to be a toolkit, and Intersect Rects is the connective tissue between boxes. Also note it takes rects as wires; you can't just type numbers into it, which is why the constructor nodes exist upstream.

Category🐝TinyBee/Rectangles

Inputs (2)

NameTypeDefaultDescription
rectATINYRECT
rectBTINYRECT

Outputs (1)

NameTypeDescription
tinyrectTINYRECT