Skip to content

Extension #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/CGPoint+GeometryKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// File.swift
//
//
// Created by Muukii on 2022/03/28.
//

import Foundation
26 changes: 26 additions & 0 deletions Sources/CGRect+GeometryKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import CoreGraphics.CGGeometry

extension CGRect {

/// Returns a rectangle that fits inside provided bounding rectangle with respecting aspect ratio.
public func gk_makeRectThatAspectFit(
aspectRatio: CGSize
) -> CGRect {

let size = aspectRatio.gk_sizeThatAspectFit(in: self.size)
var origin = origin
origin.x += (size.width - size.width) / 2.0
origin.y += (size.height - size.height) / 2.0
return CGRect(origin: origin, size: size)
}

public func gk_makeRectThatAspectFill(aspectRatio: CGSize) -> CGRect {

let minimumRect = self
let size = aspectRatio.gk_sizeThatAspectFill(minimumSize: minimumRect.size)
var origin = CGPoint.zero
origin.x = (minimumRect.size.width - size.width) / 2.0
origin.y = (minimumRect.size.height - size.height) / 2.0
return CGRect(origin: origin, size: size)
}
}
67 changes: 67 additions & 0 deletions Sources/CGSize+GeometryKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import CoreGraphics.CGGeometry

extension CGSize {

public func gk_sizeThatAspectFit(maxSize: CGFloat) -> CGSize {

guard width >= maxSize || height >= maxSize else {
return self
}

var s = self

if width > height {
s.width = maxSize
s.height *= maxSize / width
} else {
s.height = maxSize
s.width *= maxSize / height
}

s.width.round()
s.height.round()

return s
}

public func gk_sizeThatAspectFit(in boundingSize: CGSize) -> CGSize {

let aspectRatio = self

let widthRatio = boundingSize.width / aspectRatio.width
let heightRatio = boundingSize.height / aspectRatio.height
var size = boundingSize

if widthRatio < heightRatio {
size.height = boundingSize.width / aspectRatio.width * aspectRatio.height
} else if heightRatio < widthRatio {
size.width = boundingSize.height / aspectRatio.height * aspectRatio.width
}

return CGSize(
width: ceil(size.width),
height: ceil(size.height)
)
}

public func gk_sizeThatAspectFill(minimumSize: CGSize) -> CGSize {

let aspectRatio = self

let widthRatio = minimumSize.width / aspectRatio.width
let heightRatio = minimumSize.height / aspectRatio.height

var size = minimumSize

if widthRatio > heightRatio {
size.height = minimumSize.width / aspectRatio.width * aspectRatio.height
} else if heightRatio > widthRatio {
size.width = minimumSize.height / aspectRatio.height * aspectRatio.width
}

return CGSize(
width: ceil(size.width),
height: ceil(size.height)
)
}
}
70 changes: 12 additions & 58 deletions Sources/GeometryKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,33 @@ public enum Geometry {

extension Geometry {

public static func sizeThatAspectFit(size: CGSize, maxPixelSize: CGFloat) -> CGSize {
public static func sizeThatAspectFit(
size: CGSize,
maxPixelSize: CGFloat
) -> CGSize {

guard size.width >= maxPixelSize || size.height >= maxPixelSize else {
return size
}

var s = size

if size.width > size.height {
s.width = maxPixelSize
s.height *= maxPixelSize / size.width
} else {
s.height = maxPixelSize
s.width *= maxPixelSize / size.height
}

s.width.round()
s.height.round()

return s
size.gk_sizeThatAspectFit(maxSize: maxPixelSize)
}

public static func sizeThatAspectFit(aspectRatio: CGSize, boundingSize: CGSize) -> CGSize {
let widthRatio = boundingSize.width / aspectRatio.width
let heightRatio = boundingSize.height / aspectRatio.height
var size = boundingSize

if widthRatio < heightRatio {
size.height = boundingSize.width / aspectRatio.width * aspectRatio.height
} else if heightRatio < widthRatio {
size.width = boundingSize.height / aspectRatio.height * aspectRatio.width
}

return CGSize(
width: ceil(size.width),
height: ceil(size.height)
)

aspectRatio.gk_sizeThatAspectFit(in: boundingSize)
}

public static func sizeThatAspectFill(aspectRatio: CGSize, minimumSize: CGSize) -> CGSize {
let widthRatio = minimumSize.width / aspectRatio.width
let heightRatio = minimumSize.height / aspectRatio.height

var size = minimumSize

if widthRatio > heightRatio {
size.height = minimumSize.width / aspectRatio.width * aspectRatio.height
} else if heightRatio > widthRatio {
size.width = minimumSize.height / aspectRatio.height * aspectRatio.width
}

return CGSize(
width: ceil(size.width),
height: ceil(size.height)
)

aspectRatio.gk_sizeThatAspectFill(minimumSize: minimumSize)
}

/// Returns a rectangle that fits inside provided bounding rectangle with respecting aspect ratio.
public static func rectThatAspectFit(aspectRatio: CGSize, boundingRect: CGRect) -> CGRect {
let size = sizeThatAspectFit(aspectRatio: aspectRatio, boundingSize: boundingRect.size)
var origin = boundingRect.origin
origin.x += (boundingRect.size.width - size.width) / 2.0
origin.y += (boundingRect.size.height - size.height) / 2.0
return CGRect(origin: origin, size: size)

boundingRect.gk_makeRectThatAspectFit(aspectRatio: aspectRatio)
}

/// Returns a rectangle that fills inside provided bounding rectangle with respecting aspect ratio.
public static func rectThatAspectFill(aspectRatio: CGSize, minimumRect: CGRect) -> CGRect {
let size = sizeThatAspectFill(aspectRatio: aspectRatio, minimumSize: minimumRect.size)
var origin = CGPoint.zero
origin.x = (minimumRect.size.width - size.width) / 2.0
origin.y = (minimumRect.size.height - size.height) / 2.0
return CGRect(origin: origin, size: size)
minimumRect.gk_makeRectThatAspectFill(aspectRatio: aspectRatio)
}

public static func diagonalRatio(to: CGSize, from: CGSize) -> CGFloat {
Expand Down