Skip to content

Instantly share code, notes, and snippets.

@dagronf
Last active March 8, 2024 00:37
Show Gist options
  • Save dagronf/cb9ea487b10151e69e2555e819b1ef3d to your computer and use it in GitHub Desktop.
Save dagronf/cb9ea487b10151e69e2555e819b1ef3d to your computer and use it in GitHub Desktop.
A CGPath extension for generating squircle (superellipse) paths. Adapted from the PaintCode blog (https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles)
//
// Copyright © 2024 Darren Ford. All rights reserved.
//
// Adapted from the PaintCode blog: https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles
//
// MIT license
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// PaintCode License: -
//
// Created by Matej Dunik on 11/12/13.
// Copyright (c) 2013 PixelCut. All rights reserved except as below:
// This code is provided as-is, without warranty of any kind. You may use it in your projects as you wish.
//
import Foundation
import CoreGraphics.CGPath
// Source: [PaintCode](https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles)
extension CGPath {
/// Create a superellipse (squircle) within the specified rect with a default corner radius
/// - Parameters:
/// - rect: The superellipse rect
/// - cornerRadius: The corner radius
/// - Returns: A path containing the superellipse
///
/// Source: [PaintCode](https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles)
@inline(__always) static func squircle(in rect: CGRect) -> CGPath {
Self.superellipse(in: rect, cornerRadius: min(rect.width, rect.height))
}
/// Create a superellipse (squircle) within the specified rect with a default corner radius
/// - Parameters:
/// - rect: The superellipse rect
/// - Returns: A path containing the superellipse
///
/// Source: [PaintCode](https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles)
@inline(__always) static func superellipse(in rect: CGRect) -> CGPath {
Self.superellipse(in: rect, cornerRadius: min(rect.width, rect.height))
}
/// Create a superellipse (squircle) within the specified rect
/// - Parameters:
/// - rect: The squircle rect
/// - cornerRadius: The corner radius
/// - Returns: A path containing the squircle
///
/// Source: [PaintCode](https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles)
@inline(__always) static func squircle(in rect: CGRect, cornerRadius: CGFloat) -> CGPath {
Self.superellipse(in: rect, cornerRadius: cornerRadius)
}
/// Create a superellipse (squircle) within the specified rect
/// - Parameters:
/// - rect: The superellipse rect
/// - cornerRadius: The corner radius
/// - Returns: A path containing the superellipse
///
/// Source: [PaintCode](https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles)
static func superellipse(in rect: CGRect, cornerRadius: CGFloat) -> CGPath {
let limit: CGFloat = min(rect.size.width, rect.size.height) / 2 / 1.52866483
let limitedRadius: CGFloat = min(cornerRadius, limit)
@inline(__always) func topLeft(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
CGPoint(
x: rect.origin.x + x * limitedRadius,
y: rect.origin.y + y * limitedRadius
)
}
@inline(__always) func topRight(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
CGPoint(
x: rect.origin.x + rect.size.width - x * limitedRadius,
y: rect.origin.y + y * limitedRadius
)
}
@inline(__always) func bottomRight(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
CGPoint(
x: rect.origin.x + rect.size.width - x * limitedRadius,
y: rect.origin.y + rect.size.height - y * limitedRadius
)
}
@inline(__always) func bottomLeft(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
CGPoint(
x: rect.origin.x + x * limitedRadius,
y: rect.origin.y + rect.size.height - y * limitedRadius
)
}
let path = CGMutablePath()
path.move(to: topLeft(1.52866483, 0.00000000))
path.addLine(to: topRight(1.52866471, 0.00000000))
path.addCurve(
to: topRight(0.66993427, 0.06549600),
control1: topRight(1.08849323, 0.00000000),
control2: topRight(0.86840689, 0.00000000)
)
path.addLine(to: topRight(0.63149399, 0.07491100))
path.addCurve(
to: topRight(0.07491176, 0.63149399),
control1: topRight(0.37282392, 0.16905899),
control2: topRight(0.16906013, 0.37282401)
)
path.addCurve(
to: topRight(0.00000000, 1.52866483),
control1: topRight(0.00000000, 0.86840701),
control2: topRight(0.00000000, 1.08849299)
)
path.addLine(to: bottomRight(0.00000000, 1.52866471))
path.addCurve(
to: bottomRight(0.06549569, 0.66993493),
control1: bottomRight(0.00000000, 1.08849323),
control2: bottomRight(0.00000000, 0.86840689)
)
path.addLine(to: bottomRight(0.07491111, 0.63149399))
path.addCurve(
to: bottomRight(0.63149399, 0.07491111),
control1: bottomRight(0.16905883, 0.37282392),
control2: bottomRight(0.37282392, 0.16905883)
)
path.addCurve(
to: bottomRight(1.52866471, 0.00000000),
control1: bottomRight(0.86840689, 0.00000000),
control2: bottomRight(1.08849323, 0.00000000)
)
path.addLine(to: bottomLeft(1.52866483, 0.00000000))
path.addCurve(
to: bottomLeft(0.66993397, 0.06549569),
control1: bottomLeft(1.08849299, 0.00000000),
control2: bottomLeft(0.86840701, 0.00000000)
)
path.addLine(to: bottomLeft(0.63149399, 0.07491111))
path.addCurve(
to: bottomLeft(0.07491100, 0.63149399),
control1: bottomLeft(0.37282401, 0.16905883),
control2: bottomLeft(0.16906001, 0.37282392)
)
path.addCurve(
to: bottomLeft(0.00000000, 1.52866471),
control1: bottomLeft(0.00000000, 0.86840689),
control2: bottomLeft(0.00000000, 1.08849323)
)
path.addLine(to: topLeft(0.00000000, 1.52866483))
path.addCurve(
to: topLeft(0.06549600, 0.66993397),
control1: topLeft(0.00000000, 1.08849299),
control2: topLeft(0.00000000, 0.86840701)
)
path.addLine(to: topLeft(0.07491100, 0.63149399))
path.addCurve(
to: topLeft(0.63149399, 0.07491100),
control1: topLeft(0.16906001, 0.37282401),
control2: topLeft(0.37282401, 0.16906001)
)
path.addCurve(
to: topLeft(1.52866483, 0.00000000),
control1: topLeft(0.86840701, 0.00000000),
control2: topLeft(1.08849299, 0.00000000)
)
path.closeSubpath()
return path
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment