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 to split a bezier path #11

Closed prasad1120 closed 4 years ago

prasad1120 commented 4 years ago

Hi Adam,

I am trying to split a path with another path like so:

let splitterPath = UIBezierPath(rect: someRect)
UIColor.black.setStroke()
splitterPath.lineWidth = 3
splitter.stroke()

UIColor.green.setFill()
splittingPath.fill()

// returns empty array
splittingPath.uniqueShapesCreatedFromSlicing(withUnclosedPath: splitterPath) 

However, uniqueShapesCreatedFromSlicing returns an empty array. I was hoping for a green shape to be returned without the extraneous part outside the black box(splitterPath).

Simulator Screen Shot - iPhone X - 2020-06-03 at 23 39 54

Please let me know if I am missing something and how to get the desired result.

adamwulf commented 4 years ago

That'd odd, it should be giving you two shapes if i'm understanding correctly. the green portion inside the black box and the smaller triangle green portion outside the box. Can you reply with the contents of each path? if you print description of them in the Xcode console debugger, then it'll print out an obj-c representation of the path. that'll help me track down specifics of what might be going on.

prasad1120 commented 4 years ago

print(splitterPath.debugDescription)

path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(19.000000, 26.870058)];
[path addLineToPoint:CGPointMake(201.000000, 26.870058)];
[path addLineToPoint:CGPointMake(201.000000, 121.000000)];
[path addLineToPoint:CGPointMake(19.000000, 121.000000)];
[path closePath];

print(splittingPath.debugDescription)

path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(19.000000, 39.000000)];
[path addCurveToPoint:CGPointMake(19.000000, 39.000000) controlPoint1:CGPointMake(19.000000, 39.000000) controlPoint2:CGPointMake(19.000000, 39.000000)];
[path addLineToPoint:CGPointMake(19.000000, 19.000000)];
[path addLineToPoint:CGPointMake(19.000000, 121.000000)];
[path addCurveToPoint:CGPointMake(19.000000, 121.000000) controlPoint1:CGPointMake(19.000000, 121.000000) controlPoint2:CGPointMake(19.000000, 121.000000)];
[path addLineToPoint:CGPointMake(201.000000, 121.000000)];
[path addLineToPoint:CGPointMake(201.000000, 121.000000)];
[path addCurveToPoint:CGPointMake(201.000000, 121.000000) controlPoint1:CGPointMake(201.000000, 121.000000) controlPoint2:CGPointMake(201.000000, 121.000000)];
[path addLineToPoint:CGPointMake(201.000000, 39.000000)];
[path addLineToPoint:CGPointMake(201.000000, 39.000000)];
[path addCurveToPoint:CGPointMake(201.000000, 39.000000) controlPoint1:CGPointMake(201.000000, 39.000000) controlPoint2:CGPointMake(201.000000, 39.000000)];
[path addLineToPoint:CGPointMake(220.000000, 39.000000)];
[path addLineToPoint:CGPointMake(220.000000, 39.000000)];
[path addCurveToPoint:CGPointMake(206.564971, 33.435029) controlPoint1:CGPointMake(214.960887, 39.000000) controlPoint2:CGPointMake(210.128162, 36.998220)];
[path addLineToPoint:CGPointMake(200.000000, 26.870058)];
[path addLineToPoint:CGPointMake(195.778175, 31.091883)];
[path addLineToPoint:CGPointMake(195.778175, 31.091883)];
[path addCurveToPoint:CGPointMake(176.686292, 39.000000) controlPoint1:CGPointMake(190.714692, 36.155365) controlPoint2:CGPointMake(183.847137, 39.000000)];
[path addLineToPoint:CGPointMake(19.000000, 39.000000)];
adamwulf commented 4 years ago

ah, the issue is that splittingPath needs to b a closedPath. It does end at the same point it starts at, but needs to have an explicit closePath element. splittingPath.close() should do the trick

prasad1120 commented 4 years ago

I was the under the impression that returning back to the starting point is same as .close(). Two values are getting returned after closing the path now. Thanks a lot!

adamwulf commented 4 years ago

Glad that sorted it! :)