linebender / kurbo

A Rust library for manipulating curves
Apache License 2.0
716 stars 69 forks source link

Add the `Rect::overlaps` and `Rect::contains_rect` methods #347

Closed nils-mathieu closed 2 months ago

nils-mathieu commented 5 months ago

I stumbled on this while toying with the vello rendering engine (which is great btw, you guys are doing god's work). I'm trying to create a simple audio sequencer with it and while trying to determine whether shapes were visible on screen, I found that there was no way of checking for the intersection of two Rects.

Right now, the only way to do that is to compute their intersection, then verify whether it's empty or not.

let rect1 = Rect::new(0.0, 0.0, 10.0, 10.0);
let rect2 = Rect::new(5.0, 5.0, 15.0, 15.0);
assert!(!rect1.intersect(rect2).is_empty());

This is not ideal for two reasons:

  1. It's not very ergonomic. Checking whether two rectangles overlap should probably not involve that intermediary step.
  2. This is doing a bunch of wasted work to find out what the intersection is when we only care about whether it's empty or not.

This pull request adds Rect::overlaps and Rect::contains_rect, which do exactly that.

let rect1 = Rect::new(0.0, 0.0, 10.0, 10.0);
let rect2 = Rect::new(5.0, 5.0, 15.0, 15.0);
assert!(rect1.overlaps(rect2));
assert!(!rect1.contains_rect(rect2));
nils-mathieu commented 4 months ago

My mistake, I completely forgot about this. I just commited the suggested modifications.

waywardmonkeys commented 2 months ago

@nils-mathieu Could you please rebase this on top of current main to pick up the CI changes / updates? Thanks!

nils-mathieu commented 2 months ago

Github's autosync merged rather than rebasing without asking, I hope it's okay. Seems green though x)

waywardmonkeys commented 2 months ago

One last thing (from me): Could you add an entry to CHANGELOG.md? Just use the existing format, add the PR number sorted numerically and your GitHub user name sorted alphabetically and an entry for this under 'Added'.

nils-mathieu commented 2 months ago

done!

waywardmonkeys commented 2 months ago

Thanks everyone!