Skip to content

Clean up Rect set operations (union, intersect, diff, overlaps)#276

Draft
ffreyer wants to merge 9 commits intomasterfrom
ff/rect-diff
Draft

Clean up Rect set operations (union, intersect, diff, overlaps)#276
ffreyer wants to merge 9 commits intomasterfrom
ff/rect-diff

Conversation

@ffreyer
Copy link
Collaborator

@ffreyer ffreyer commented Jan 31, 2026

This adds bbox_diff(a, b) which calculates the bounding box of all rects produced by cutting b out of a (without calculating those rects).

This is something I will probably need for optimizing an SDF volume plots in Makie. Doesn't need to be merged/tagged until I get that near completion

@ffreyer
Copy link
Collaborator Author

ffreyer commented Jan 31, 2026

https://en.wikipedia.org/wiki/Allen%27s_interval_algebra

# http://en.wikipedia.org/wiki/Allen%27s_interval_algebra
function before(b1::Rect{N}, b2::Rect{N}) where {N}
for i in 1:N
maximum(b1)[i] < minimum(b2)[i] || return false
end
return true
end
meets(b1::Rect{N}, b2::Rect{N}) where {N} = maximum(b1) == minimum(b2)
function overlaps(b1::Rect{N}, b2::Rect{N}) where {N}
for i in 1:N
maximum(b2)[i] > maximum(b1)[i] > minimum(b2)[i] &&
minimum(b1)[i] < minimum(b2)[i] || return false
end
return true
end
function starts(b1::Rect{N}, b2::Rect{N}) where {N}
return if minimum(b1) == minimum(b2)
for i in 1:N
maximum(b1)[i] < maximum(b2)[i] || return false
end
return true
else
return false
end
end
function during(b1::Rect{N}, b2::Rect{N}) where {N}
for i in 1:N
maximum(b1)[i] < maximum(b2)[i] && minimum(b1)[i] > minimum(b2)[i] || return false
end
return true
end
function finishes(b1::Rect{N}, b2::Rect{N}) where {N}
return if maximum(b1) == maximum(b2)
for i in 1:N
minimum(b1)[i] > minimum(b2)[i] || return false
end
return true
else
return false
end
end

This stuff makes sense for intervals but I fail to see how this should generalize to rectangles. For examples saying one interval is before another is clear, but for 2D rects it could mean being outside towards -x, or -y or -(x, y) (current), or any of those. meets(a, b) could be:

--.--
a | b
--'--

or

| b |
|---|
| a |

or (current)

  | b
--+---
a |

or other rotations. Also, for the edge versions, do widths need to match for the dimension that doesn't meet?

This doesn't seem clear enough to have. It's been there since the dawn of GeometryTypes 11 years ago

@ffreyer ffreyer changed the title Add rect diff Clean up Rect set operations (union, intersect, diff, overlaps) Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant