Is it possible to remove a chunk of NSBezierPath
that is defined by an NSRect
region within the path?
Asked
Active
Viewed 2,985 times
7
3 Answers
11
As was noted in the comments, Mr. Caswell's answer is actually the opposite of what the OP was asking. This code sample shows how to remove a rect from a circle (or any bezier path from any other bezier path). The trick is to "reverse" the path that you want to remove, then append it to the original path:
NSBezierPath *circlePath = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(0, 0, 100, 100)];
NSBezierPath *rectPath = [NSBezierPath bezierPathWithRect:NSMakeRect(25, 25, 50, 50)];
rectPath = [rectPath bezierPathByReversingPath];
[circlePath appendBezierPath:rectPath];
Note: things get a little trickier if the bezier paths cross each other. Then you have to set the proper "winding rule".

Roberto
- 391
- 5
- 8
5
Absolutely. This is what clipping regions do:
// Save the current clipping region
[NSGraphicsContext saveGraphicsState];
NSRect dontDrawThisRect = NSMakeRect(x, y, w, h);
// Either:
NSRectClip(dontDrawThisRect);
// Or (usually for more complex shapes):
//[[NSBezierPath bezierPathWithRect:dontDrawThisRect] addClip];
[myBezierPath fill]; // or stroke, or whatever you do
// Restore the clipping region for further drawing
[NSGraphicsContext restoreGraphicsState];

jscs
- 63,694
- 13
- 151
- 195
-
4NSRectClip is not correct. It intersects with the current clippath. So after applying it only content within dontDrawThisRect is drawn instead taking out this area from the clipping region. So it is quite the opposite of what the OP wanted. – Mike Lischke Apr 13 '13 at 12:53
-
Can I use this to remove a circle drawn in my program? I am trying to animate a circle in my program.The circle is moving along a path so I am doing is create a new circle in every iteration of a for loop.But how do I remove the old circle? Can anybody please help me? – zzzzz Oct 29 '13 at 07:35
-
Here is my question http://stackoverflow.com/questions/19650688/making-a-ball-circle-move-in-cocoa-using-nsbezierpathobjective-c – zzzzz Oct 29 '13 at 07:36
0
Based on Roberto's answer, I've updated the code to Swift 5. This will draw a red 100x100 circle, with a 60x60 rectangle cut out in the middle.
let path = NSBezierPath(ovalIn: NSRect(x: 0, y: 0, width: 100, height: 100))
let cutoutRect = NSBezierPath(rect: NSRect(x: 20, y: 20, width: 60, height: 60)).reversed
path.append(cutoutRect)
NSColor.red.setFill()
path.fill()

dAngelov
- 830
- 6
- 10