Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created March 15, 2021 08:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/9dd4ebd90df0984042e36243cbd50e44 to your computer and use it in GitHub Desktop.
Save chriseidhof/9dd4ebd90df0984042e36243cbd50e44 to your computer and use it in GitHub Desktop.
/// Takes an absolute path and fits it inside the proposed rectangle.
struct FittingShape: Shape {
var absolutePath: Path
func path(in rect: CGRect) -> Path {
let p = absolutePath
let boundingRect = p.boundingRect
let scale = min(rect.width/boundingRect.width, rect.height/boundingRect.height)
let scaled = p.applying(.init(scaleX: scale, y: scale))
let scaledBoundingRect = scaled.boundingRect
let offsetX = scaledBoundingRect.midX - rect.midX
let offsetY = scaledBoundingRect.midY - rect.midY
return scaled.offsetBy(dx: -offsetX, dy: -offsetY)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment