Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim/subtract CGPath from CGPath?

I have some CGPath (a poly with two circle) carried out on a CAShapeLayer.

Can I trim/subtract paths like this?

enter image description here

like image 952
Geri Borbás Avatar asked Oct 14 '13 13:10

Geri Borbás


1 Answers

Instead of do the math, the result can be achieved at drawing time using kCGBlendModeClear.

// Create the poly.
CGContextBeginPath(context);
CGContextMoveToPoint       (context, a_.x, a_.y);
CGContextAddLineToPoint    (context, b_.x, b_.y);
CGContextAddLineToPoint    (context, c_.x, c_.y);
CGContextAddLineToPoint    (context, d_.x, d_.y);
CGContextClosePath(context);

// Draw with the desired color.
CGContextSetFillColorWithColor(context, self.color.CGColor);
CGContextFillPath(context);

// Create a circle.
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, (CGRect){
    b_.x - self.radius,
    b_.y - self.radius,
    self.radius * 2.0,
    self.radius * 2.0 });
CGContextClosePath(context);

// Draw with Clear (!) blend mode.
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetFillColorWithColor(context, self.color.CGColor);
CGContextFillPath(context);
like image 113
Geri Borbás Avatar answered Oct 20 '22 13:10

Geri Borbás