Two UIBezierPaths intersection as a UIBezierPath

2 Solutions Collect From Internet About “Two UIBezierPaths intersection as a UIBezierPath”

I wrote a UIBezierPath library that will let you cut a given closed path into sub shapes based on an intersecting path. It’ll do essentially exactly what you’re looking for:

NSArray<UIBezierPath*>* componentShapes = [shapePath uniqueShapesCreatedFromSlicingWithUnclosedPath:scissorPath];

Alternatively, you can also just find the intersection points:

NSArray* intersections = [scissorPath findIntersectionsWithClosedPath:shapePath andBeginsInside:nil];

I’m not aware of a method for getting a new path that’s the intersection of two paths, but you can fill or otherwise draw in the intersection by using the clipping property of each path.

In this example, there are two paths, a square and a circle:

let path1 = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 100, height: 100))
let path2 = UIBezierPath(ovalIn: CGRect(x:50, y:50, width: 100, height: 100))

I make a renderer to draw these, but you could be doing this in drawRect or wherever:

let renderer = UIGraphicsImageRenderer(bounds: CGRect(x: 0, y: 0, width: 200, height: 200))
let image = renderer.image {
    context in
    // You wouldn't actually stroke the paths, this is just to illustrate where they are
    // You would just do this part

The resulting image looks like this (I’ve stroked each path for clarity as indicated in the code comments, in actual fact you’d only do the fill part):

enter image description here