# Swift... Clip to a Bezier Curve

**URL:** <https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373>\
**Category:** Swift\
**Created:** [10 July 2023 14:26 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373 "2023-07-10T14:26:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [10 July 2023 14:26 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373/1 "2023-07-10T14:26:19Z")

</div>

my app contains this code

```auto
    public func drawPieSlice(_ rect:CGRect,startAngle:CGFloat,endAngle:CGFloat,fill:Bool=false) {
        let r = adjRect(rect)
        let center : CGPoint = CGPoint(x: r.midX,y: r.midY)
        let radius : CGFloat = min(r.width,r.height)/2
        
        let path = UIBezierPath(circleSegmentCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle)
        if fill==true {
            path.fill()
        } else {
            path.lineWidth = penSize
            path.stroke()
        }
    }

```

What I would like to add is the ability to “clip” to the created path, and draw lines for cross hatching inside the defined area. Any ideas?

---

<div class="post-metadata">

**Author:** ![TomasJ](https://ifnotnil.com/user_avatar/ifnotnil.com/tomasj/32/4998_2.png) [@TomasJ](https://ifnotnil.com/u/TomasJ)\
**Post date:** [10 July 2023 17:07 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373/2 "2023-07-10T17:07:53Z")

</div>

I used a rect in xojo for Clipped Pies… many years ago 😉

> **[Free Control for Dashboards](https://forum.xojo.com/t/free-control-for-dashboards/12321)**
>
> Hi Xojo Developers, today I throw in this xojo control for free (LGPL). It is not perfect but it simply does its job in displaying percentage values on a Dashboard. Feel free to download, improve and use in your projects. It would be great to...

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [10 July 2023 17:25 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373/3 "2023-07-10T17:25:50Z")

</div>

not sure how clipping to a rect is the same as clipping to a shape that is not a rect

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [10 July 2023 18:20 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373/4 "2023-07-10T18:20:45Z")

</div>

maybe this ?

CGContextEOClip

sounds like it lets you modify the current clipping path with a new one

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [10 July 2023 21:25 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373/5 "2023-07-10T21:25:33Z")

</div>

It should be so while the flexibility is needed for all dynamically changed graphics.

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [13 July 2023 02:16 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373/6 "2023-07-13T02:16:59Z")

</div>

NSBeizerPath has a setClip function.

> **[setClip() | Apple Developer Documentation](https://developer.apple.com/documentation/appkit/nsbezierpath/1520704-setclip)**
>
> Replaces the clipping path of the current graphics context with the area inside the path.

UIBezierPath has a addClip function which seems like it should do the same thing.

> **[addClip() | Apple Developer Documentation](https://developer.apple.com/documentation/uikit/uibezierpath/1624341-addclip)**
>
> Uses the clipping path of the current graphics context to intersect the region that the path encloses, and makes the resulting shape the current clipping path.

Failing that, you can get the CGPath, get the CGContext from the current NSGraphicsContext (or equivalent) and then use CGContextClipToPath

> **[cgPath | Apple Developer Documentation](https://developer.apple.com/documentation/uikit/uibezierpath/1624342-cgpath)**
>
> The Core Graphics representation of the path.

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [13 July 2023 04:55 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373/7 "2023-07-13T04:55:45Z")

</div>

AddClip was what did it… thanks

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [13 July 2023 05:11 UTC](https://ifnotnil.com/t/swift-clip-to-a-bezier-curve/3373/8 "2023-07-13T05:11:19Z")

</div>

You’re welcome.
