ClippingBezier icon indicating copy to clipboard operation
ClippingBezier copied to clipboard

How union two UIBezierPath together

Open ArtMartiros opened this issue 5 years ago • 4 comments

How union two UIBezierPath together using your framework?

ArtMartiros avatar Mar 21 '19 07:03 ArtMartiros

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.

adamwulf avatar Mar 22 '19 00:03 adamwulf

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

Adenc avatar Jun 15 '23 09:06 Adenc

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?

adamwulf avatar Jun 16 '23 02:06 adamwulf

@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.

guoyingtao avatar Jan 30 '24 05:01 guoyingtao