Created
December 4, 2015 00:11
swift: Geometric Utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ZGeoUtils.swift | |
// ZKit | |
// | |
// Created by Kaz Yoshikawa on 2015/02/08. | |
// Copyright (c) 2015 Electricwoods LLC. All rights reserved. | |
// | |
import Foundation | |
import CoreGraphics | |
func DegreesToRadians(value:Double) -> Double { | |
return value * M_PI / 180.0 | |
} | |
func RadiansToDegrees (value:Double) -> Double { | |
return value * 180.0 / M_PI | |
} | |
func CGRectMakeAspectFill(size: CGSize, bounds: CGRect) -> CGRect { | |
let horizontalRatioToFit = bounds.size.width / size.width | |
let verticalRatioToFit = bounds.size.height / size.height | |
let imageHeightWhenItFitsHorizontally = horizontalRatioToFit * size.height | |
let imageWidthWhenItFitsVertically = verticalRatioToFit * size.width | |
let minX = CGRectGetMinX(bounds) | |
let minY = CGRectGetMinY(bounds) | |
if imageHeightWhenItFitsHorizontally > bounds.size.height { | |
let margin = (imageHeightWhenItFitsHorizontally - bounds.size.height) * 0.5; | |
return CGRectMake(minX, minY - margin, size.width * horizontalRatioToFit, size.height * horizontalRatioToFit) | |
} | |
else { | |
let margin = (imageWidthWhenItFitsVertically - bounds.size.width) * 0.5; | |
return CGRectMake(minX - margin, minY, size.width * verticalRatioToFit, size.height * verticalRatioToFit); | |
} | |
} | |
func CGRectMakeAspectFit(size: CGSize, bounds: CGRect) -> CGRect { | |
let minX = CGRectGetMinX(bounds) | |
let minY = CGRectGetMinY(bounds) | |
let widthRatio = bounds.size.width / size.width | |
let heightRatio = bounds.size.height / size.height | |
let ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio | |
let width = size.width * ratio | |
let height = size.height * ratio | |
let marginX = (bounds.size.width - width) / 2.0 | |
let marginY = (bounds.size.height - height) / 2.0 | |
return CGRectMake(minX + marginX, minY + marginY, width, height) | |
} | |
func CGSizeMakeAspectFit(imageSize: CGSize, frameSize: CGSize) -> CGSize { | |
let widthRatio = frameSize.width / imageSize.width | |
let heightRatio = frameSize.height / imageSize.height | |
let ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio | |
let width = imageSize.width * ratio | |
let height = imageSize.height * ratio | |
return CGSizeMake(width, height) | |
} | |
func CGRectGetMidXMidYPoint(rect: CGRect) -> CGPoint { | |
return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)) | |
} | |
func CGRectGetMinXMinYPoint(rect: CGRect) -> CGPoint { | |
return CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect)) | |
} | |
func CGRectGetMaxXMaxYPoint(rect: CGRect) -> CGPoint { | |
return CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect)) | |
} | |
func CGRectGetMinXMaxYPoint(rect: CGRect) -> CGPoint { | |
return CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect)) | |
} | |
func CGRectGetMaxXMinYPoint(rect: CGRect) -> CGPoint { | |
return CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect)) | |
} | |
func CGRectGetMinXMidYPoint(rect: CGRect) -> CGPoint { | |
return CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect)) | |
} | |
func CGRectGetMaxXMidYPoint(rect: CGRect) -> CGPoint { | |
return CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect)) | |
} | |
func CGRectGetMidXMinYPoint(rect: CGRect) -> CGPoint { | |
return CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)) | |
} | |
func CGRectGetMidXMaxYPoint(rect: CGRect) -> CGPoint | |
{ | |
return CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)) | |
} | |
func CGRectByExtendingWidth(rect: CGRect, width: CGFloat) -> CGRect | |
{ | |
return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width + width, rect.size.height) | |
} | |
func CGRectByExtendingHeight_(rect: CGRect, height: CGFloat) -> CGRect | |
{ | |
return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height + height) | |
} | |
// MARK: - | |
func CGPointOffset(point: CGPoint, offset: CGPoint) -> CGPoint | |
{ | |
return CGPointMake(point.x + offset.x, point.y + offset.y); | |
} | |
func CGRectMakeWithPoints(point1: CGPoint, point2: CGPoint) -> CGRect | |
{ | |
let minX = min(point1.x, point2.x) | |
let minY = min(point1.y, point2.y) | |
let maxX = max(point1.x, point2.x) | |
let maxY = max(point1.y, point2.y) | |
return CGRectMake(minX, minY, maxX - minX, maxY - minY) | |
} | |
// MARK: - | |
func CGPathCreateRoundRect(rect: CGRect, radius: CGFloat) -> CGPathRef | |
{ | |
assert(CGRectGetWidth(rect) >= radius * 2.0, "invalid radius") | |
assert(CGRectGetHeight(rect) >= radius * 2.0, "invalid radius") | |
let t = CGRectGetMinY(rect) | |
let b = CGRectGetMaxY(rect) | |
let l = CGRectGetMinX(rect) | |
let r = CGRectGetMaxX(rect) | |
let pathRef = CGPathCreateMutable() | |
CGPathMoveToPoint(pathRef, nil, l+radius, t) | |
CGPathAddArcToPoint(pathRef, nil, l, t, l, t+radius, radius) | |
CGPathAddArcToPoint(pathRef, nil, l, b, l+radius, b, radius) | |
CGPathAddArcToPoint(pathRef, nil, r, b, r, b-radius, radius) | |
CGPathAddArcToPoint(pathRef, nil, r, t, r-radius, t, radius) | |
CGPathCloseSubpath(pathRef); | |
return pathRef | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment