adamwulf / ClippingBezier

ClippingBezier calculates intersection points, paths, and shapes between two UIBezierPaths
http://getlooseleaf.com/opensource/
MIT License
254 stars 34 forks source link

How union two UIBezierPath together #8

Open ArtMartiros opened 5 years ago

ArtMartiros commented 5 years ago

How union two UIBezierPath together using your framework?

adamwulf commented 5 years ago

There's not an explicit union method. You can try using intersectionWithPath: and differenceWithPath: to build the union.

UIBezierPath* pathA;
UIBezierPath* pathB;

UIBezierPath* intersection = [pathA intersectionWithPath:pathB];
UIBezierPath* diffL = [pathA differenceWithPath:pathB];
UIBezierPath* diffR = [pathB differenceWithPath:pathA];

UIBezierPath* union = [UIBezierPath bezierPath];
[union appendPath:intersection];
[union appendPath:diffL];
[union appendPath:diffR];

I think that'll do roughly want you want, though it'll have path lins at the intersections of the two paths instead of a smooth union. stroked path would look odd, but filled path would be ok I believe.

Adenc commented 1 year ago

I found the union path method in the latest library, but an error result is returned.

UIBezierPath *pathA = [UIBezierPath bezierPath];
[pathA addArcWithCenter:CGPointMake(100, 100) radius:100 startAngle:0 endAngle:M_PI*2 clockwise:YES];
[pathA closePath];

UIBezierPath *pathB = [UIBezierPath bezierPath];
[pathB addArcWithCenter:CGPointMake(100, 102) radius:100 startAngle:0 endAngle:M_PI*2 clockwise:YES];
[pathB closePath];

NSArray *result = [pathA unionWithPath:pathB];

@adamwulf

adamwulf commented 1 year ago

Unfortunately there's some problems with paths that have very close derivatives. these two circles meet at almost tange points, and i suspect that's some of the issue. does it return the correct results if the two circles are further apart so that they're intersections have a wider angle?

guoyingtao commented 6 months ago

@ArtMartiros @Adenc Maybe this repo can solve the issue.

Update

Since iOS 16 (or macOS 13), CGPath provides path calculations like union which you can use directly and more performant than some third party libraries.